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

2011/11/26

nullpointerexception help

ardwennem2 ardwennem2

2011/11/26

#
Hi, I need help on solving a Nullpointer exception: This problem has to do with three classes: Sheep class: method() { world01 world = (world01)getWorld(); rock rock = world.getrock(); rock.lostlife(); } world01 class: public world01(Sheep sheep) // left the sheeps away... (not really, just here) { rock rock = new rock(); addObject(rock, 345, 300); rock = new rock(); addObject(rock, 242, 225); } private rock rock; public rock getrock() { return rock; } rock class: public void lostlife() { getWorld().removeObject(this); } I can compile this section, but if I acces the method(), I get a nullpointerexception... help please?
Morran Morran

2011/11/26

#
Does the problem start when rock calls lostlife()? Or does it come before then? Do you actually give the world( world01 ) a sheep when it starts? Try this: In the sheep class, add a test:
method()
{
world01 world = (world01)getWorld();
rock rock = world.getrock();
if(rock != null) {
rock.lostlife();
}
}
It looks like you may have been trying to access the rock after it was deleted.
ardwennem2 ardwennem2

2011/11/26

#
that sounds pretty logic... and I don't get an error anymore, yet the object still doesn't go away... to me this seems as if I can't acces the rock...
Morran Morran

2011/11/26

#
Why are you using world01.getRock()? Could you use getOneIntersectingObject(rock.class) instead?
ardwennem2 ardwennem2

2011/11/26

#
Because I want the rocks to go away if I lose a life, and they are supposed to appear again (immediatly) at their starting position. The rocks are moveble and I used this so they wouldn't be at different places after I moved one of them and lost a life. In other words... (me thinking...) maybe it would work as well if I simply moved the rock... but wouldn't that be more difficult to do? (especially since I have more than one rock...)
Morran Morran

2011/11/26

#
Just do: rock tempRock = (rock) getOneIntersectingObject(rock.class); if(tempRock != null) { getWorld().removeObject(tempRock); getWorld().addObject(new rock(), startX, startY); } startX and startY would be whatever you want the starting position to be. The rock would get removed, and a new rock would immediately replace it at the specified location.
ardwennem2 ardwennem2

2011/11/26

#
yet wouldn't this only work if my sheep intersected with the rock?
ardwennem2 ardwennem2

2011/11/26

#
If I do do that, I get the same error...
danpost danpost

2011/11/26

#
You could create the rock with 'new Rock(int x, int y)' and have instance variables for startX and startY in Rock class that are set to x and y. Then when you need to move the rock back to its starting position the startX and startY will determine where to move (without removing) the rock.
ardwennem2 ardwennem2

2011/11/26

#
but the removing wasn't the problem, the problem was the accessing of the rock...
danpost danpost

2011/11/26

#
It appears that you have a reference for one rock in world01 and it is being set to the second rock created. You may want to change that variable to an array 'private rock rock = { null, null };' and as you create the rocks, set the first one to rock and the second one to rock. Then change the method from 'getRock()' to 'getRock(int whichOne)' and return rock. As a side note, you could change 'private rock rock' (or private rock rock = { null, null };) to 'public' and remove the getRock() method altogether. Anyway, it probably would be best to have lostLive in world01 (not Rock), since all rock must be set back to original locations (use a 'for' loop (1) to relocate, or (2) to grab startX and startY, remove and create anew.
ardwennem2 ardwennem2

2011/11/26

#
so basicly this should work, right?: boolean stop = false; while(stop == false) { for(int i=0;i<4;i++) { if (rock == null) { rock= new rock(); addObject(rock,50,50); rock.setLocation(100,100); stop = true; } else { rock.setLocation(100,100); } } } (just for some reason, which is probably not too difficult, but I haven't found it yet... it ignores the while..., which I've found out... now to solve it... too easy...i=5... xD) Anyway... I have at least acces to every rock now, so Thanks, you helped me a lot.
danpost danpost

2011/11/26

#
It is not getting to the next loop of the while until the for loop completes with all four rocks. What the code appears to be telling me is 'If any rocks are removed, then replace them and set stop to true, else make sure they stay on top of each other at (100, 100)'. I doubt that is what you are trying to make it do; if I knew exactly what you were attempting, I might be able to help.
ardwennem2 ardwennem2

2011/11/27

#
I already figured it out, but thanx anyway, couldn't have done it without you
You need to login to post a reply.