4

I have a program in Java using LWJGL. It runs fine in Eclipse, but when I try to compile it as a jar file it crashes, giving me a NullPointerException. It's been asked before, I know, but I don't seem to get an answer that works. Any help here? Thanks in advance!

The thing that seems to be having the problem is the class TextureHelper:

public class TextureHelper {

    public static Texture LoadTexture(String texture)
    {
        try {
            return TextureLoader.getTexture("PNG", ResPlaceholder.class.getResourceAsStream(texture));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}

Some notes:

I've also tried "/res/" + texture, as well as many other things like it.

ResPlaceholder is a class that sits in the res folder where I store all my images. It's a blank empty class.

This works perfectly in Eclipse.

My jar has these folders (just as an example):

foo.jar

----core

--------TextureLoader

----res

-------- Assorted Image Files

-------- ResPlaceholder

This is the same as the packages in eclipse.

Any help you have would be appreciated, I've been stuck on this for days with no progress.

EDIT:

META-INF/MANIFEST.MF

Main.class config/

config/Images.class

core/

core/LevelLoader.class

core/TextureHelper.class

core/TileSet.class

-Skipping some other stuff that has nothing to do with this-

res/

res/ResPlaceholder.class

res/BlankImg.png

res/test.txt

res/testImg.png

res/testTiles.png

Kaia
  • 862
  • 5
  • 21
  • Can you show a small image of your Eclipse package explorer showing precisely where the texture resource is located relative to the class files? Or even better, show us the result of your use of `jar tf myJarFile.jar` – Hovercraft Full Of Eels Jul 17 '13 at 02:17
  • As an experiment, see if you get a valid URL using `getResource(texture)`.. – Andrew Thompson Jul 17 '13 at 02:18
  • Andrew Thompson, I'm confused what I would use a URL for. This isn't an applet, it's just a standard file. Hovercraft Full of Eels, I'll get right on it, just a bit. – Kaia Jul 17 '13 at 02:24
  • @HovercraftFullOfEels, here are the relevant files: http://imgur.com/VGPvNso – Kaia Jul 17 '13 at 02:40
  • @Keon: post the information returned from `jar tf myJar.jar` here please. – Hovercraft Full Of Eels Jul 17 '13 at 02:40
  • META-INF/MANIFEST.MF Main.class config/ config/Images.class core/ core/LevelLoader.class core/TextureHelper.class core/TileSet.class -*Skipping some other stuff that has nothing to do with this*- res/ res/ResPlaceholder.class res/BlankImg.png res/test.txt res/testImg.png res/testTiles.png Edit: Doesn't look like I can space it out. hmm. Would it be easier to do it some other way? I noticed the tag `embedded-resource` was added. Is there an easier way to do this than embedding, and if so, what are they? – Kaia Jul 17 '13 at 02:46
  • Oops, forgot the @HovercraftFullOfEels. :P – Kaia Jul 17 '13 at 02:49
  • @Keon: no please post it as an edit to your question. – Hovercraft Full Of Eels Jul 17 '13 at 02:50

1 Answers1

3

If the path String is something like: /res/testImg.png it should work.

i.e.,

String resourcePath = "/res/testImg.png";
InputStream inStream = ResPlaceholder.class.getResourceAsStream(resourcePath );
BufferedImage img = ImageIO.read(inStream);

// use the img BufferedImage here
// for example:
ImageIcon icon = new ImageIcon(img);
JOptionPane.showMessageDialog(null, icon);

As an aside, it's always good to test problems and check new concepts in small test programs. In your case, I would create a small simple program with just a main method that attempts to extract a jar's image resource and display it in an option pane much like my small code above does. I'd try to avoid long lines and instead separate each step on its own line, again similar to my post above. This way if an exception is thrown, the code on the line will be much more informative as to what is causing the error.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373