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

2018/1/6

java.lang.NullPointerException error

ImAndrew ImAndrew

2018/1/6

#
Okay, I see I'm pretty bad at OOP I don't really know what to with this error. I tried to create a health status with the hero health variable.
 GreenfootImage healthStatus;
 Dungeon dungeon = (Dungeon)getWorld();
 Hero hero = dungeon.getHero(); //Here I get the error in the HealthStatus class
    
HealthStatus healthstatus = new HealthStatus(); // here too in my world class(they are global var)
Hero hero = new Hero();
Super_Hippo Super_Hippo

2018/1/6

#
Are the first three lines in the constructor of the HealthStatus class? If yes, the constructor is executed when it isn't in any world, so 'dungeon' will be null and you can't call a method ('getHero') on null. If not, you need to provide a bit of context.
ImAndrew ImAndrew

2018/1/6

#
The first 3 lines were global in healthstatus class and the other 2 were global variables in myWorld.Anyway, I resolved the problem with a HealthStatus constructor who accepted the hero object whom I put in a variable and used. :D
You need to login to post a reply.