36redsoxfan
36redsoxfan

Reputation: 250

Java ArrayList Null Pointer

UPDATE I found the solution:D Thanks for the replies but none of them were the real problem. My problem was that I was init'ing the images after I added them to the ArrayList.

So I have a class Named Tile:

public class Tile {
BufferedImage img;
int x,y;

public Tile(BufferedImage image, int x, int y)
{
    this.x = x;
    this.y = y;
    this.img = image;
}

public int getX(){
    return x;
}
public int getY(){
    return y;
}
public BufferedImage getImage(){
    return img;
}
}

Then I have a class that holds the Image Variables called DATA_IMAGES:

public class DATA_IMAGES {
//terrain
public static BufferedImage SNOW;
public static BufferedImage GRASS;
public static BufferedImage CAVE;

//Entities
public static BufferedImage TREE;
public static BufferedImage player;
public static int frame = 1;
public static void initImages(){
    try {
        player = ImageIO.read(new File("res/images/player/f1.png"));
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null,"Can't Load Images "+e.getMessage(),"Fatal Error",JOptionPane.ERROR_MESSAGE);
        System.exit(0);
    }
    try{
    //SNOW = new ImageIcon("res/images/snow.png").getImage();
    //CAVE = new ImageIcon("res/images/snow.png").getImage();
    GRASS = ImageIO.read(new File("res/images/grass.png"));

    TREE = ImageIO.read(new File("res/images/tree.png"));
    }catch(Exception ex){
        JOptionPane.showMessageDialog(null,"Can't Load Images","Fatal Error",JOptionPane.ERROR_MESSAGE);
        System.exit(0);
    }



}
}

So when I was testing this I have a class that is called Main:

public class Main extends JFrame {
private static final long serialVersionUID = 6149918602875295087L;
public static void main(String args[]){
    new Main();
}
TileHelper tiles = new TileHelper(TileHelper.MAIN_LIST);
public Main(){
    TileHelper.MAIN_LIST.add(new Tile(DATA_IMAGES.GRASS,100,100));
    DATA_IMAGES.initImages();
    setSize(640,480);
    setResizable(false);
    setTitle("Stranded");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    setVisible(true);

    createBufferStrategy(2);
    GameLoop();
}
public void GameLoop(){
    Thread gameloop = new Thread(){
        public void run(){
            while(true){
                RenderInit();
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    gameloop.start();
}
public void RenderInit(){
    BufferStrategy buff = getBufferStrategy();
    Graphics g = buff.getDrawGraphics();

    g.clearRect(0, 0, getWidth(), getHeight());
    g.drawString("COOL TEXT", 100, 100);

    Render(g);

    g.dispose();
    buff.show();
    Toolkit.getDefaultToolkit().sync();
}
public void Render(Graphics g){
    tiles.RenderAll(g);
}

}

Then you might of noticed there was a TileHelper class too:

public class TileHelper {
ArrayList<Tile> list;
//Main list for adding tiles
public static ArrayList<Tile> MAIN_LIST = new ArrayList<Tile>();

public TileHelper(ArrayList<Tile> list){
    this.list = list;
}

public void RenderAll(Graphics g){
    for(int i = 0; i < list.size(); i++){
        Tile t = new Tile(list.get(i).getImage(), list.get(i).getX(),list.get(i).getY());
        g.drawImage(list.get(i).getImage(),t.getX(),t.getY(),null);
        System.out.println(t.getX()+"  "+t.getY() + list.get(i).getImage().getHeight());
    }
}
}

When you look closely you see i print out the x value, y value, and image height. I keep getting a null pointer because the BufferedImage isn't being set for some reason?

Question How do I fix this so the BufferedImage is being recognized and I don't get the null pointer.

Here is the Error just incase you need it...

Exception in thread "Thread-3" java.lang.NullPointerException
at com.survivalpixel.stranded.TileHelper.RenderAll(TileHelper.java:19)
at com.survivalpixel.stranded.Main.Render(Main.java:60)
at com.survivalpixel.stranded.Main.RenderInit(Main.java:53)
at com.survivalpixel.stranded.Main$1.run(Main.java:35)

Upvotes: 0

Views: 595

Answers (3)

Alan Stokes
Alan Stokes

Reputation: 18964

You'd probably be better off if initImages was a static initialisation block rather than a function. As it is you need to make sure it gets called before any use of the variables it initialises. Have you checked that is the case?

Upvotes: 0

FThompson
FThompson

Reputation: 28687

DATA_IMAGES.GRASS is null when you add it to the TileHelper.MAIN_LIST.

You simply are calling DATA_IMAGE.initImages() after you add the GRASS image, so the GRASS image is null at the time when it is added.

DATA_IMAGES.initImages();
TileHelper.MAIN_LIST.add(new Tile(DATA_IMAGES.GRASS,100,100));

Upvotes: 3

Mattias Buelens
Mattias Buelens

Reputation: 20159

public Main(){
    TileHelper.MAIN_LIST.add(new Tile(DATA_IMAGES.GRASS,100,100));
    DATA_IMAGES.initImages();
    // ...

You need to swap those two lines around. DATA_IMAGES.GRASS is still null before calling initImages().

(Seriously though, you'd have figured that out easily if you'd just placed a break point at the start of your constructor and stepped through it.)

Upvotes: 5

Related Questions