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

2013/10/30

NullPointerException

Lee1121 Lee1121

2013/10/30

#
Hello guys, I've got a NullPointerException problem I can't seem to solve. Whenever I get close to an enemy I get the error (they are spawned to the right side & upper side of the spawn). Thanks in advance, Lee My project
danpost danpost

2013/10/30

#
You probably will not get much help unless you either upload your scenario on this site or show the code you are using in the discussion thread.
Lee1121 Lee1121

2013/10/30

#
Just done it, This is the new link to the scenario Thanks for the advice danpost
danpost danpost

2013/10/30

#
Line 15 in the Enemy class assigns dungeonWorld a value using 'getWorld()' which has a value of 'null' at the time the assignment takes place. The assignment of the fields take place during the construction of the object and the object is never added into any world until construction is complete. To assign the proper value to that field, you need to make use of the 'addedToWorld' method supplied by the Actor class.
public void addedToWorld(World world)
{
    dungeonWorld = (Dungeon) getWorld();
}
The 'addedToWorld(World)' method is automatically called by the system when the object is added into a world; so, you do not have to call it programmatically.
Lee1121 Lee1121

2013/10/30

#
danpost wrote...
Line 15 in the Enemy class assigns dungeonWorld a value using 'getWorld()' which has a value of 'null' at the time the assignment takes place. The assignment of the fields take place during the construction of the object and the object is never added into any world until construction is complete. To assign the proper value to that field, you need to make use of the 'addedToWorld' method supplied by the Actor class.
public void addedToWorld(World world)
{
    dungeonWorld = (Dungeon) getWorld();
}
The 'addedToWorld(World)' method is automatically called by the system when the object is added into a world; so, you do not have to call it programmatically.
Thank you very much for your time Dan.
You need to login to post a reply.