images are loaded while using eclipse but i get NullPointerException in jar
As you can see I have tried various ways of loading images to my program,
Code :
Image iiii = new ImageIcon("src/gra/grafika/ramka.png").getImage();
//Image iiii = new ImageIcon(this.getClass().getResource("grafika/ramka.png")).getImage();
//Image iiii = new ImageIcon(ClassLoader.getSystemResource("gra/grafika/ramka.png")).getImage();
this.setIconImage(iiii);
(I keep every image loaded by the same method so i can easily change the way of doing this.)
The point is when i run it in eclipse there there is everything fine, but after making jar i get NullPointerException in line containing " Image iiii = ..." or just no single image appears (no nullpointerexception)
Question is HOW should i load them because I am a little bit resigned after wasting like 12h (pathetic) of testing ways, changing adresses without any success : |
Re: images are loaded while using eclipse but i get NullPointerException in jar
Are the images packaged into the jar as well (unpackage the jar to check and make sure they are there)?
Re: images are loaded while using eclipse but i get NullPointerException in jar
If the images are in the jar you need to access them as resources not files.
You can use a zip utility to look at the contents of a jar file to verify that the path to the images is correct.
Re: images are loaded while using eclipse but i get NullPointerException in jar
images are in location gra > grafika in jar file
Re: images are loaded while using eclipse but i get NullPointerException in jar
Does the URL returned by getResource() look correct? Add a println() call that prints it.
Re: images are loaded while using eclipse but i get NullPointerException in jar
Quote:
Originally Posted by
Teuthoidea
images are in location gra > grafika in jar file
Based upon the path you mentioned above, you should load the image as a resource, passing the absolute path of the package the images reside in. If the image ranka is in /gra/grafika, then pass that path:
Code :
Image iiii = new ImageIcon(this.getClass().getResource("/gra/grafika/ramka.png")).getImage();
As Norm mentioned, check the paths to the images...preface the path with a '/' to indicate absolute path (relative to the root package within the jar)
Re: images are loaded while using eclipse but i get NullPointerException in jar
System.out.println(this.getClass().getResource("/gra/grafika/ramka.png"));
this line prints null just like there's no such file
also tested line from copeg
and that's the results of running it
null <- println~
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
Re: images are loaded while using eclipse but i get NullPointerException in jar
Is the class in a package? Did you try it with the leading / on the path as copeg suggested?
Check the spelling and the path for correctness.
1 Attachment(s)
Re: images are loaded while using eclipse but i get NullPointerException in jar
hierarchy of packages : Attachment 1275
Re: images are loaded while using eclipse but i get NullPointerException in jar
You need to have the path point to where there is a file. It is not something that can be done remotely on this forum. You need to try many different combinations until you find the one that works.
Re: images are loaded while using eclipse but i get NullPointerException in jar
i'll be glad if you give me an example with path point i've seen it earlier today but cant google it out now
Re: images are loaded while using eclipse but i get NullPointerException in jar
Sorry, what is "path point"? What I was talking about was the paths as you see them in a jar file using a tool like zip file tool.
I see now. I'll reword it: The path should point to where there is a file.
Re: images are loaded while using eclipse but i get NullPointerException in jar
i have seen smth like using " new point () " don't remember arguments of it and whole thing was related to my problem but seemd to be hard at all
Re: images are loaded while using eclipse but i get NullPointerException in jar
Nothing to do with a point class.
I'll reword it: The path should refer to where there is a file.
Re: images are loaded while using eclipse but i get NullPointerException in jar
strange thing - my friend said that it's working while i have tested it at my pc (win7) and then at winxp at virtualbox both failed, moreover he send me his game and i images there were not loading like at my game (i got screen that it's working at his pc)
Re: images are loaded while using eclipse but i get NullPointerException in jar
Are you saying the program in the jar file executes OK on one PC and has problems loading images on another PC?
Re: images are loaded while using eclipse but i get NullPointerException in jar
problem solved - i thought that it'll load images from the inside of jar but it just has to be in proper folder to run :) thanks for help