1

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;
    }

}
  • In this method `public void render(Graphics g) { Game game = new Game(); g.drawImage(tex.player, (int)x, (int) y, null); }`. What is the purpose of `game` if you don't use it? Do you have a `tex` object in `GameObject`? Is it `null`? – ROMANIA_engineer Dec 09 '16 at 11:21
  • I forgot to delete Game. I used it to open my background. Background = game.getInGameBackground(); – Mihai Andrei Dec 09 '16 at 11:36
  • [link](https://www.youtube.com/watch?v=J_3Yq-sluR4&list=PLzM5baL2UjtJ10b_Z2fVG39d1CQvHscW7&index=61&spfreload=10) @RO_engineer M-am folosit de clipul acesta, ai putea sa ma ajuti te rog? Pana diseara trebuie sa-l rezolv. (I used this video to do this, can you help me? I have to fix it until this evening) – Mihai Andrei Dec 09 '16 at 11:51
  • The video is too long. But you could start to identify (print/debug) which object is `null` in the following line `g.drawImage(tex.player, (int)x, (int) y, null);`. Is it `g` or `tex`? Then edit your post and add the content of the `GameObject` class. If `tex` is `null`, it is inherited from there. – ROMANIA_engineer Dec 09 '16 at 13:33
  • 'g' can't be, because i can open an image with 'g.drawImage(Game.character, (int)x, (int) y, Game.character.getWidth()/7 ,Game.character.getHeight()/7, null);'. So tex is null... Wait, i'm posting it – Mihai Andrei Dec 09 '16 at 14:11
  • And where do you call the following constructor `MainCharacter(...)`? – ROMANIA_engineer Dec 09 '16 at 15:50
  • Yeah, now i was making the bullet class and noticed that `tex = new GlobalTextures(this);` was initialized after MainCharacter..................... I'm so noob.... Nevermind, ty for your help. – Mihai Andrei Dec 09 '16 at 16:29

0 Answers0