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

2012/12/21

Null Pointer Exception for what?

Entity1037 Entity1037

2012/12/21

#
import greenfoot.*; public class GameStartButton extends Actor { public void act() { GreenfootImage norm = new GreenfootImage("1-Player.png"); GreenfootImage highlighted = new GreenfootImage("1-PlayerHighlighted.png"); MouseInfo mouse = Greenfoot.getMouseInfo(); if (mouse.getY()>=getY()-19 && mouse.getY()<=getY()+19 && mouse.getX()>=getX()-38 && mouse.getX()<=getX()+38){ setImage(highlighted); }else{ setImage(norm); } if (Greenfoot.mouseClicked(this)){ getWorld().addObject(new StartGame(),0,0); getWorld().removeObject(this); } } } It gives me a null pointer exception at line 10 (setImage(highlighted);) and I don't know why. Can someone help me?
Entity1037 Entity1037

2012/12/21

#
Oops, line ten is the line where it gets the mouse coordinates. Sorry.
miles7191 miles7191

2012/12/21

#
The problem is you are asking for the coordinates and there is no mouse. Before you have mouse.getY()>=getY()-19 add mouse!=null. so like this...
import greenfoot.*;

public class GameStartButton extends Actor
{
    public void act() 
    {
        GreenfootImage norm = new GreenfootImage("1-Player.png");
        GreenfootImage highlighted = new GreenfootImage("1-PlayerHighlighted.png");
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse!=null&&mouse.getY()>=getY()-19 && mouse.getY()<=getY()+19 && mouse.getX()>=getX()-38 && mouse.getX()<=getX()+38){
            setImage(highlighted);
        }else{
            setImage(norm);
        }
        if (Greenfoot.mouseClicked(this)){
            getWorld().addObject(new StartGame(),0,0);
            getWorld().removeObject(this);
        }
    }    
}
Entity1037 Entity1037

2012/12/21

#
Thank You. Actually the problem is so simple, I'm surprised I didn't figure it out! Oh well :/
You need to login to post a reply.