Java Array: no value received

Asked

Viewed 52 times

1

I’m having a little problem regarding a matrix I’m trying to develop to get coordinates inside a JFrame and pass them to position the buttons correctly. Follow the code:

public class Coordenadas {

    //coordenadas
     int posicLinha;
     int posicColuna;
    //metodo para atribuir as coordenadas as devidas posições da matriz. 
    public void atribPosic (){
        Coordenadas[][] tabuleiro = new Coordenadas [8][8];//matriz de objetos coordenadas
        for (int l=0;l<8;l++){
            for (int c=0;c<8;c++){
                tabuleiro[l][c].posicLinha = 100+50*l;//primeira coordenada de linha, será igual a 100
                tabuleiro[l][c].posicColuna = 100+50*c;//primeira coordenada de coluna, será igual a 100
                System.out.print(tabuleiro[l][c].posicLinha);//imprime a coordenada da linha 
                System.out.print(tabuleiro[l][c].posicColuna);//imprime a coordenada da coluna
            }
        }
    }
    public static void main (String[]args){
        Coordenadas coordenadas = new Coordenadas ();
        coordenadas.atribPosic();
    }
}

My problem is, it doesn’t print the value due to an error in the coordinate value assignment formula. I would like help, I do not know the cause of this mistake.

1 answer

3

You don’t say in the question what the mistake is. What you’re having is a NullPointerException. The assignment formulas you use are not the problem and are correct.

The reason for error is because the matrix is initially created containing null in all positions. And when you do tabuleiro[l][c].posicLinha, tabuleiro[l][c] will be null and try to access the posicLinha of null will give NullPointerException.

The simplest and easiest solution to this problem of yours is to add this at the beginning of for intern:

tabuleiro[l][c] = new Coordenadas();

This will make your code run, but it still won’t print what you want because you give a print in one number, gives another print then without adding space, breaking the line or making the numbers stick together, and for each coordinate you do this by making all the numbers stick to each other forming a single linguistics of digits at the end in a single line with no spaces or commas and nothing in between. By using what is below, this is solved:

System.out.println(tabuleiro[l][c].posicLinha + ", " + tabuleiro[l][c].posicColuna);

With these two corrections, your code will behave as expected. However, your code still suffers from other structural problems, although it works anyway (but will probably break if you make any changes). Note that to create the matrix you use an empty and useless instance of Coordenadas that only exists so that you can call the method atribPosic. This can be solved by using the modifier static in the method, dispensing with the need for an instance to use it.

Also not putting visibility modifiers in your fields/attributes is not usually a good idea. You hardly intended to use package visibility. In addition, class names should preferably be nouns in the singular, and therefore Coordenada would be a better name than Coordenadas.

There is also no need or purpose in whether to "eat" some letters of variable names and methods, and therefore atribuirPosicao is better than atribPosic. The same goes for posicLinha and posicColuna who might stay posicaoLinha and posicaoColuna, but simply linha and coluna is simpler.

Your code with these fixes and changes all look like this:

public class Coordenada {

    private int linha;
    private int coluna;

    public static void atribuirPosicao() {
        Coordenada[][] tabuleiro = new Coordenada[8][8];
        for (int l = 0; l < 8; l++) {
            for (int c = 0; c < 8; c++) {
                tabuleiro[l][c] = new Coordenada();
                tabuleiro[l][c].linha = 100 + 50 * l;
                tabuleiro[l][c].coluna = 100 + 50 * c;
                System.out.println(tabuleiro[l][c].linha + ", " + tabuleiro[l][c].coluna);
            }
        }
    }

    public static void main(String[] args) {
        Coordenada.atribuirPosicao();
    }
}

Finally, I recommend that you see that other answer of mine that I posted in that other question to understand what are the causes of a NullPointerException.

  • Thank you very much! I managed to fix the mistake. It was silly thing. Thanks!

Browser other questions tagged

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