NullPointerException when packing game to jar

Hello,

I can’t find a solution: I made a little game with Eclipse. Within my workspace I told Eclipse to create two folders src and bin. I manually added two folders pics and sound. Therefore my folder structure is looking like this:


workspace
|__Game
    |___bin
         |___pics
         |___sound
         |___org
              |___ ....
    |___src
         |___org
              |___ ....


The sound files are loaded as following:


       URL sound_sub_url  = getURL("./sounds/submarine.wav");
       ....
       getClass().getClassLoader().getResource(sound_sub_url);

This works fine as long as I’m testing my game in Eclipse. When I create a jar-file from my bin folder I receive a NullPointerException when starting the game from the jar. The NullPointerException happens when my program is trying to load the first sound file.

Is it a problem of my folder structure or should I create the URL a different way?

In the diagram you wrote “sound” instead of “sounds”, but I guess its just a typo.

I would use “/sounds/submarine.wav” and also check if the sounds are inside the jar.

Deleting the point in “./sounds/…” doesn’t solves the problem Then I get a NullPointerException when I run the program in Eclipse.

Thats a a different problem. Have you tried if it works as jar? (It should.)

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResource(java.lang.String)

O.k. I will try it, but I wonder if there is a way to get it working both in Eclipse and jar-file. ???

O.k. I did it like suggested and ran it from jar but I still have the same problem… :’(

um…

That code shoul;dnt even compile

I dont see a version of ClassLoader.getResource() in the docs that takes a URL.

You wan to be getting it by file name, not with a URL.

Huh?

How about this:



getResource

public URL getResource(String name)

    Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

    The name of a resource is a '/'-separated path name that identifies the resource. 


Ooops. I copied my code wrongly: It looks like this:


       URL sound_sub_url  = getURL("./sounds/submarine.wav");
       
       ...

       public URL getURL(String path){       
         getClass().getClassLoader().getResource(sound_sub_url);
       }



:-[ deleting the dots will do the job ( i forgot to delete the first slash, too) :-[

Thank you. ;D

Look at what you quoted, it takes a String.

Your code passes in a URL, not a String.

I assume you still copied it wrong.

I’ve found it finally. The method is


       public URL getURL(String path){       
           getClass().getClassLoader().getResource(path);
       }

And it is called like this


    URL sound_sub_url  = getURL("sounds/submarine.wav");

There was a typing error -sorry. :frowning:
Everything runs fine now - I just posted the game in the “Games Showcase”-Forum. ;D