Game won't run, going crazy.

Hey guys, been lurking for awhile but only just made an account.

Maybe it’s cause it’s 1am here and I should go to bed but I’ve been working on my first java game and it was working fine up until now.

It runs as expected in eclipse but when I export it as a runnable .jar it will start the game (load the first option box) then close, i found it to be an error loading one of the image files but what I am confused about is it was working perfectly earlier and still loads the images fine when run in eclipse yet I haven’t changed any code.
The error: https://dl.dropboxusercontent.com/u/148329451/error.png

If someone could take a look at the code or try and export as a .jar and let me know if you can get it working

source: https://dl.dropboxusercontent.com/u/148329451/Shooter_1.rar

Thanks!

Dude I had the same problem and solved in about 2 minutes ago :o. First off, are you using any libraries? (LWJGL, Slick2D, LibGDX, etc…)

input == null means that the file you’re trying to load doesn’t exist basically. The way to fix it is to refresh eclipse project, because eclipse only exports files that are visible in your project menu. So basically if you don’t see the images from your eclipse project viewer, that means you need to refresh the project by Right Click -> Refresh or F5

Trollwarrior, while you may be right, I doubt that’s the issue. The issue here is probably that OP isn’t using the correct IO functions. Creepy, do you load your files using the Java class File? If so, you can’t do that. There are no files in a jar, so you cannot actually create a file out of your images when you export your resources inside of your jar.

I’ll bet money that you are making the same mistake everyone does:

You’re using [icode]new File(“someImageInMyJar.png”)[/icode] somewhere in your code.

You can’t do this if the image file is in a jar file (like the jar file that also contains your code). Why? Because the jar file is actually a zip file, and the image is an entry inside of it, not a real file.

You have to use Class.getResourceAsStream() or ClassLoader.getResourceAsStream(). Google those.

EDIT: sortof ninja’s by opiop. Also, troll, you’re wrong. It was already working in eclipse, besides he exported it (probably) out of the workspace.

@opiop

input == null means the file you specified for image loading doesn’t exist. Simply as that. You can work your way from there.

Indeed. It doesn’t exist because the image is in the jar, not in a file. Simple as that.

Okay let me correct myself. ImageIO.read doesn’t want files. It wants an InputStream from which it can read the data. ImageIO.read(File) is a convenience method, so you don’t have to convert your file into InputStream yourself.

EDIT-
I hope I’m correct :smiley:
EDIT–
You can copy windows console output by doing this:
-> Right Click anywhere on the console
-> Mark
-> Use left mouse button to select what you want to copy
-> Right Click again will copy the information

So I looked at the source, you are using getResourceAsStream, good for you.

However your IMAGE_DIR is incorrect for the project structure. Change it to “/images/” (from “…/images/”) and it works.

ahhh thank you! this solved the problem :slight_smile: