0

I've been trying to use a sprite sheet that i found online, and use some of the images from it for a small game that i am making. I have looking at tutorials about how to read from a sprite sheet, but when im trying to load the Sprite sheet, the compiler doesnt seem to be able to load the file, which is in a resources package.

This is the error i get:

java.lang.IllegalArgumentException: input == null!
Image load failed

at javax.imageio.ImageIO.read(ImageIO.java:1388)
at data.BufferedImageLoader.loadImage(BufferedImageLoader.java:17)
at data.Boot.spriteSheetLoader(Boot.java:113)
at data.Boot.<init>(Boot.java:89)
at data.Boot.main(Boot.java:132)
Exception in thread "main" java.lang.NullPointerException
at data.SpriteSheet.grabSprite(SpriteSheet.java:23)
at data.Boot.spriteSheetLoader(Boot.java:122)
at data.Boot.<init>(Boot.java:89)
at data.Boot.main(Boot.java:132)

This is the Image i am using: http://imgur.com/a/D5A1N

Main Class

package data;

import org.lwjgl.opengl.Display;
import org.newdawn.slick.opengl.Texture;

import helpers.Artist.*;

import static helpers.Artist.*;

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

import javax.swing.JFrame;


public class Boot extends JFrame {

BufferedImage image;

public Boot(){


    beginSession();



    //4, in order for this to work, we use a nested if statement(Tile Grid class)
    int [][] map = {
        {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {2, 2, 2, 2, 2, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
        {2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
    };


    //1can either load the textures like this or*
    /*Texture tex = LoadTexture("resources/Grass32.png", "PNG");
    Texture tex0 = LoadTexture("resources/Dirt32.png", "PNG");*/

    //2like this, method (QuickLoad) is from Artist
    /*Texture tex = QuickLoad("Grass32");
    Texture tex1 = QuickLoad("Dirt32");*/

    //3like this, this is from the Tile constructor/Tile type class
    /*Tile tile = new Tile(100, 100, 32, 32, TileType.Grass);
    Tile tile1 = new Tile(132, 100, 32, 32, TileType.Grass);*/

    //4to fill the screen up with the same tiles
    TileGrid grid = new TileGrid(map);
    //sets the tile, in order to do that, we say where we want to set it,            then we have to grab a tile from the grid, with the coordinates and type
    grid.setTile(0, 1, grid.GetTile(0, 0).getType());

    //loading the enemy
    Enemy e = new Enemy(QuickLoad("RedTank32"), grid.GetTile(9, 13), 32, 32, 2);



    while(!Display.isCloseRequested()){

        /*1/2 drawQuadTex(tex, 0, 0, 32, 32);
        drawQuadTex(tex1, 32, 0, 32, 32);*/

        //3/1
        /*drawQuadTex(tile.getTexture(), tile.getX(), tile.getY(), tile.getWidth(), tile.getHeight());
        drawQuadTex(tile1.getTexture(), tile1.getX(), tile1.getY(), tile1.getWidth(), tile1.getHeight());*/

        //3/2instead of the above, we can pass in the arguments into a method in the tile class, and call that method here
        /*tile.Draw();
        tile1.Draw();*/

        //4
        grid.Draw();
        e.Draw();
        spriteSheetLoader();

        Display.update();
        Display.sync(60);
    }
    Display.destroy();
}
//this is where i load the sprite sheet
public void spriteSheetLoader(){

    //this is for sprite sheet
    /*BufferStrategy bs = this.getBufferStrategy();
    if(bs == null){
        createBufferStrategy(3);
    return;
    }
    Graphics g = bs.getDrawGraphics();
    g.drawImage(image, 0, 0, getWidth(), getHeight(), this);

    g.drawImage(grass, 150, 150, this);*/


    BufferedImageLoader Loader = new BufferedImageLoader();
    BufferedImage spriteSheet = null;
    try {
    //It seems to have a problem loading this
         spriteSheet = Loader.loadImage("SpriteSheet.png");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Image load failed");

    }

    SpriteSheet ss = new SpriteSheet(spriteSheet);

    image = ss.grabSprite(0, 0, 32, 32);

}
@Override
public void paint(Graphics g){
    g.drawImage(image, 160, 160, null);
    repaint();
}

public static void main(String[] args) {
    new Boot();


}

}

Artist Class

public class Artist extends Canvas{


private static final long serialVersionUID = 1L;

public static final int WIDTH = 640, HEIGHT = 480;


public static void beginSession(){

    Display.setTitle("Game");
    try {
        Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
        Display.create();
    } catch (LWJGLException e) {

        e.printStackTrace();
    }

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);//setting up camera
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);

}
public static void DrawQuad(float x, float y, float width, float height){
    glBegin(GL_QUADS);
    glVertex2f(x, y);//top left corner
    glVertex2f(x + width, y);//top right corner
    glVertex2f(x + width, y + height);//bottom right corner
    glVertex2f(x, y + height);//bottom left corner
}

public static void drawQuadTex(Texture texture, float x, float y, float width, float height){
    texture.bind();
    glTranslatef(x, y, 0);
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex2f(0, 0);
    glTexCoord2f(1, 0);
    glVertex2f(width, 0);
    glTexCoord2f(1, 1);
    glVertex2f(width, height);
    glTexCoord2f(0, 1);
    glVertex2f(0, height);
    glEnd();
    glLoadIdentity();

}
//this is to load a single image
public static Texture LoadTexture(String path, String fileType){

    Texture tex = null;
    InputStream in = ResourceLoader.getResourceAsStream(path);
    try {
        tex = TextureLoader.getTexture(fileType, in);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return tex;

}
//this is to load a single image
public static Texture QuickLoad(String name){


    Texture tex  =  null;
    tex =  LoadTexture("resources/" + name + ".png", "PNG");
    return tex;

}

Sprite sheet class

public class SpriteSheet {

public BufferedImage spriteSheet;

public BufferedImage Grass;
public BufferedImage Dirt;


public SpriteSheet(BufferedImage ss) {

  this.spriteSheet = ss;


}


public BufferedImage grabSprite(int x, int y, int width, int height) {

  BufferedImage sprite = spriteSheet.getSubimage(x, y, width, height);


  return sprite;

}

}

Bufferd Image class

public class BufferedImageLoader {

private BufferedImage image;

public BufferedImage loadImage(String path){

    URL url = this.getClass().getResource(path);
    try {
        image = ImageIO.read(url);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return image;
}
}

I do have other classes but its mostly to load single images, rather than from a sprite sheet.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
pdhk14
  • 43
  • 2
  • 8
  • Lots of issues in that code include mixing AWT (Canvas) with Swing components (everything else), drawing directly on a JFrame, drawing in a paint method, calling repaint within a paint method,... it looks like you're doing a lot of guessing, and that's not going to work well for you. As for your primary problem, you need to post the stack trace as well as indicate which line throws the exception. – Hovercraft Full Of Eels Sep 25 '16 at 21:59
  • Where is your `SpriteSheet.png` file in relation to your java/class files? Your code is assuming that it's in the same directory/resource path. – Hovercraft Full Of Eels Sep 25 '16 at 22:01
  • Also when solving a problem like this, it's best to test and solve the main problem in isolation of your main program. Your current problem is one of loading a resource, and that's it, and so 90% or more the other code above is completely irrelevant to this issue, and only serves to distract us and you. Best to create a small program where you simply load and display the resource and nothing more, a [mcve]. Then if you get stuck, you can post this code, along with more information on file location information, and it will be easier to solve. – Hovercraft Full Of Eels Sep 25 '16 at 22:03
  • The sprite sheet im using is in the src/resources package. – pdhk14 Sep 25 '16 at 22:03
  • Please tell us **exactly** where. Show us as an image if possible. If you move the sheet into your main class file package, what happens? – Hovercraft Full Of Eels Sep 25 '16 at 22:04
  • The line that throws the exception is the try/catch in my Boot class in the spriteSheetLoader method. – pdhk14 Sep 25 '16 at 22:07
  • Please read [this question/answer](http://stackoverflow.com/questions/6608795/what-is-the-difference-between-class-getresource-and-classloader-getresource/6608848#6608848). – Hovercraft Full Of Eels Sep 25 '16 at 22:10
  • This is a screen shot of the directory: http://imgur.com/a/53IRf – pdhk14 Sep 25 '16 at 22:11
  • Yeah, this has nothing to do with images, with Swing, with graphics and all with how to use resources, and how to load them, and where to find them. Please check out the many similar questions and answers that can be found: [google search link](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+loading+resources). – Hovercraft Full Of Eels Sep 25 '16 at 22:12
  • Thanks you very much. Apologies for the incorrect formatting, in this post. So i take it that the way i thought it loaded resources and the way it actually loads them were completely wrong? – pdhk14 Sep 25 '16 at 22:19
  • Yes, that's the problem in a nutshell. – Hovercraft Full Of Eels Sep 25 '16 at 22:19
  • But more importantly, in the future you will want to post less code. In fact when I run into similar problems, I stop what I'm doing and try to create a very simple and small program, one simply to reproduce the problem in as naked a situation as possible. By doing this I often see the issues causing the problem and can solve it, but failing that, I know that I can post a decent question here, one that shows only code relevant to the problem. – Hovercraft Full Of Eels Sep 25 '16 at 22:21

0 Answers0