Tomasz Gutkowski
Tomasz Gutkowski

Reputation: 1418

Why 'variable can be only null at this location'?

I have this:

    String data[][] = null;
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            data[i][j]= "test";
        }
    }

But it doesn't work. Variable data is null.

Upvotes: 0

Views: 2002

Answers (8)

scravy
scravy

Reputation: 12283

Of course is data null, you explicitly said so. If you think that

String data[][] = null;

should initialize a 2-dimensional array and each value to null, you are mistaken.

What you need is for example:

String data[][] = new String[10][10]

this initialized a 2-dimensional array with 100 elements, that is to say: an array with 10 elements, each being an array with 10 elements.

An array in Java is an object, just like any other object, and thus has to be initialized with new. data in your example is a reference to an array which itself consists of references to other arrays (= objects).

Upvotes: 3

belgther
belgther

Reputation: 2534

because you have to initialize your array first. Consider the array like a "pointer" in C/C++. You have to write something like String data[][] = new String[10][10];

Upvotes: 4

Pratik
Pratik

Reputation: 30855

you didn't initialize you array. you can define array size as per your need like given example

2d array it's like table row and column you must initialize first row size then column size

here are the example of initialization

data[][] = new String[5][]; // after this you need to define for 2d array like this
data[0] = new String[2];  
data[1] = new String[3];
data[2] = new String[1];

as above the column was dynamic you define the column size as much as you want another one is

data[][] = new String[5][3] then each row has 3 column

Upvotes: 0

M S
M S

Reputation: 4093

String data[][] is just a reference that can hold a two dimentional string array. since you have not added any object to the reference, it points to null

so do

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

to add an object in to the reference.

Upvotes: 0

Dewasish Mitruka
Dewasish Mitruka

Reputation: 2896

Array assign value as a Reference type . to to asign any value in it, you have to first create instance of it otherwise it will give error. so you have to write the following :

 String data[][] = new String[10][10];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            data[i][j]= "test";
        }
    }

Upvotes: 0

Andy
Andy

Reputation: 9048

That is because you've assigned null to it. You need to create an array and assign that to data instead. Try this instead:

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

Upvotes: 1

Klas Lindb&#228;ck
Klas Lindb&#228;ck

Reputation: 33273

Arrays in java are kind of objects and have to be allocated with new.

Replace

 String data[][] = null;

with

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

Upvotes: 3

stacker
stacker

Reputation: 68942

Your first line should be

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

Upvotes: 7

Related Questions