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

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

8 Comments

Sweet handle! Where'd you get the pic? Is that the cover for the last book?
@Code-Guru Nope. This is the cover: tor.com/blogs/2012/05/…
Sweet! I love it! Can't wait!
@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/…
I found the prologue, but not the first chapter. I'll have to find some time to read them both later.
|
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][];

Comments

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

Comments

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.

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.