This site requires JavaScript, please enable it in your browser!
Greenfoot back
Ariadia
Ariadia wrote ...

2015/5/10

Urgent Help! "java.lang.StackOverflowError"

Ariadia Ariadia

2015/5/10

#
"java.lang.StackOverflowError at greenfoot.Actor.getClassImage(Actor.java:646) at greenfoot.Actor.<init>(Actor.java:124) at UpCard.<init>(UpCard.java:9) at CardA.<init>(CardA.java:9)" The last two repeat a BUNCH before stopping. I was trying to make a random card image appear when clicked, and then for each card to have an answer. (I know that doesn't make much sense, but if you know what Quizlet is, it's sort of a quiz type of thing.... eh.) I think the problem may lie in the code for UpCard and CardA (B-D as well)... Here's the code for UpCard:
public void addCard(){
        int n = Greenfoot.getRandomNumber(6);
        if (n == 1){
            getWorld().addObject(A, getX(), getY());
            getWorld().removeObject(this);
        }
        else if (n == 2){
            getWorld().addObject(B, getX(), getY());
            getWorld().removeObject(this);
        }
        else if (n == 3){
            getWorld().addObject(C, getX(), getY());
            getWorld().removeObject(this);
        }
        else if (n == 4){
            getWorld().addObject(D, getX(), getY());
            getWorld().removeObject(this);
        }
        else if (n == 1){
            getWorld().addObject(M, getX(), getY());
            getWorld().removeObject(this);
        }
    }
And CardA:
public void chooseCard(){
        int i = Greenfoot.getRandomNumber(11);
        GreenfootImage img = new GreenfootImage("CardA" + i + ".jpg");
        setImage(img);
    }
I have the same code in CardB-D. This project is due tomorrow so if someone can help me asap, it'd be so much appreciated! :0
danpost danpost

2015/5/10

#
Ariadia wrote...
The last two repeat a BUNCH before stopping.
This should be a clue. Each class is creating instances of the other class from their constructors. One says to create an instance of the other and then the other says create an instance of the one -- which creates an instance of the other which creates an instance of the one, etc. etc.
Ariadia Ariadia

2015/5/10

#
So, like a never ending loop? Okay.. How do I fix it? I don't really understand where the problem is.
danpost danpost

2015/5/11

#
Ariadia wrote...
So, like a never ending loop? Okay.. How do I fix it? I don't really understand where the problem is.
You need to show the codes for 'public CardA' and for 'public UpCard'.
Ariadia Ariadia

2015/5/11

#
Okay, I figured out that error, I think. (I was initializing things that didn't need to be, so I changed it.) Here is the current code for CardA:
public class CardA extends UpCard
{
    
    /**
     * Act - do whatever the CardA wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        chooseCard();
        checkAnswer();
    }  
    
    public void chooseCard(){
        int i = Greenfoot.getRandomNumber(11);
        GreenfootImage img = new GreenfootImage("CardA" + i + ".jpg");
        setImage(img);
    }
    
    public void checkAnswer(){
        if (Greenfoot.isKeyDown("A")){
            rightAnswer();
        }
        else if (Greenfoot.isKeyDown("B") || Greenfoot.isKeyDown("C") || Greenfoot.isKeyDown("D"))
        { wrongAnswer();}
    }
}
And to UpCard:
public class UpCard extends Actor
{
    public boolean userTurn;
    /**
     * Act - do whatever the UpCard wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }  
    
    public void addCard(){
        CardA A = new CardA();
        CardB B = new CardB();
        CardC C = new CardC();
        CardD D = new CardD();
        MiniGame M = new MiniGame();
        if (userTurn == true){
            int n = Greenfoot.getRandomNumber(6);
            if (n == 1){
                getWorld().addObject(A, getX(), getY());
                userTurn = false;
                getWorld().removeObject(this);
            }
            else if (n == 2){
                getWorld().addObject(B, getX(), getY());
                userTurn = false;
                getWorld().removeObject(this);
            }
            else if (n == 3){
                getWorld().addObject(C, getX(), getY());
                userTurn = false;
                getWorld().removeObject(this);
            }
            else if (n == 4){
                getWorld().addObject(D, getX(), getY());
                userTurn = false;
                getWorld().removeObject(this);
            }
            else if (n == 1){
                getWorld().addObject(M, getX(), getY());
                userTurn = false;
                getWorld().removeObject(this);
            }
        }
    }
    
    public void rightAnswer(){
        Score score = new Score();
        score.increment();
        nextQuestion();
    }
    
    public void wrongAnswer(){
        Score score = new Score();
        score.decrement();
        nextQuestion();
    }
    
    public void nextQuestion(){
        DownCard down = new DownCard();
        getWorld().addObject(down, getX(), getY());
        userTurn = true;
        getWorld().removeObject(this);
    }
}
^ In the UpCard class, I had all of the "CardA A = new CardA();" stuff at the top. By any chance, could you help with the problem I have with the chooseCard method? How does one get a random number not including 0?
danpost danpost

2015/5/11

#
Ariadia wrote...
How does one get a random number not including 0?
int max = 10;
int rand = 1+Greenfoot.getRandomNumber(max);
The above will give 'rand' a value between one and ten inclusive.
Ariadia Ariadia

2015/5/11

#
Okay, thank you! :)
You need to login to post a reply.