I am making a game where you shoot down birds. I’ve made an actor that acts as a Crosshair, following the mouse around.
This is the code which is generating the error (setLocation is line 18):
MouseInfo mouse = Greenfoot.getMouseInfo();
setLocation(mouse.getX(), mouse.getY());
And the error code:
java.lang.NullPointerException
at Crosshair.act(Crosshair.java:18)
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)
If you’re having a null pointer exception in the code you wrote then that means that your mouseinfo object is null.
According to the documentation:
Basically this means your mouse if out of boundaries when that method is called.
In order to help you out more please describe what exactly where you hoping to achieve.