Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 34 of 34

Thread: Why isn't the MouseListener showing the X and Y cords like I want it to?

  1. #26
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    It appears that animator.getMousePosition() is returning null so that's what's causing the exception.

    But what do I do then?

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Strange. I was able to get the earlier code to work without exceptions.

  3. #28
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Yes, the other code that involved the MouseInfo class did work, but I wasn't looking for the mouse position on my computer screen. Just the mouse position on the FountainAnimator object.

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    If you have working code in one program, look at how it works and copy the technique to the other program.

  5. #30
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    OK, if the mouse isn't actually in the component, that call will return null. For some reason, you're not checking the return value, you're just calling a method on it, so you get a NullPointerException when the mouse isn't over the component.

    Leaving aside, for a moment, better designs, you can fix the code you have by simply checking your return value and not using it if it's null:
    ...
    Point pos = animator.getMousePosition();
    if (pos != null) {
        xLocation = pos.getX();
        yLocation = pos.getY();
    }
    ...

  6. #31
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Yeah, but when I put it in the Component, it still says null.

  7. #32
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    I tried the old code, but am not sure how to apply it to the animator object only.

    Right now it will just show the position of the mouse on my screen, even outside any components.

  8. #33
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Ok, this time it's relative to the animator object.

    Thanks.


  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Where/when do you want the mouse location?
    If only over a component, use a mouse listener for that component.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. 3Dmouse mouselistener
    By zimzille in forum Java Theory & Questions
    Replies: 0
    Last Post: April 6th, 2011, 03:12 AM
  2. Help with mouselistener and mousemotionlistener pls..
    By rentaw02 in forum Member Introductions
    Replies: 1
    Last Post: March 18th, 2011, 05:45 PM
  3. MouseListener
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 11:43 AM
  4. [SOLVED] implements MouseListener doesn't work why
    By voltaire in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2010, 04:30 PM
  5. MouseListener & MouseMotionListener
    By JavaLearner in forum AWT / Java Swing
    Replies: 9
    Last Post: March 26th, 2010, 07:18 AM