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

2011/11/26

Game Over

weleyk weleyk

2011/11/26

#
I need some help!! I'm making a game where you fire arrows at babies. But if they touch you, a game over screen appears. When I touch a baby, a terminal window appears. it looks like this: java.lang.NullPointerException at BadGuy.defeat(BadGuy.java:86 at BadGuy.act(BadGuy.java:31) at Minions.act(Minions.java:18) at greenfoot.core.Simulation.actActor(Simulation.java:507) if you need more, I'll post the rest. Here's my BadGuy Act method: public void act (int arrows, int score) TargetWorld world = (TargetWorld)getWorld(); if(getOneIntersectingObject(FireArrow.class0 != null) }world.addArrows(arrows); world.addScore(score); world.removeObject(this); return; { move(); checkPlatformFall(); defeat(); } And now the defeat method: Public void defeat() { TargetWorld world = (TargetWorld)getWorld(); if(getOneIntersectingObject(Man.class)) { JOptionPane.showMessageDialog(null, "Game Over!/nScore: " + score.getScore; At the top I put Import javax.swing.JOptionPane; and private Score score; If anybody knows what to do please respond! } }
weleyk weleyk

2011/11/26

#
p.s I don't know what the brakets are doing at the bottom.
Morran Morran

2011/11/26

#
Maybe you shouldn't call world.removeObject(this) until after the end of the method.
delmar delmar

2011/11/26

#
You code is way too full or errors to say much about it. Did you type it up? You should really copy-and-paste the code from your editor to make sure we are really looking at the real thing. The code you are showing here cannot compile. One other hint to see what's going on: In the original error message, look for the first line after java.lang.NullPointerException It says: at BadGuy.defeat(BadGuy.java:86) The "86" in this is a line number. In Greenfoot, go to the Preferences (through the menu), and then the editor tab. There you can switch on display of line numbers. Then open the editor for your "BadGuy" class, and you can see the line numbers. Find line 86 -- that's where it's crashing. Show us the line, and the code around it.
weleyk weleyk

2011/12/1

#
Here is line 85 and 86: 85: if(getOneIntersectingObject(Man.class)!= null) 86:JOptionPane.showMessageDialog(null, "Game Over!/nScore: " + score.getScore()); Tell me if you need anything else
danpost danpost

2011/12/2

#
Between lines 85 and 86, temporarily insert 'Greenfoot.stop();'. Then when it gets to that point and stops, right-click on the world and under 'inherited from World' select 'List getObjects()' and specify 'Score.class' to make sure you score object has been created (and not removed).
davmac davmac

2011/12/2

#
danpost, Greenfoot.stop() stops the simulation but not execution of the current method. That is, the current method continues to its end before the simulation really stops. So, what you have suggested won't quite work. weleyk, from the code you've shown, the only way you'd be getting a NullPointerException is if the "score" variable contains null. Do you ever actually assign something to "score"?
kiarocks kiarocks

2011/12/3

#
how about a breakpoint? I use those a lot.
danpost danpost

2011/12/3

#
Thank you, davmac. And thank you, kiarocks, that is what I was thinking about. My apologies, weleyk,.
You need to login to post a reply.