-1

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?

Code java

1 Answer 1

1

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

1
  • Thanks! How do I create an iterator over "matriceBinaria"? I have set as follows: pastebin.com/Lbs6bWuj but I get "ArrayIndexOutOfBoundsException" and I print only the first values ​​of "valori" set to null! Commented Nov 25, 2012 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.