0

I am trying to add the first element of each array in the two-dimensional array folderArray into a one dimensional array called tempArray as can be seen in the code shown below. However I am getting a null pointer exception from the tempArray. How can I fix this?

    int listLength = folderArray.length;
    String tempArray[] = null;
    for(int x = 0; x<listLength;x++){
        tempArray[x] = folderArray[x][0];
    }
3
  • 1
    What do you think String tempArray[] = null; does? Commented Mar 31, 2014 at 11:06
  • @ZouZou I cant use the tempArray inside the for loop without initializing it to null or else i get an error Commented Mar 31, 2014 at 11:08
  • But what do you think initializing it to null does? See also stackoverflow.com/questions/218384/… Commented Mar 31, 2014 at 11:16

2 Answers 2

3

You have to initialise your tempArray before you can assign anything to its elements:

String tempArray[] = new String[listLength];

is a good start (instead of String tempArray[] = null;)

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

Comments

1

Because you are assigning tempArray[] as null

Change it as,
String tempArray[] = new String[listLength];

Comments

Your Answer

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