0

Hi I've made two programs: one is a level editor and the other one is a mario clone game.

I did: project structure-> artifacts and i've created a jar for both application.

Now if i run the level editor's jar works without any problem. If i run the game's jar it doesn't start and i get:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1348)
    at com.platform.game.window.BufferedImageLoader.loadImage(BufferedImageLoader.java:15)
    at com.platform.selectlevel.LevelsFinder.<init>(LevelsFinder.java:60)
    at com.platform.game.window.Menu.<init>(Menu.java:72)
    at com.platform.game.window.Window.addMenu(Window.java:55)
    at com.platform.game.window.Window.<init>(Window.java:31)
    at com.platform.game.window.Game.main(Game.java:272)

if i compile inside the editor it works good, how can i fix that ? any tips ?

BufferedImageLoader:

public class BufferedImageLoader {

    private BufferedImage image = null;

    public BufferedImage loadImage(String path){

        try {
            image = ImageIO.read(getClass().getResourceAsStream(path));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        return image;
    }
}

i got the exception only when i run the game from a jar and not from the IDE, just would like to know why? somebody knows that ?

pjs
  • 18,696
  • 4
  • 27
  • 56
Levenlol
  • 305
  • 5
  • 17
  • What's the code for loading your images? Looks like the image can't be found/read for some reason – MadProgrammer Jan 06 '16 at 23:48
  • It's impossible to know what is wrong without seeing some code, but I would try to check the images path or any config/argument that you need to start the app. Also is a good idea to debug the class BufferedImageLoader line 15, to understand which input is been sent as null to the ImageIO. – Alberto Anderick Jr Jan 06 '16 at 23:51
  • `IllegalArgumentException` is not a checked exception (it extends `RuntimeException`), so the fact that it compiles does not mean it will work. You're passing an `InputStream` to `ImageIO.read()` that is `null`, which means you probably forgot to initialize it. – childofsoong Jan 06 '16 at 23:53
  • the fact is that if i run inside the editor everything works fine, if i create and run the jar i got that error, i will put bufferedImageloader code in the question. – Levenlol Jan 07 '16 at 00:15

1 Answers1

-1

As far as I can see the problem is not in the jar export, but in your code:

In your method BufferedImageLoader.loadImage(String path) (in the file BufferedImageLoader.java at line 15) you call ImageIO.read(InputStream input). And you pass it a null object returned by Class.getResourceAsStream(path), and as the docs for ImageIO.read(InputStream input) state:

Throws: IllegalArgumentException - if input is null.

This causes a IllegalArgumentException. I recommend using this code:

public class BufferedImageLoader {

    private BufferedImage image = null;

    public BufferedImage loadImage(String path) {

        try {
            return image = ImageIO.read(BufferedImageLoader.class.getResourceAsStream(path));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

See this post for more info.

Community
  • 1
  • 1
Felix D.
  • 786
  • 1
  • 11
  • 17
  • i got those error only if i run the game for the jar, if i compile and run from the editor everything works fine. – Levenlol Jan 07 '16 at 00:16