I have implemented a mouse listener in a class that extends a JDialog.
I added a JLabel that contains a graphic. When the user clicks on the
JLabel, the mouse clicked method should trigger a method that does
something with the coordinates. However, when I do click, I get a null
pointer exception. It goes to the line where i assign the variable
int xCoor = getMousePosition().x;
however, if i make xCoor a point, that is fine. However again, if I
were try to copy either the x-coordinate or the y coordinate into an
int variable, I will get a null pointer. Myself, being a hella noob,
cannot figure this out.
Thanks for any help....
Manish Pandit - 05 Oct 2006 23:44 GMT
getMousePosition() should not be in your listener. In the listener, you
need to do a event.getPoint() which will be relative to your source
component. You can use translatePoint() to translate it based on any
other component.
getMousePosition() will return null if the mouse is clicked outside the
component in which you are calling getMousePosition() - I believe that
is the reason for your NPE.
If this doesnt help, do paste your listener code.
-cheers,
Manish
Justin - 09 Oct 2006 01:40 GMT
works like a charm. thank you very much
> getMousePosition() should not be in your listener. In the listener, you
> need to do a event.getPoint() which will be relative to your source
[quoted text clipped - 9 lines]
> -cheers,
> Manish