Can't grabImage() from spreadsheet

7 Views Asked by At

I am getting this error code:

Exception in thread "Thread-2" java.lang.NullPointerException
at com.game.src.main.SpriteSheet.grabImage(SpriteSheet.java:15)
at com.game.src.main.Player.<init>(Player.java:23)
at com.game.src.main.Game.init(Game.java:37)
at com.game.src.main.Game.run(Game.java:83)
at java.lang.Thread.run(Unknown Source)

Here is init() from game

public void init()
{
    handler = new Handler();
    handler.addObject(new Player(200,(370-64),this,1,ObjectId.Player1));
    BufferedImageLoader loader = new BufferedImageLoader();
    try
    {
        spriteSheet = loader.loadImage("/spritesheet.png");
        background = loader.loadImage("/background.gif");
    }catch(IOException e)
    {
        e.printStackTrace();
    }

    addKeyListener(new KeyInput(handler, this));
    this.requestFocus();


}

The constructor for Player:

public Player(float x, float y, Game game, int playerX, ObjectId id) {
    super(x,y,id);
    this.game = game;
    SpriteSheet ss =  new SpriteSheet(game.getSpriteSheet());
    if (playerX == 1)
    {
        player = ss.grabImage(1,1,32,32);
    }
    else player = ss.grabImage(4,1,32,32);
    gravity = 0.2;
    maxDY = 5;
}

and Class SpreadSheet

package com.game.src.main;
import java.awt.image.*;
public class SpriteSheet {
private BufferedImage image;
/**
 * Constructor for objects of class SpriteSheet
 */
public SpriteSheet(BufferedImage image)
{
    this.image = image;
}

public BufferedImage grabImage(int col, int row, int width, int height)
{
    BufferedImage img = image.getSubimage((col*32) - 32, (row*32) - 32, width, height);
    return img;
}
}

And GameObject():

package com.game.src.main;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.LinkedList;

public abstract class GameObject {


protected float x,y;
protected ObjectId id;
protected float velX = 0, velY = 0;

public GameObject(float x, float y, ObjectId id)
{
    this.x = x;
    this.y = y;
    this.id = id;
}   
public abstract void tick(LinkedList<GameObject> object);
public abstract void render(Graphics g);
public abstract Rectangle getBounds();
public float getX()
{
    return x;
}
public  float getY()
{
    return y;
}

public void setX(float x)
{
    this.x =x;
}
public void setY(float y)
{
    this.y = y;
}
public void setVelX(float velX)
{
    this.velX = velX;
}
public void setVelY(float velY)
{
    this.velY = velY;
}

public ObjectId getId()
{
    return id;
}

public void jump(double jumpHeight, boolean canJump)
{
    if (canJump)
        velY -= jumpHeight;
}

}

My project path is:

>1v1
   >src
   >res

This was working before but when I made Player a GameObject it stopped working. Any ideas?

0

There are 0 best solutions below