I cannot change an attribute of my matrix

Asked

Viewed 43 times

0

@EDIT Presenting 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 PIECE

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

BOARD 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 "Tile matrix" (board[10][10]) has a Boolean attribute "VISIBLE" (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.

This code is sufficient to identify the error?

Obs: I highlighted variables using asterisks.

  • 1

    Present a [mcve] to make it possible to execute the code. Access the link to learn how to make a.

1 answer

3


Its array is multidimensional and has only types Peças(which is already a mistake to use accents in variable names), but when starting the array this way:

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

you are just allocating space in memory so that this array can receive n number of pieces within that array only. No piece was created inside it, only null spaces.

You need to, in addition to starting the array, also allocate it with objects of type Peça, instantiating one per position, otherwise it will just be an array full of null spaces.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.