Problem with NullPointerException

0

I have a class like this:

public class TileRender {

    private Map mapa;
    private TileMap tileMap;
    private Tile[] tiles;
    private List<ObjectPointMap> objMap;

    public TileRender(TileMap tileMap, Map mapa) {
        this.mapa = mapa;
        this.tileMap = tileMap;
        this.objMap = new ArrayList<>();

        this.carregarObj();
    }

    public void carregarObj() {

        for(int i=0; i<tileMap.getHeight(); i ++) {
            for(int j=0; j<tileMap.getWidth(); j++) {
                objMap.add(new ObjectPointMap(j * 16, i * 16));
            }
        }
        this.carregarTiles();

    }

    public void carregarTiles() {

        tiles = new Tile[objMap.size()];
        for(int i=0; i<objMap.size(); i++) { 
            BufferedImage image = mapa.getTile((int) (objMap.get(i).getY() / 16), (int) (objMap.get(i).getX() / 16));
            tiles[i] = new Tile(image);
            tiles[i].setPosition(objMap.get(i).getX(), objMap.get(i).getY());
        }
    }

    public void draw(Graphics2D g2d) {

        for(int i=0; i<tiles.length; i++) { 

            //BufferedImage image = mapa.getTile((int) (objMap.get(i).getY() / 16), (int) (objMap.get(i).getX() / 16));
            g2d.drawImage(tiles[i].getImage(), tiles[i].x, tiles[i].y, null);
        }
    }
}

The problem is that calling the carregarTiles() method in both the constructor and the loadObj method causes this error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
When inserted in the draw () method, it works.

    
asked by anonymous 27.09.2017 / 16:22

1 answer

1

The problem is due to its objMap is empty when it enters the method objMap.size() its size is equal to or less than zero

    
27.09.2017 / 16:29