• Post Reply Bookmark Topic Watch Topic
  • New Topic

Just Another Resource problem  RSS feed

 
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy folks,

So here is a simplified version of one of a class I made:
At first I tried without the dot '.' before the slash '/' but I was getting a NullPointerException, it seems to work fine on Eclipse with the '.'

However, when making a JAR file and running it with java -jar file.jar I get the exact same nasty error code as I do when I run it in Eclipse with the '.'s. Removing the dots and making the JAR file doesn't work either. I'm very curious as to why this doesn't work because I've used the exact same method before without any problems (without the dots however).

Error code:
Exception in thread "main" java.lang.ExceptionInInitializerError
at tetris.controller.GUIController.<init>
at tetris.Main.main
Caused by: java.lang.NullPointerException
at javax.swing.ImageIcon.<init>
at tetris.GameConstants.<clinit>
... 2 more


Help on this would greatly be appreciated
 
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Olivier Legat wrote:At first I tried without the dot '.' before the slash '/' but I was getting a NullPointerException, it seems to work fine on Eclipse with the '.'

With a leading "/" Java will look for the files at the root of the current folder / JAR file. The "./" means relative to the class you request the resources with.

However, when making a JAR file and running it with java -jar file.jar I get the exact same nasty error code as I do when I run it in Eclipse with the '.'s. Removing the dots and making the JAR file doesn't work either. I'm very curious as to why this doesn't work because I've used the exact same method before without any problems (without the dots however).

getResource returning null means the resource can't be found. Check if they are in the same folder as your .class files.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Edit: This is similar to what Rob Prime just said, I kind of missed his post though.)
First I will have to AdmitWhenYouAreGuessing.

If you check the full stack trace there might be a FileNotFoundException
hidden. Make sure that you know which directory the . (dot) resolves to.
The stack trace might show you where the program is searching for the
file.

You might want to post the full stack trace.

(Points for simplifying the class, you are half way to IsolateTheProblem. :-))
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:With a leading "/" Java will look for the files at the root of the current folder / JAR file. The "./" means relative to the class you request the resources with.


Indeed, you're right The resource files are in the same folders as the classes that make use of them, hence why I intended to use "./", but in fact it works fine without adding anything at all before the filename. Such as so:
Apparently the method getResource automatically add "package_path"+"/" before the name if there isn't a '/' already. I can't I didn't spot that first

Thank-you all for your time and help.
 
Rob Spoor
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!