Arraylist returning null values

Asked

Viewed 651 times

-1

Well, I have an array, I add values to that array, I send them to a method in another class that puts the values of that array into one string only and I add that single string into an Arraylist, but when I show it, it only shows several "null".

main class:

class Principal {
    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);
        Logica chamar = new Logica(valores);
        String[][] valores = new String[3][2];
        ArrayList colecao = new ArrayList();
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 2; j++) {
                if (j == 0) {
                    System.out.println("Insira um nome.");
                } else {
                    System.out.println("Insira um número.");
                } valores[i][j] = entrada.next();
            }
        } colecao.add(chamar);
        for (Object resolucao:colecao) {
            System.out.println(resolucao.toString());
        }
    }
}

logic class:

class Logica {
    String valores;
    public Logica(String[][] valores) {
        valores = new String[3][2];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 2; j++) {
                this.valores = valores[i][j] + "\n";
            }
        }
    }
    public String toString() {
        return valores;
    }
}

What is wrong?

1 answer

5


Your code has some errors, I will explain the modifications that would need to be performed:

Main class:

Its code demonstrates a custom, possibly inherited from C programming, which is to declare all variables at the beginning of the method before using them. In Java you don’t have to do this, it can even cause problems in your logic, for example in this section:

Logica chamar = new Logica(valores);
String[][] valores = new String[3][2];

This doesn’t even compile. You’re passing the variable valores for the class builder Logica before valores be stated. I believe what you want is to call that line:

Logica chamar = new Logica(valores);

just below the for chained, that is, just above that other line:

colecao.add(chamar);

With that your object chamar will receive the valores already completed by the user, which does not happen when new Logica(valores) is executed before the for.

Logic class:

This class has a line that should not exist...

valores = new String[3][2];

...because on that line you lose the values passed to the class Logica through your constructor. There is already a line initiating the variable valores with the sizes [3][2] in the main class, there is no need to do this here again. Rebooting the variable this way you do valores reference a new object array which will not contain the values entered by the user; therefore, this line must be removed.

Finally, if what you want to do is concatenate each value and form a single large string with the concatenated values, you must swap this line:

this.valores = valores[i][j] + "\n";

for that:

this.valores = this.valores + valores[i][j] + "\n";

or by a version using the operator +=:

this.valores += valores[i][j] + "\n";

One last recommendation: you are using the same name (valores) for a variable that is field of its class Logica and for another that is parameter of the constructor of this class. Avoid using the same name on both, as this makes the code less readable and can even cause confusion and logical errors when the code needs to be modified. Using different names for each of the variables you are even free to use this to specify that you are referring to one and not the other in your code.

  • I made the changes, it worked as expected, however, at the time of displaying, a "null" appears in the first line before the entered data. It was "nullvalor", the rest is normal. Where could this null be emerging?

  • 1

    Ah, I think that’s why the global variable valores starts as nil.

  • 1

    @Patrick I think it’s because in class Logica failed to declare the value of the attribute valores thus: String valores = "";

Browser other questions tagged

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