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

2018/1/25

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space

jxkan jxkan

2018/1/25

#
hi, im trying to change world when i hit an obstacle
public void CollisionToken()
{  
   Actor Token;
   Token = getOneObjectAtOffset(0,0,Token.class);  
      
   if(Token != null)  
   {  
       Greenfoot.setWorld(question); 
     
   }  
}
but when i compile this code green foot stops responding and just comes up with
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "TimerQueue" java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
in the greenfoot terminal window, and then even when i restart greenfoot the project wont respond and i have to go back to a previously saved part of the project, anyway i could get round this issue?, This is the code for the world i am calling if its any help
public class Question extends World
{
    Play play = new Play();
    Menu menu = new Menu();
    Actor question = new Actor() {};
    int lastQuestion = -1;
    int score = 0;
    int random;
   Actor[] answer;
   int[] randomPosition = {-1, -1, -1, -1};
   String[][] questions = //first one=question, second one=correct answer
    {
        {"which base is not found in RNA?", "thymine", "cytosine", "adenine"},
        {"Which of the following bonds are broken \n during DNA replication?", "hydrogen bonds between bases", "phosphodiester bonds", "covalent bonds between bases"},
        {"How many base pairs are there in one \n full turn of the DNA double helix?", "10", "16", "64"},
        {"Analysis of a sample of DNA found that 20 of the bases were adenin.\n What percentage of the bases would be pyrimidines?", "50", "25", "60"},
        {"Which technique was used\n to determine the double-helical \n structure of DNA?", "Dray crystallography", "electrophoresis", "chromatography"},
        {"Analysis of a molecule of DNA found it to contain \n200 adenine bases, 20% of the total number of bases \nin the strand.How many phosphate groups \ndid it contain?",
         "0%", "15%", "80%"},
    };

  
     
    public Question()
    {    
        super(600, 400, 1);
        
        addObject(question, 0, 0);
        createNewQuestion();
        
    }
     
    private void createNewQuestion()
    {
        if (answer != null)//delete previous question
        {
            for (int i=0; i<3; i++) removeObject(answer[i]);
        }
        answer = new Answer[3]; //declaring answer
        random = Greenfoot.getRandomNumber(questions.length);//get random question
        question.setImage(new GreenfootImage(questions[random][0], 18, Color.BLACK, new Color(0,0,0,0))); //displaying random question
        question.setLocation(300,80); //setting location
        
        for (int i=0; i<3; i++)//loop for each answer
        {
            whileLoop: while (true)//infinite loop
            {
                int x = Greenfoot.getRandomNumber(3);
                for (int j=0; j<i; j++)
                {
                    if (randomPosition[j] == x) continue whileLoop; //stop overlapping of answers
                }
                randomPosition[i] = x; //setting position of answer to randomly generated number
                break;
            }
            answer[i] = new Answer(i==0, questions[random][i+1]);
            addObject(answer[i], 200+answer[i].getImage().getWidth()/2, 200+randomPosition[i]*60); 
        }
         
    }  
    private class Answer extends Actor
    {
       
        boolean correct;
       String answer;
        public Answer(boolean correct, String answer)
        {
            this.answer = answer;
            this.correct = correct;
            updateImage();
        }
         
       private void updateImage()
        {
          
          setImage(new GreenfootImage(answer, 20, Color.BLACK, new Color(0, 0, 0, 0)));//setting variable c to a transparent color
        }
         
        public void act()
        {
            if (Greenfoot.mousePressed(this))
            {
                
               
                if(correct)
                {
               Greenfoot.setWorld(play);
            }
            else
            {
               Greenfoot.setWorld(menu); 
            }
             
            
        }
               
    }
}
}
Vercility Vercility

2018/1/25

#
for (int j=0; j<i; j++)
                {
                    if (randomPosition[j] == x) continue whileLoop; //stop overlapping of answers
                }
x is always 0,1 or 2 while your randomPosition array is filled with -1 only You're running an infinite Loop and your memory is full
danpost danpost

2018/1/25

#
I cannot understand why the 'for' loop is inside the 'while' loop to begin with. You are picking a random answer in the 'for' loop. You do not need both a random answer AND a random place to put it. Any order of random answers can be placed into the array consecutively. The thing to get around here is reducing the list of possibilities as they are chosen (and without altering the array). Best might be to put the possible indices in an array and pick them randomly:
int[] indices = { 0, 1, 2 };
for (int i=0; i<indices.lengh; i++)//loop for each index (to answer)
{
    int x = Greenfoot.getRandomNumber(3-i); // random index to an answer
    randomPosition[i] = indices[x]; // place chosen index in position array
    indices[x] = indices[3-i]; // replace position of chosen index in index array with current last one in index array
    answer[i] = new Answer(randomPosition[i]==0, questions[random][randomPosition[i]+1]); // create Answer object
    addObject(answer[i], 200+answer[i].getImage().getWidth()/2, 200+i*60);  // add Answer object into world
}
I think I dd that right (not tested, however).
You need to login to post a reply.