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

2011/12/17

java.lang.NullPointerException Error

DMCGames DMCGames

2011/12/17

#
The Error: java.lang.NullPointerException at Dragonoir.act(Dragonoir.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) java.lang.NullPointerException at Dragonoir.act(Dragonoir.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) Line 24: myDHealthbar.healthcheck(); healthcheck method:
    public void healthcheck()
    {
if(gethealth() < 25)  
{setImage("0.png");  
}else if(gethealth() > 25 && gethealth() <= 75)  
{setImage("50.png");  
}else if(gethealth() > 75 && gethealth() <= 125)  
{setImage("100.png");  
}else if(gethealth() > 125 && gethealth() <= 175)  
{setImage("150.png");  
}else if(gethealth() > 175 && gethealth() <= 225)  
{setImage("200.png");  
}else if(gethealth() > 225 && gethealth() <= 275)  
{setImage("250.png");  
}else if(gethealth() > 275 && gethealth() <= 325)  
{setImage("300.png");  
}else if(gethealth() > 325 && gethealth() <= 375)  
{setImage("350.png");  
}else if(gethealth() > 375 && gethealth() <= 425)  
{setImage("400.png");  
}else if(gethealth() > 425 && gethealth() <= 475)  
{setImage("450.png");  
}else if(gethealth() > 475 && gethealth() <= 525)  
{setImage("500.png");  
}else if(gethealth() > 525 && gethealth() <= 575)  
{setImage("550.png");  
}else if(gethealth() > 575 && gethealth() <= 625)  
{setImage("600.png");  
}else if(gethealth() > 625 && gethealth() <= 675)  
{setImage("650.png");  
}else if(gethealth() > 675 && gethealth() <= 725)  
{setImage("700.png");  
}else if(gethealth() > 725 && gethealth() <= 775)  
{setImage("750.png");  
}else if(gethealth() > 775 && gethealth() <= 825)  
{setImage("800.png");  
}else if(gethealth() > 825 && gethealth() <= 875)  
{setImage("850.png");  
}else if(gethealth() > 875 && gethealth() <= 925)  
{setImage("900.png");  
}else if(gethealth() > 925 && gethealth() <= 975)  
{setImage("950.png");  
}else  
{setImage("1000.png");  
}  
    }
I'm not sure how to resolve it =/
b321234 b321234

2011/12/17

#
Does myDHealthbar refer to any class other than this class? Then I think you should add a code like MyDHealthbar myDHealthbar = new MyDHealthbar(); This is just a guess. I ain't sure if it works...
kiarocks kiarocks

2011/12/17

#
NOTE: do not put the above line where you put your other variables. Instead do this:
MyDHealthbar myDHealthbar; //put this with your other variables

protected void addedToWorld(World world)
{
  myDHealthbar = new MyDHealthbar();
}

davmac davmac

2011/12/17

#
DMCGames: line 24 of "Dragonoir" class must be the act() method, not the healthCheck() method: java.lang.NullPointerException at Dragonoir.act(Dragonoir.java:24) ... it says that the exception is in the act() method of the Dragonoir class (line 24).
DMCGames DMCGames

2011/12/17

#
Thank you for your help I seem to have got it to work but....... The healthbar still won't update it self, I have something like this:
                               if (cansee(Avenger.class))
                 {  
                    Healthpoints = Healthpoints - 50;
                    getHit();
                    checkhealth();
                }
and checkhealth() is :
    private void checkhealth()
    {
        DHealthbar dHealthbar = new DHealthbar();
        dHealthbar.healthcheck();
    }
danpost danpost

2011/12/17

#
I simplified your 'healthcheck()' method as follows:
public void healthcheck()
{
    int healthvalue = gethealth() + 24 - ((gethealth() + 24) % 50);
    String pngFile = "" + healthvalue + ".png";
    setImage(pngFile);
}
Three simple statements; no 'if's; includes 25 as a possible return for gethealth(); allows a health range of (-24, 1025) .
You need to login to post a reply.