Results 1 to 10 of 10
  1. #1
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default How to import sprites into JAR?

    Hello all,

    I created a simple 2D game in Java. Exported it as a Runnable JAR in Eclipse and tried to run it from the command line. Got the following error:

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init><Unknown Source>
    at Paddle.<init><Paddle.java:20>
    at Board.<init><Board.java:48>
    at Starter.<init><Starter.java:31>
    at Starter.main<init><Starter.java:21>

    I have sprites used in the game, stored in the bin folder, inside the project folder created by Eclipse. In Eclipse, the game runs fine.

    I came to the conclusion that the sprites probably didn't extract into the JAR file, so the game couldn't reach them and gave a NullPointerException error.

    If I'm correct, could you explain to me how to make Eclipse import the sprites of the game into the Runnable JAR, so the game will run fine?

    Thanks a lot

    (EDIT: didn't cross-post this time :) )

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: How to import sprites into JAR?

    The simplest thing to do is to include them in the src directory and use them as resources in your code.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default How to import sprites into JAR?

    (For some reason the previous thread was deleted, so I'm opening a new one).

    I made a simple 2D game in Java. It uses sprites, from the bin folder in the project folder that Eclipse created.

    In Eclipse the game works fine. But trying to run the exported Runnable JAR from the command line, gives me NullPointerException errors wherever there's a sprite involved in the code.

    So I tried to import the "sprites" folder into Eclipse as I was advised:

    How to import sprites into JAR?-s.jpg

    When I run it in the command line, still the same errors.

    Any advice? Thanks :)
    Attached Thumbnails Attached Thumbnails How to import sprites into JAR?-s.bmp  

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: How to import sprites into JAR?

    Your previous post was not deleted, it's right here: http://www.java-forums.org/eclipse/8...tml#post366819
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default Re: How to import sprites into JAR?

    Oh my bad. Do you know the reason why the program still won't run?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: How to import sprites into JAR?

    I've merged both threads into one.

    Sprites inside jar files are no longer files, they're resources. I assume you're using the File class, which won't work with resources. You have to import them as resources instead of using the File class.

    If that doesn't fix the problem, I suggest posting an SSCCE demonstrating the very basics of attempting to display a sprite.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default Re: How to import sprites into JAR?

    What I'm doing is this:

    image = new ImageIcon(this.getClass().getResource("sprites/ball.png")).getImage();

    This is how I include sprites in the program. Is this problematic?

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: How to import sprites into JAR?

    Other than telling you that ball.png might not be the same thing as ball.PNG depending on your OS, without an SSCCE it's going to be pretty hard to debug your program.

    The getResource() method returns a URL, from which you can get the full path. Where is it trying to load the resource from?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default Re: How to import sprites into JAR?

    Pretty sure it's trying to load the resource from the sprites folder I imported into Eclipse, aka C:\Documents and Settings\Aviv\workspace\BattlePingPong\bin\sprites .

  10. #10
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default Re: How to import sprites into JAR?

    lol it was the png =\= PNG thing lol :) Thanks a lot

Similar Threads

  1. need help displaying two sprites properly
    By jwl in forum New To Java
    Replies: 3
    Last Post: 08-05-2012, 05:53 AM
  2. background static sprites active
    By leggoeggo in forum Java Gaming
    Replies: 7
    Last Post: 07-10-2012, 05:28 PM
  3. Replies: 1
    Last Post: 02-18-2012, 08:47 PM
  4. Sprites - My Game (Sort of)
    By David M. in forum Java 2D
    Replies: 2
    Last Post: 04-25-2011, 11:00 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •