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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    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:
1
2
3
4
5
6
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:
1
2
3
4
5
6
               if (cansee(Avenger.class))
 
    Healthpoints = Healthpoints - 50;
    getHit();
    checkhealth();
}
and checkhealth() is :
1
2
3
4
5
private void checkhealth()
{
    DHealthbar dHealthbar = new DHealthbar();
    dHealthbar.healthcheck();
}
danpost danpost

2011/12/17

#
I simplified your 'healthcheck()' method as follows:
1
2
3
4
5
6
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.