Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

null pointer exception string 2d array in java

public String[][] fetchData()
{
    String[][] data = null;
    int counter = 0;
    while (counter < 10){
        data[counter] = new String[] {"abc"};
        counter++;
    }
    return data;
}

Getting the error in this loop. Please let me know where i am wrong

like image 753
user1714837 Avatar asked Oct 02 '12 16:10

user1714837


2 Answers

You need to allocate memory to data.

String[][] data = new String[ROW][COLUMN].

Read this

like image 112
Lews Therin Avatar answered Oct 10 '22 21:10

Lews Therin


String[][] data = null;

==> you have a null pointer exception when you try to write in data

You might do

String[][] data = new String[10][];
like image 34
Denys Séguret Avatar answered Oct 10 '22 21:10

Denys Séguret