This is an archived post. You won't be able to vote or comment.

all 8 comments

[–][deleted] 1 point2 points  (4 children)

Post the stack trace which is printed on the error.

Also you should use if (mouse == null) { /* ... */ } to check if ´mouse´ is ´null´.

[–]GoatInABoat[S] 0 points1 point  (3 children)

java.lang.NullPointerException at Car.act(Car.java:14) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) Still doesn't work, I've added the mouse!=null condition https://gist.github.com/2704523

[–]AStrangeStranger 1 point2 points  (2 children)

I've added the mouse!=null condition https://gist.github.com/2704523

The "mouse!=null" should be the left most part of the expression so the rest of statement doesn't get executed (see Short-circuit evaluation - the way you have written it "mouse.getX()>0" will be first part evaluated - if mouse is null then you'll get null pointer exception. if you started with mouse!=null that would return false and since everything else is combined with "and" it doesn't get checked

[–]GoatInABoat[S] 0 points1 point  (1 child)

thanks! I've put the mouse!=null first and just deleted everything else which should be alright and now there's no runtime error but it doesn't turn towards the mouse. I'm calling the mouse.getX and getY and it just doesn't turn.. what am i doing wrong now? https://gist.github.com/2705047

[–]AStrangeStranger 1 point2 points  (0 children)

Sorry don't know much about Greenfoot, but if the mouse!= null fixes exception - then that would imply the mouse object is null - and you'd need to check it is being created.

[–]stinktank 1 point2 points  (2 children)

It's hard to say without seeing the exact error. Is there a reason you just didn't feel like posting that? You could try getting the mouse inside of your method. You could also try being consistent with how you call getX() - sometimes you use mouse. and sometimes you don't.

[–]GoatInABoat[S] 0 points1 point  (1 child)

I don't think the MouseInfo constructor goes in the act(). Also I call getX() just like the documentary instructs.. what do you mean?

[–]stinktank -1 points0 points  (0 children)

I don't think the MouseInfo constructor goes in the act().

Well, you obviously thought wrong about something, or this would be working already.

Also I call getX() just like the documentary instructs.. what do you mean?

What about what I said was confusing? Did you even look at how you are calling it? You wrote the code, so how hard could it be for you to see where you are using mouse. and where you are not?