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

2013/5/9

Null Pointer Exception That Doesn't Make Sense

Entity1037 Entity1037

2013/5/9

#
The title says it all. I'm getting a "Java.Lang.NullPointerException" at line 420 in my "X" class, and at line 12 in my "AlphaStage" class. I don't know what the problem is though; I haven't tried using anything without defining its existence, so I'm at a loss. Could someone point out the problem? //health is measured based on actor positions on the Y coordinate //getWorld().getHeight()/2-96 is the top of the health meter //getWorld().getHeight()/2+82 is the bottom of the health meter //Spacing between health point actors is 6 cells (pixels) //X coordinate for all health related actors for X is 30 //Total health is 30 units int health=getWorld().getHeight()/2-96; <-- This is line 420 int damage=0; int heal=0; public void health() { if (damage!=0){ for (damage=damage; damage>0; damage--){ if (health<=getWorld().getHeight()/2+82){ health+=6; List health1=getWorld().getObjectsAt(30,health,Health1.class); if (! health1.isEmpty()){ Actor Health1 = (Actor) health1.get(0); if (Health1!=null){ getWorld().removeObject(Health1); } } }else{ die=true; } } } if (heal!=0){ for (heal=heal; heal>0; heal--){ if (health>=getWorld().getHeight()/2-96){ health-=6; getWorld().addObject(new Health1(),30,health); } } } //Other things that kill you if (getOneObjectAtOffset(0,0,Block.class)!=null){ die=true; } if (getY()==getWorld().getHeight()-1){ die=true; } if (die==true){ getWorld().removeObject(this); } } public AlphaStage() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); Greenfoot.setSpeed(56); addObject(new TestOrb(),getWidth()/2,getHeight()/2+50); addObject(new X(),getWidth()/2,getHeight()/2); <-- This is line 12 createXHealth(); } Thank you.
Entity1037 Entity1037

2013/5/9

#
P.S.: I'm using my moms laptop. I figured out why it was so slow, which was making it impossible to get almost anything done in programming: she had like 14 toolbars installed. :P
Entity1037 Entity1037

2013/5/9

#
P.S.S.: The error appears when I try to compile, and it doesn't load the scenario either.
danpost danpost

2013/5/9

#
Line 420 errors because you are trying to access a world for an object that has not yet been completely created, and therefore not in a world yet. Just use 'int health = 104'; or, you could use 'int health;' and add the following method which runs automatically when the object is placed in the world (you do not call this method):
public void addedToWorld(World world)
{
    health = world.getHeight()/2-96;
}
I cannot say why line 12 of your AlphaStage class is causing problems with what was given (unless you are referring to this in conjunction with the error on line 420).
Entity1037 Entity1037

2013/5/9

#
Thanks! That fixed both exceptions. One more thing: how do you make the code appear as if it was in an editor when you write a reply to a discussion?
danpost danpost

2013/5/9

#
You are supposed to be able to use the 'code' tag link along the bottom of the 'Post a reply' box; but, it never worked for me. What I do is put a code tag at the beginning of the code and a closing code tag at the end. The one at the end is and the one at the beginning is
danpost danpost

2013/5/9

#
I had to reverse the order to show you. If I had not, it would have come out like this: The one at the beginning is
   and the one at the end is   
. I do not know how long it has been working; but I just tried the 'code' input link below the Post a reply box, and it seems to be ok for me now.
Entity1037 Entity1037

2013/5/9

#
System.out.Println("Thank you!");
Like that?
Entity1037 Entity1037

2013/5/9

#
Nice. : )
You need to login to post a reply.