Hello

Im having some new Java problems, while trying to export my project to a JAR file in Eclipse. When I run it from the IDE its OK, all roses and bourbon, but I get hit over the head this when I run it from cmd :

C:\Users\Joey\Desktop>java -jar FLIPMEMATEY.jar
Exception in thread "main" java.lang.NullPointerException
        at henry.eats.cake(Hello.java:84)
..print stack

In line 84, I need to read some pics in a List that are located in "src\\PICS". By carefully examining the newly created JAR file Ive noticed that the folder PICS is now in root of the JAR.
Could that be the problem :?
Some directions please?

Thank you very much
Joey

Recommended Answers

All 10 Replies

What code causes the NullPointerException at line 84?

You should include all you image files in the jar file so you don't need to worry about reading them from the disk.

What code causes the NullPointerException at line 84?

You should include all you image files in the jar file so you don't need to worry about reading them from the disk.

Yes, the all the files are included in the JAR. Now when I create a new folder called src and place the PICS folder inside and then copy it to the JAR, it works!
Im using a absolute path "src\\PICS" in my project.

Have you fixed the problem now?

Have you fixed the problem now?

No, because the files need to be in the same folder as my JAR.. Only in that case it works. Ive tried to use this

URL url = this.getClass().getClassLoader().getResource(path); 
ImageIcon icon = new ImageIcon(url);

But to no avail..
It just cant read my picture files inside the JAR.

because the files need to be in the same folder as my JAR

No, the files need to be IN THE JAR.

No, the files need to be IN THE JAR.

Yes, the files are in the JAR all along.

FLIPMEMATEY.jar ROOT:

prog
PICS
META-INF
.classpath
.project
manifest.MF

Doesnt work, nullpointer ex [works fine inside the IDE]
But when I do this:
Create new folder, place the jar inside along with the PICS folder

SOMENEWFOLDER root:

FLIPMEMATEY.jar
PICS

It works. Now why is that?
Does it have something to do with the way Ive declared the FILE path inside the project:

FILE read = new FILE("src\\PICS\\")

The code in line 84 that makes the exception is a call to a function that returns the List object with the pictures inside the PICS folder.

If you're using Windows then it's possibe to get this problem because Windows file names are not case-sensitive, but files within a jar are case-sensitive. So if the file name in you code differs in case from the real file name it will work in Windows, but not in a jar.

commented: Gr8 guy! +3

If you're using Windows then it's possibe to get this problem because Windows file names are not case-sensitive, but files within a jar are case-sensitive. So if the file name in you code differs in case from the real file name it will work in Windows, but not in a jar.

Sir James man, you are the boss.:)
Great hint, works like a charm now. ::respect ::

Kind words, thank you.

FILE read = new FILE("src\\PICS\\")

This usage requires that the files be on the disk.
You should change how you access the image files to treat them as resources which will allow you to put them into the jar file and read them from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.