I'm trying to change the color of a button when the mouse is hovered over it. I've achieved it by using the mouse info coordinates, but once in a while I get null pointer exceptions. Any suggestions?
Its world:
Start.class:
The exception: With line 16 being: if (Greenfoot.getMouseInfo().getX() >= 350 && Greenfoot.getMouseInfo().getX() <= 550 &&
Once the exception comes up it'll pop up on every run until I comment out the if statement, compile, uncomment, an recompile. Then it'll sometimes work for a little while again.
Also, is it possible to register a mouse drag in a direction? So say if I drag my mouse to the right or left I can execute different things? I was thinking it would involve mouseDragged and mouseDragEnded but wasn't sure how to implement it.
Start start = new Start(); addObject(start, 450, 170);
public class Start extends Buttons { public void act() { setImage(new GreenfootImage("New Game", 50, Color.BLUE, null)); if (Greenfoot.getMouseInfo().getX() >= 350 && Greenfoot.getMouseInfo().getX() <= 550 && Greenfoot.getMouseInfo().getY() >= 155 && Greenfoot.getMouseInfo().getY() <= 185) { setImage(new GreenfootImage("New Game", 50, Color.RED, null)); } if (Greenfoot.mouseClicked(this)) { Greenfoot.setWorld(new SnowWorld()); } } }
java.lang.NullPointerException at Start.act(Start.java:16) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)