I have this code that tells me a NullPointerException
precisely at the method inizializza()
at line:
valori[y].add(new Record(matriceBinaria[i][j], j));
How can I add items to the valori
without that kind of exception?
I have this code that tells me a NullPointerException
precisely at the method inizializza()
at line:
valori[y].add(new Record(matriceBinaria[i][j], j));
How can I add items to the valori
without that kind of exception?
You must also initialize each element in you array as follows :
if (valori[y] == null)
valori[y] = new ArrayList();
valori[y].add(new Record(matriceBinaria[i][j], j));
ArrayList[size]
is actually an array where each element is the ArrayList
object. Since ArrayList
is not a primitive you must alloacte memory for it by using new
. So when you do valori[y]
in your code you are accessing an uninitialized (null) object