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

Discussion in 'Java' started by Justin, Oct 5, 2006.

  1. Justin

    Justin Guest

    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....
     
    Justin, Oct 5, 2006
    #1
    1. Advertising

  2. 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
     
    Manish Pandit, Oct 5, 2006
    #2
    1. Advertising

  3. Justin

    Justin Guest

    works like a charm. thank you very much


    Manish Pandit wrote:
    > 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, Oct 9, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Healey

    Probably a dumb s/// question.

    Mark Healey, Mar 16, 2005, in forum: Perl
    Replies:
    2
    Views:
    516
    Glenn Jackman
    Mar 16, 2005
  2. Nagaraj
    Replies:
    1
    Views:
    870
    Lionel B
    Mar 1, 2007
  3. Tom Anderson

    Probably dumb JAR question

    Tom Anderson, Jan 25, 2009, in forum: Java
    Replies:
    6
    Views:
    405
    Roedy Green
    Jan 27, 2009
  4. Jerry C.
    Replies:
    8
    Views:
    244
    Uri Guttman
    Nov 23, 2003
  5. Mark Healey

    Probably a dumb s/// question.

    Mark Healey, Mar 16, 2005, in forum: Perl Misc
    Replies:
    13
    Views:
    199
    Joe Smith
    Mar 28, 2005
Loading...

Share This Page