1

I have a problem with my code, because it throws NullPointerException when I try to assign string from arraylist to array.

 String[][] data = new String[idList.size()][];
 for(int i = 1; i<=idList.size(); i++) {
     data[i][0] = idList.get(i);
     data[i][1] = nameList.get(i);
     data[i][2] = hList.get(i);
     data[i][3] = sList.get(i);
     data[i][4] = fList.get(i);
     data[i][5] = mList.get(i);
     data[i][6] = osList.get(i);
     data[i][7] = tsList.get(i);
     data[i][8] = podList.get(i);
     data[i][9] = pacList.get(i);
 }

Can someone please tell me how do I fix that?

1
  • In your debugger you will be able to see that data[0] is null. I suggest you initialise them. Commented Apr 10, 2016 at 8:46

2 Answers 2

1

data[i] is null, since you initialized data to contain idList.size() null references of type String[].

Change

String[][] data = new String[idList.size()][];

to

String[][] data = new String[idList.size()][10];

which will initialize data to contain idList.size() references to a String array of length 10 (String[10]).

Sign up to request clarification or add additional context in comments.

Comments

0

I suggest to define the array properly (in both dimensions )

replace this

new String[idList.size()][];

for something like

new String[idList.size()][10]; // you have 10 different lists there...

and iterate in the for loop with zero base...

Example:

    String[][] data = new String[idList.size()][10];
    for (int i = 0; i < idList.size(); i++) {
        data[i][0] = idList.get(i);
        data[i][1] = idList.get(i);
        data[i][2] = idList.get(i);
        data[i][3] = idList.get(i);
    }

Comments

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.