Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NullPointerException, at javax.swing.ImageIcon.<init>(Unknown Source)

Status
Not open for further replies.

mydavor

Programmer
Mar 21, 2004
41
AU
THIS PIECE OF CODE SUDDENLY STARTED FAILING FOR SOME IMAGES, IT USED TO WORK FINE. THE IMAGES ARE AVAILABLE, AND THE SAME STUFF WORKS PERFECTLY WHEN ACCESSED FROM A REMOTE BOX.

AT FIRST I SUSPECTED ON MISSING JAR, SO I CHECKED MY CLASSPATH MANY TIMES, BUT NO, IT FAILS ONLY FOR A FEW IMAGES - YET I COULD NOT SEE ANY DIFFERENCE BETWEEN UNSUCCESSFUL AND SUCCESFUL IMAGES - AGAIN, EVERYTHING WORKED SMOOTHLY TILL YESTERDAY.
private java.awt.Image loadImage(String name){
try {
DPUtility.println("DPDataSelection -- loadImage 1"); // SB
java.net.URL url = getClass().getResource(name);
DPUtility.println("DPDataSelection -- loadImage 2"); // SB
return createImage((java.awt.image.ImageProducer)url.getContent());
}
catch(Exception eLoadImage){
DPUtility.println("DSB :\t ERROR :> DevPro - Image could not be loaded: " + name);
eLoadImage.printStackTrace(); // SB
return null;
}
}



HERE FOLLOWS THE OUTPUT:

DPDataSelection -- loadImage 1
DPDataSelection -- loadImage 2
DSB : ERROR :> DevPro - Image could not be loaded: trying.gif

java.lang.NullPointerException
at devpro.beans.DPDataSelection.loadImage(DPDataSelection.java:286)
at devpro.beans.DPDataSelection.<init>(DPDataSelection.java:226)
at DPRFTApplet.<init>(DPRFTApplet.java:435)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at devpro.beans.DPDataSelection.<init>(DPDataSelection.java:226)
at DPRFTApplet.<init>(DPRFTApplet.java:435)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)



 
NOTE THAT ALL THE IMAGES ARE IN classes/images and WEB-INF/images, THEY ARE also in a .jar file.
should they be in the jar file ?
 
PLEASE DON'T SHOUT !!!

I would suspect that the "name" or path of the resource you are try to locate is not correct.

Consider this code

Code:
public class Test {
 	public static void main(String args[]) throws Exception {
		System.err.println(new Test().getClass().getResource("Test.java"));
	}
}

Then jar up your class and Test.java into a jar file :

Code:
jar -cvf test.jar Test.class Test.java

and set your classpath to point only at the jar file :

Code:
set CLASSPATH=C:\java\test.jar

and then execute the class, you will see this :

Code:
C:\java>java Test
jar:file:/C:/java/test.jar!/Test.java

Now make a new directory, move the Test.java to it, and rejar :

Code:
C:\java>mkdir tmp

C:\java>xcopy Test.java tmp
C:Test.java
1 File(s) copied

C:\java>jar -cvf test.jar Test.class tmp/Test.java
added manifest
adding: Test.class(in = 590) (out= 378)(deflated 35%)
adding: tmp/Test.java(in = 26586) (out= 6754)(deflated 74%)

and when you run the class again, you will get this :

Code:
C:\java>java Test
null

So from this, you can see that paths are very important when finding resources relative to the code's execution directory.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top