Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Strange exception possibly related to MouseListener?  RSS feed

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys/gals,
I'm getting a strange exception for which I can't really trace the cause. I'm try to recursively draw a user-defined number of squares or circles in a JFrame. The trouble occurs when I click the 'Draw' button. The myMouseListener class calls the commence() method in the NumberPanel class, and you see the (strange) stack trace that ensues. Can anyone help me narrow this mystery down? A million thanks in advance!

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at project7.NumberPanel.commence(NumberPanel.java:43)
at project7.NumberPanel$myMouseListener.mouseClicked(NumberPanel.java:54)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
at java.awt.Component.processMouseEvent(Component.java:6044)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3995)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

ColorPanel class:


DrawingPanel class:

Main class:

NumberPanel class:

ShapePanel class:
 
Francois Nadeau
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Patrick,

You are getting a NullPointerException because either the cp (ColorPanel) or the np (NumberPanel) class variables are null on line 43 in your NumberPanel class.

Scanning your code quickly I notice that you never set the np value (which will throw a NullPointerException when you try to call it's functions). You do set a np variable's value at line 50 of the NumberPanel class, but that variable is an internal variable inside your myMouseListener. To fix this you should remove the np variable declaration inside the myMouseListener class (it's ok to do so since it is an internal class).



Or I suppose you could simply remove all np variables all together and call the functions directly.


Cheers.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Je vois que vous avez raison, mais.... what I don't understand is: you'll see that when I add the actionListener, that I send this to the myMouseListener constructor. this should be copied into np, should it not? I don't see why that didn't work. Thank you for the help!

Francois Nadeau wrote:Hi Patrick,

You are getting a NullPointerException because either the cp (ColorPanel) or the np (NumberPanel) class variables are null on line 43 in your NumberPanel class.

Scanning your code quickly I notice that you never set the np value (which will throw a NullPointerException when you try to call it's functions). You do set a np variable's value at line 50 of the NumberPanel class, but that variable is an internal variable inside your myMouseListener. To fix this you should remove the np variable declaration inside the myMouseListener class (it's ok to do so since it is an internal class).




Or I suppose you could simply remove all np variables all together and call the functions directly.


Cheers.
 
Francois Nadeau
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Patrick,

You had two np variables. One was in your NumberPanel class and the other was in your myMouseListener class. You were setting the one in myMouseListener, but trying to use the one in NumberPanel.

Patrick Brooks wrote:
this should be copied into np, should it not?

A quick note on your choice of words when you say "this copied into np". Object assignments in Java are by reference not by value, so you do not copy the value but reference the same object in memory.

Cheers.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aw, silly mistake! Sometimes it takes another eye to see these things. Thanks again!

Francois Nadeau wrote:Hi Patrick,

You had two np variables. One was in your NumberPanel class and the other was in your myMouseListener class. You were setting the one in myMouseListener, but trying to use the one in NumberPanel.

Patrick Brooks wrote:
this should be copied into np, should it not?

A quick note on your choice of words when you say "this copied into np". Object assignments in Java are by reference not by value, so you do not copy the value but reference the same object in memory.

Cheers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!