Search code examples
javamouseeventmouseclick-event

Is it possible to getX() with getClic()?


i tried this and it causes me a nullpointerexception when i try to access getX(), why ?? b is my GUI : http://pastebin.com/8J4uA1ny (if you need something more tell me in the comments i'll add it, fanks)

MouseEvent m = b.getClic();
xMouse = m.getX();
yMouse = m.getY();
System.out.println("xMouse : "+xMouse+"  | yMouse : "+yMouse);

thanks for help mates


Solution

  • In your code (

        if(this.clics.isEmpty())
            return null;
        else 
            return this.clics.remove(0);
    

    Since you only add something to clics when there actually is a click we can assume it's the first condition the one that's causing your problems.

    Since you are considering the posibility of your array having no events, you should add a condition like:

    MouseEvent m = b.getClic();
    if (m != null) {
        xMouse = m.getX();
        yMouse = m.getY();
        System.out.println("xMouse : "+xMouse+"  | yMouse : "+yMouse);
    }