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];
    }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • 1
    What do you think `String tempArray[] = null;` does? – Alexis C. Mar 31 '14 at 11:06
  • @ZouZou I cant use the tempArray inside the for loop without initializing it to null or else i get an error –  Mar 31 '14 at 11:08
  • But what do you think initializing it to `null` does? See also http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception – Alexis C. Mar 31 '14 at 11:16

2 Answers2

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;)

Aleks G
  • 56,435
  • 29
  • 168
  • 265
1

Because you are assigning tempArray[] as null

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

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55