It appears that animator.getMousePosition() is returning null so that's what's causing the exception.
But what do I do then?
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.
It appears that animator.getMousePosition() is returning null so that's what's causing the exception.
But what do I do then?
Strange. I was able to get the earlier code to work without exceptions.
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.
If you have working code in one program, look at how it works and copy the technique to the other program.
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(); } ...
Yeah, but when I put it in the Component, it still says null.
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.
Ok, this time it's relative to the animator object.
Thanks.
Where/when do you want the mouse location?
If only over a component, use a mouse listener for that component.