This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

NullPointer on loading ImageIcon  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I wonder if you can help me. I am trying to load an image (PNG) into an ImageIcon.

But all I get is a NullPointerException wherever I place the image.

My code:


How I call it:


I know the file exists and I have tried it with and without the preceding '/'.

Help would be greatly appreciated as this is currently holding me up.

Thanks


Richard
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try myClass.class.getResource(relativePath/img);

where myClass is the class name in your project and the relative path is the directory in your project you have strored the img
 
Richard Cook
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ya,

Thanks for that, but I'm afraid that didn't work.

What I can't understand is that it is a form created by netbeans and I also have a label with an image which works.

And the code for that is:

Which was generated, but when I copy the code to my section it fails.

Weird!

Richard
 
Jim Akmer
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use getClass(). In the class you are trying to use the code, use the class name e.g of the class you copied (className.class.getResource) that snippet from, and works for that class. Make sure that the directory has the img you want.
 
Message for you sir! I think it is a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!