• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java GUI Error | AWT-EventQueue-0

 
vishal khanna
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source Code :

Error Message :
ue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)
[/code]
My Question :
Why this error and how to resolve it?
GUIDesign.jpg
[Thumbnail for GUIDesign.jpg]
 
Paul Clapham
Sheriff
Posts: 21886
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stack trace says the error occurs at line 50. That's this line of code:



A NullPointerException here means that the lblNewLabel variable is null. And looking at your code, I don't see where you ever assign it a value. So to fix the error, assign it a non-null value.

Note that line 56 doesn't do that. It assigns a non-null value to a different lblNewLabel variable, one which is local to your initialize() method. There's no need for that declaration; just use the class-level variable.
 
vishal khanna
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul for your Quick reply.

at line 56. I replaced (JLabel lblNewLabel = new JLabel("New label");) with (lblNewLabel = new JLabel("New label");)

and I got the desired output with no error..

 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Subtle but important difference. What you are doing is changing a declaration‑and‑initialisation to a statement. The declaration makes a local variable which shadows (I think that is the right word) the field.

But I think there may be another solution. Why is that label a field of the class in the first place? Most GUI components should be local variables in the constructor or setUpGUI method and should not be fields. So consider whether you should delete the field.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!