I tried to read and show a sprite, but all what I got is this error:
Exception in thread "Thread-2" java.lang.NullPointerException
at Game.MainCharacter.render(MainCharacter.java:44)
at Game.Game.render(Game.java:105)
at Game.Game.run(Game.java:149)
at java.lang.Thread.run(Unknown Source)
The sprite was opened correctly (I checked it), but I don't know where I forgot something...
Here are the classes that were used (I wrote just the code that were used): GlobalTextures Class - contains all my spritesheeets
package Game;
import java.awt.image.BufferedImage;
public class GlobalTextures {
public BufferedImage player;
private SpriteSheet character;
public GlobalTextures(Game game) {
character = new SpriteSheet(game.getCharacterSpriteSheet());
getTextures();
}
private void getTextures() {
player = character.getSprite(1, 1, 333, 472);
if(player == null) System.out.println("It doesn't exist");
}
}
Player Class - there I open the character and I'm 90% sure that there's the error, but I don't know why...
package Game;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class MainCharacter extends GameObject implements Entity{
public MainCharacter(double x, double y, GlobalTextures tex) {
super(x, y, tex);
}
public void render(Graphics g) {
Game game = new Game();
g.drawImage(tex.player, (int)x, (int) y, null);
}
}
Sprite class - here I get the sprite and return the character
package Game;
import java.awt.image.BufferedImage;
public class SpriteSheet {
private BufferedImage image;
public SpriteSheet(BufferedImage image)
{
this.image = image;
}
public BufferedImage getSprite(int col, int row, int width, int height)
{
BufferedImage img = image.getSubimage((col * 2000) - 2000, (row * 2829) - 2829, width, height);
return img;
}
}
////////////////////////////////////////////////////////////////////////////////
package Game;
public class GameObject {
protected double x;
protected double y;
protected GlobalTextures tex;
public GameObject(double x, double y, GlobalTextures tex){
this.x = x;
this.y = y;
this.tex = tex;
}
}