aspose file tools*
The moose likes Beginning Java and the fly likes [newbie] Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[newbie] Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" Watch "[newbie] Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" New topic
Author

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

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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)




Jon
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Oct 13, 2005
Posts: 39791
    
  28
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

Joined: Oct 13, 2005
Posts: 39791
    
  28
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

Joined: Jun 09, 2003
Posts: 4632
> 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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [newbie] Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException