質問

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:

getMouseInfo

public static MouseInfo getMouseInfo() Return a mouse info object with information about the state of the mouse.

Returns: The info about the current state of the mouse, or null if the mouse cursor is outside the world boundary (unless being dragged).

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top