This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException

 
jwiscou chen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I run follow code have a NullPointerException occured can anyBody help me

Thanks Advance
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I run follow code have a NullPointerException

Edit your post and add a comment to the line of code on which Java told you the exception occurred. Something like:

//**THE ERROR IS HERE***
[ November 06, 2006: Message edited by: sven studde ]
 
Henry Wong
author
Marshal
Pie
Posts: 21399
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stacktrace from a null pointer exception, provides the java filename, the line number within that file, along with the callstack.

If you are having problems deciphering it. Provide us with the trace, and we'll show you what to look at next.

Henry
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if your npe is in valueChanged(), wrap the
label.setIcon(icon);
in a SwingUtilities.invokeLater(...

you will also have to change
ImageIcon icon ..
to
final ImageIcon icon ..
 
jwiscou chen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your advice now I modify to follow can anyone help me

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your problem is that you create the JList before creating the label.
when the JList is created, a valueChanged event is fired, and in this the label,
which is referenced, is still null.

couple of solutions
1) see earlier reply re SwingUtilities
2) create the label before creating the list

label = new JLabel(new ImageIcon(file[0].getPath()));//<----move to here
list = new JList(fileName);
 
jwiscou chen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now I know thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic