Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[newbie] Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trouble on the terraces...

1. This call seems not to be accepted by the compiler:


2. When I created another constructor to "avoid" the call using ImageIcon (guess only), I'm getting NullPointer Execptions.
Output:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at homenetwork.bkr.training.ColorAction.actionPerformed(ColorAction.java:30)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at homenetwork.bkr.training.ColorAction.actionPerformed(ColorAction.java:30)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at homenetwork.bkr.training.ColorAction.actionPerformed(ColorAction.java:30)


I'm thinking line 30 is the second, and that leaves buttonPanel as being the main culprit. Are you sure it's not null at the time of execution? You never give it any value, so I'm pretty sure it's always null.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you get an Exception you have got it to compile, and run.

I see Rob has beaten me to it with a suggestion, so look at Rob's suggestion first, and if you have problems again, come back to us.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
at homenetwork.bkr.training.ColorAction.actionPerformed(ColorAction.java:30)


I'm thinking line 30 is the second, and that leaves buttonPanel as being the main culprit. Are you sure it's not null at the time of execution? You never give it any value, so I'm pretty sure it's always null.


Yeah that was it. I copied ColorAction as an inner class of ButtonFrame, however, now:

1. Do I need to carry out some command to change the color of the frame, or is the call within the ColorAction constructor sufficient?
The color values seem to be read correctly (see Output).

2. buttonPanel still retains a value of null, however, this is set within ButtonFrame constructor; spot anything? (see line 46)

The whole thing...


Output (lines 47):
DEBUG create: buttonPanel=javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]


Output (lines 79,80,84):
DEBUG : buttonPanel==null
DEBUG: backgroundColor=java.awt.Color[r=255,g=0,b=0] //red
DEBUG : buttonPanel==null
DEBUG: backgroundColor=java.awt.Color[r=0,g=0,b=255] //blue
DEBUG : buttonPanel==null
DEBUG: backgroundColor=java.awt.Color[r=255,g=255,b=0] //yellow



 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should now be able to work out how many null references you have.

And where do you have a null reference to buttonPanel? By "where", I mean, "which object of which class?"
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: You can simplify your debugging statements, for example from thisto thisThat will still print out null if appropriate.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 2. buttonPanel still retains a value of null, however, this is set within ButtonFrame constructor; spot anything? (see line 46)

you have duplicate declarations
1)
JPanel buttonPanel = new JPanel(); //creates panel (1)
this one is local to the constructor

2)
protected JPanel buttonPanel;
this one is a class field, the one referenced from the actionListener, and is null

to fix
//JPanel buttonPanel = new JPanel(); //creates panel (1)
buttonPanel = new JPanel(); //creates panel (1)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic