I can not change an attribute of my array

0

@EDIT Introducing a minimal example, as requested:

MAIN

    package teste;
public class TESTE {
    public static void main(String[] args) {
        Tabuleiro cenario = new Tabuleiro(1);
        cenario.setVisibilidade();
    }

}

ABSTRACT CLASS PART

public abstract class Peça extends JFrame{
    ImageIcon normal;
    boolean VISIVEL;

    Peça(ImageIcon normal){
        this.normal = normal;
    }
    private String TIPO;
    public String getTipo(){
        return this.TIPO;
    }
    public void setTipo(String tipo){
        this.TIPO = tipo;
    }
}

TRAFFIC CLASS

public class Tabuleiro extends JFrame implements ActionListener{
    private int cont;
    private int DIFICULDADE,PLIN,PCOL; // PLIN = POSIÇÃO LINHA, PCOL = POSIÇÃO COLUNA
    private Peça tabuleiro[][];
    public Tabuleiro(int op){ // UM DOS POSSÍVEIS CONSTRUTORES
        super("Zumbicídio");
        this.cont = 0;
        this.DIFICULDADE = op;
        this.tabuleiro = new Peça[10][10];

    }
    public void setVisibilidade(){
        int i, j,plin,pcol;
        //DEIXANDO TODO O TABULEIRO INVISÍVEL
        for(i=0;i<10;i++){
            for(j=0;j<10;j++){
               this.tabuleiro[i][j].VISIVEL = false;
            }
        }
    }

Basically, my "play matrix" (board [10] [10]) has a boolean "VISIVEL" attribute (shown in first class). I wish to set this attribute to FALSE across the board at first. However, I do this, I get the error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

on the line

this.tabuleiro[i][j].VISIVEL = false;

of the above method.

Is this code sufficient to identify the error?

obs: I highlighted the variables using asterisks.

    
asked by anonymous 11.06.2018 / 15:09

1 answer

3

Your array is multidimensional that only contains Peças types (which is already an error using accents in variable names), but starting the array this way:

this.tabuleiro = new Peça[10][10];

You are only allocating memory space so that this array can receive n number of pieces within that array only. No parts were created inside it, just null spaces.

You need not only start the array, but also allocate it with objects of type Peça , instantiating one per position, otherwise it will be just an array full of null spaces.

    
11.06.2018 / 15:16