0

Eu tenho uma classe Assim:

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

O problema é que ao chamar o método carregarTiles() tanto no construtor como no método carregarObj ocasiona esse erro: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Já quando inserido no método draw(), funciona.

1
  • 2
    Chamar ele onde? Adicione a pilha de erros e onde você está chamando esse método.
    – user28595
    27/09/2017 às 14:23

1 Resposta 1

1

O problema é devido ao seu objMap está vazio quando ele entra dentro do método o objMap.size() o tamanho dele é igual ou inferior a zero

2
  • Mas isso é impossível.
    – leonardo
    27/09/2017 às 14:45
  • @leonardo isso pode ser verificado na prática de diversas maneiras, já pensei em umas 3. Quer tentar? 27/09/2017 às 15:41

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .