• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

NullPointerException

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when I run follow code have a NullPointerException occured can anyBody help me

Thanks Advance
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now I know thank you
 
reply
    Bookmark Topic Watch Topic
  • New Topic