5
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

4 Answers 4

5

You need to allocate memory to data.

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

Read this

8
  • Sweet handle! Where'd you get the pic? Is that the cover for the last book? Commented Oct 2, 2012 at 16:06
  • @Code-Guru Nope. This is the cover: tor.com/blogs/2012/05/… Commented Oct 2, 2012 at 16:10
  • 1
    Sweet! I love it! Can't wait! Commented Oct 2, 2012 at 16:23
  • @Code-Guru I as well. I hope you read the first chapter. If not here is the link. Some spoilers at the end! tor.com/stories/2012/09/… Commented Oct 2, 2012 at 16:29
  • I found the prologue, but not the first chapter. I'll have to find some time to read them both later. Commented Oct 2, 2012 at 16:32
2
String[][] data = null;

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

You might do

String[][] data = new String[10][];
0
1

You get a NPE because you explicitly set data to null:

String[][] data = null;

You need to allocate the number of rows first:

String[][] data = new String[][NUMBER_OF_ROWS];
1
data[counter] = new String[] {"abc"};

Here you are putting "abc" to array, but why you're using array if it has only one cell?

data[counter] = new String("sample string");

would be enough. And ofc you need also to declare "data" as one-dimensional array.

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.