Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  NullPointerException when packing game to jar  (Read 1424 times)
0 Members and 1 Guest are viewing this topic.
Offline bauerr

Junior Member




Java!!!!!!!!!!! !!!


« Posted 2006-02-22 16:09:15 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
workspace
|__Game
    |___bin
         |___pics
         |___sound
         |___org
              |___ ....
    |___src
         |___org
              |___ ....


The sound files are loaded as following:

1  
2  
3  
       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?
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #1 - Posted 2006-02-22 16:57:53 »

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.

弾幕 ☆ @mahonnaiseblog
Offline bauerr

Junior Member




Java!!!!!!!!!!! !!!


« Reply #2 - Posted 2006-02-22 17:05:39 »

Deleting the point in "./sounds/..." doesn't solves the problem Then I get a NullPointerException when I run the program in Eclipse.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #3 - Posted 2006-02-22 17:29:50 »

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%28java.lang.String%29

弾幕 ☆ @mahonnaiseblog
Offline bauerr

Junior Member




Java!!!!!!!!!!! !!!


« Reply #4 - Posted 2006-02-23 07:45:54 »

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

Junior Member




Java!!!!!!!!!!! !!!


« Reply #5 - Posted 2006-02-23 07:53:00 »

O.k. I did it like suggested and ran it from jar but I still have the same problem...  Cry
Offline Jeff

JGO Coder




Got any cats?


« Reply #6 - Posted 2006-02-23 08:03:51 »

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.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline bauerr

Junior Member




Java!!!!!!!!!!! !!!


« Reply #7 - Posted 2006-02-23 08:08:44 »

Huh?

How about this:

1  
2  
3  
4  
5  
6  
7  
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.
Offline bauerr

Junior Member




Java!!!!!!!!!!! !!!


« Reply #8 - Posted 2006-02-23 08:11:52 »

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

1  
2  
3  
4  
5  
6  
7  
       URL sound_sub_url  = getURL("./sounds/submarine.wav");
       
       ...

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

Junior Member




Java!!!!!!!!!!! !!!


« Reply #9 - Posted 2006-02-23 12:04:34 »

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

Thank you.  Grin
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #10 - Posted 2006-02-23 20:07:40 »


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.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline bauerr

Junior Member




Java!!!!!!!!!!! !!!


« Reply #11 - Posted 2006-02-23 20:24:18 »

I've found it finally. The method is

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


And it is called like this

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


There was a typing error -sorry.  Sad
Everything runs fine now - I just posted the game in the "Games Showcase"-Forum.  Grin
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (6 views)
2014-07-29 10:59:34

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (8 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!