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

2014/4/12

NullPointer problem

bilzard bilzard

2014/4/12

#
Hi all, I get a NullPointerException error. I can't figure out my problem. The ArrayLists are made in my World (SpelWereld.class).
java.lang.NullPointerException at EindScore.scorebord(EindScore.java:22) at EindScore.<init>(EindScore.java:16) at SpelWereld.gameOver(SpelWereld.java:137) at Verzamelaar.levensPositiefAndersStop(Verzamelaar.java:195) at Verzamelaar.act(Verzamelaar.java:48) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
My code:
public class EindScore extends Actor
{
    
    public EindScore(int score)
    {
        scorebord(score);
    }

    public void scorebord(int score)
    {
       SpelWereld wereld = (SpelWereld) getWorld();
        
       GreenfootImage image = new GreenfootImage(800, 400);

       image.setColor(new Color(255,255,255, 128));
       image.fillRect(0, 0, 800, 400);
       image.setColor(new Color(0, 0, 0, 128));
       image.fillRect(5, 5, 800-10, 400-10);
       Font font = image.getFont();
       font = font.deriveFont(40.0f);
       image.setFont(font);
       image.setColor(Color.WHITE);
       image.drawString("Game Over", 60, 100);
       image.drawString("Score: " + score, 60, 200);
       image.drawString("Opgepakte objecten:", 350, 100);
       image.drawImage(new GreenfootImage("BronzeDollarMunt.png"), 350 ,125);
       image.drawString(" - " + wereld.muntBrons.size(), 400 , 160);
       setImage(image);
    }
}
The error occurs at line 37:
image.drawString(" - " + wereld.muntBrons.size(), 400 , 160);
When I remove wereld.muntBrons.size() I won't receive an error. Any way I can resolve this error? Greetings.
bourne bourne

2014/4/12

#
scorebord gets called from your constructor, which calls getWorld(). So wereld is null. getWorld() will always return null from within an Actor's constructor, because it cannot be inside a World yet, as the Actor is just being constructed.
bilzard bilzard

2014/4/12

#
Hi Bourne, Thanks for your answer. I've changed my code, so the constructor doesn't call my world. I still get the error. This is my code now:
public class EindScore extends Actor
{
    
    SpelWereld wereld;
    
    public EindScore(int score)
    {
        scorebord(score);
    }
    
    public void addedToWorld(World wereld)
    {
        this.wereld = (SpelWereld) getWorld();
    }

    public void scorebord(int score)
    {
        
       GreenfootImage image = new GreenfootImage(800, 400);

       image.setColor(new Color(255,255,255, 128));
       image.fillRect(0, 0, 800, 400);
       image.setColor(new Color(0, 0, 0, 128));
       image.fillRect(5, 5, 800-10, 400-10);
       Font font = image.getFont();
       font = font.deriveFont(40.0f);
       image.setFont(font);
       image.setColor(Color.WHITE);
       image.drawString("Game Over", 60, 100);
       image.drawString("Score: " + score, 60, 200);
       image.drawString("Opgepakte objecten:", 350, 100);
       image.drawImage(new GreenfootImage("BronzeDollarMunt.png"), 350 ,125);
       image.drawString(" - " + wereld.muntBrons.size(), 400 , 160);
       setImage(image);
    }
}
bourne bourne

2014/4/12

#
You are still trying to use "wereld" in the scorebord method, and it has not been assigned yet, since it has not been added to a World yet.
bilzard bilzard

2014/4/12

#
Hi, I now have this. It works for 50%, when I have 0 lives the game needs to stop and see this scorebord at the end. The only thing I see now is the little Greenfoot icon at the center of my screen.
public class EindScore extends Actor
{
    int score;
    
    public EindScore(int score)
    {
        this.score = score;
    }
    
    public void act()
    {
        scorebord(score);
    }
    
    public void scorebord(int score)
    {
       SpelWereld wereld = (SpelWereld) getWorld();
       GreenfootImage image = new GreenfootImage(800, 400);

       image.setColor(new Color(255,255,255, 128));
       image.fillRect(0, 0, 800, 400);
       image.setColor(new Color(0, 0, 0, 128));
       image.fillRect(5, 5, 800-10, 400-10);
       Font font = image.getFont();
       font = font.deriveFont(40.0f);
       image.setFont(font);
       image.setColor(Color.WHITE);
       image.drawString("Game Over", 60, 100);
       image.drawString("Score: " + score, 60, 200);
       image.drawString("Opgepakte objecten:", 350, 100);
       image.drawImage(new GreenfootImage("BronzeDollarMunt.png"), 350 ,125);
       image.drawString(" - " + wereld.muntBrons.size(), 400 , 160);
       setImage(image);
    }
}
In my world class:
    public void gameOver()
    {
        addObject(new EindScore(labelScore.verkrijgPunten()), getWidth()/2, getHeight()/2);
    }
In my collector.class:
   private void levensPositiefAndersStop()
   {
       
       if (wereld.labelLevens.verkrijgLevens() <= 0)
       {
           wereld.gameOver();
           Greenfoot.stop();
       }
   }
What am I doing wrong and is this the right way to the solution to my problem? Greetings. EDIT!!!! : It now works properly! Added these lines to my world:
    public void gameOver()
    {
        EindScore test = new EindScore();
        addObject(test, getWidth()/2, getHeight()/2);
        
        test.scorebord(labelScore.verkrijgPunten());
        
    }
Also I've removed the constructor from EindScore.class.
danpost danpost

2014/4/12

#
You could just pass the muntBrons size in with the score to the constructor. Then the class would simply be:
import greenfoot.*;

public class EindScore extends Actor
{
    public EindScore(int score, int brons)
    {
       GreenfootImage image = new GreenfootImage(800, 400);
       image.setColor(new Color(255, 255, 255, 128));
       image.fillRect(0, 0, 800, 400);
       image.setColor(new Color(0, 0, 0, 128));
       image.fillRect(5, 5, 800-10, 400-10);
       Font font = image.getFont();
       font = font.deriveFont(40.0f);
       image.setFont(font);
       image.setColor(Color.WHITE);
       image.drawString("Game Over", 60, 100);
       image.drawString("Score: " + score, 60, 200);
       image.drawString("Opgepakte objecten:", 350, 100);
       image.drawImage(new GreenfootImage("BronzeDollarMunt.png"), 350 ,125);
       image.drawString(" - " + brons, 400, 160);
       setImage(image);
    }
}
and in your world class, the 'gameOver' method would simply be:
public void gameOver()
{
    addObject(new EindScore(labelScore.verkrijgPunten(), muntBrons.size()), getWidth()/2, getHeight()/2);
}
You need to login to post a reply.