0

So I am trying to make a simple graphical interface for a game, So I made a sprite sheet to go along with it. But in my class <SpriteSheet.java> I am coming accross this error:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source) at matrix.game.gfx.SpriteSheet.<init>(SpriteSheet.java:18)

Here is <SpriteSheet.java>

package matrix.game.gfx;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {
    public String path;
    public int width;
    public int height;

    public int[] pixels;

    public SpriteSheet(String path) {
        BufferedImage image = null;
        try {
            image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(image == null) {
            return;
        }

        this.path = path;
        this.width = image.getWidth();
        this.height = image.getHeight();

        pixels = image.getRGB(0,0, width, height, null, 0, width);
        for (int i = 0; i < pixels.length; i++) {
            pixels[i] = (pixels[i] & 0xff)/64;
        }
        for (int i = 0; i < 8; i++) {
            System.out.println(pixels[i]);
        }
    }
}

3 Answers3

0

You are trying to execute function not declared in your class:

image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));

Because of that it fails try { and throws the exception it cannot handle.

0

As you can see in the code below, the method read of ImageIO class will throw an IllegalArgumentException when the parameter input is null.

public static BufferedImage read(InputStream input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input == null!");
    }

    ImageInputStream stream = createImageInputStream(input);
    BufferedImage bi = read(stream);
    if (bi == null) {
        stream.close();
    }
    return bi;
}

This following line is trying to find the resource inside the package matrix.game.gfx.

SpriteSheet.class.getResourceAsStream(path)

If you are trying to access a file from a directory other than the class package, you should change the code as follows:

SpriteSheet.class.getClassLoader().getResourceAsStream(fullPath);

In the code above the classloader will starts the search in the root of the classpath.

0

This worked for me.

Hope it does for you too.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {
public String path;
public int width;
public int height;

public int[] pixels;

public SpriteSheet(String path) {
    BufferedImage image = null;

    //Adding a directory(FILE) object for the path specified
    File dir = new File(path);

    try {
        //Reading from the specifies directory
        image = ImageIO.read(dir);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if(image == null) {
        return;
    }

    this.path = path;
    this.width = image.getWidth();
    this.height = image.getHeight();

    pixels = image.getRGB(0,0, width, height, null, 0, width);
    for (int i = 0; i < pixels.length; i++) {
        pixels[i] = (pixels[i] & 0xff)/64;
    }
    for (int i = 0; i < 8; i++) {
        System.out.println(pixels[i]);
    }
}
public static void main(String[] args) {
    new SpriteSheet(path/to/file);


}
}
Ayman Patel
  • 564
  • 2
  • 8
  • 23