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

2017/1/14

NullPointer when referencing value from world in class

nat12 nat12

2017/1/14

#
I created a counter, it adds the score perfectly each time it removes an enemy ship. However, I wanted to make an if statement in the spawning code of the enemy ships (Inside subclass of world), as I had failed to reference the value of 'score' into the world from the 'counter' class, I decided to int the score inside the world subclass, and then reference it inside the 'counter' class. It complied without problems, but before it even starts it throws NullPointerException: "java.lang.NullPointerException at Counter.<init>(Counter.java:12) at GameWorld.<init>(GameWorld.java:13) " The first error which is being referenced in terminal(Line 12 = line 1 in this case):
1
2
3
4
5
6
7
8
9
10
11
12
13
int score = ((GameWorld) getWorld()).score;
/**
 * Act - do whatever the Counter wants to do. This method is called whenever
 * the 'Act' or 'Run' button gets pressed in the environment.
 */
public void act()
{
  setImage(new GreenfootImage("Score : " + score, 24, Color.WHITE, new Color(0, 0, 0, 0)));
}
public void addScore()
{
    score++;
}
Second reference(Line 13 = line 1 in this case):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Counter counter = new Counter();
/**
 * Constructor for objects of class GameWorld.
 *
 */
public GameWorld()
{   
    // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
    super(800, 800, 1);
    prepare();
}
public Counter getCounter()
{
    return counter;
}
And just in case, here is the code that is adding score to counter:
1
2
3
4
5
6
7
8
9
10
11
12
public void checkForEnemy()
 {
 if (getOneIntersectingObject(EnemyShip.class) != null)
 {
     getWorld().removeObject(getOneIntersectingObject(EnemyShip.class));
     GameWorld gameworld = (GameWorld)getWorld();
     Counter counter = gameworld.getCounter();
     counter.addScore();
     getWorld().removeObject(this);
 
 }
}
danpost danpost

2017/1/14

#
In the Counter class, you need to move line 1 (line 12) and insert it at line 8 (line 19). You cannot use any methods that require the actor be in a world during object construction or before it is in a world.
nat12 nat12

2017/1/14

#
Ah I see, thank you. Didn't think of that. I was also presented with the problem that the addScore() method didn't know what score is. How would I in future projects access a variable from within another method (same class)? Currently I fixed it by replacing 'score++;' with '((GameWorld) getWorld()).score++;'. Not sure if this is an actual fix or just a temporary solution, but seems to be working fine now.
danpost danpost

2017/1/14

#
nat12 wrote...
I was also presented with the problem that the addScore() method didn't know what score is. How would I in future projects access a variable from within another method (same class)? Currently I fixed it by replacing 'score++;' with '((GameWorld) getWorld()).score++;'. Not sure if this is an actual fix or just a temporary solution, but seems to be working fine now.
That is a good permanent fix. The 'score' is an instance field is in your GameWorld class which is where the compiler needs to look for it at (in your GameWorld object).
nat12 nat12

2017/1/14

#
Alright, well that's good to hear. Thank you once more.
You need to login to post a reply.