probably a dumb question, but I cant figure it out....

J

Justin

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....
 
M

Manish Pandit

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top