Variable might not have been initialized, but I can't get around it...

I'm getting a variable might not have been initialized error, and I know why, but I can't get around it. Basically, this snippet of my code is reading in a map file for a game. The first two lines of the map file are configuration information, and the rest is coordinates for the tiling graphics.

while ((str = in.readLine()) !=null){

if(!read_info){

//process first line: tileset image, size, playerpos

temp = str.split(",");

loadTileset(temp[0],32);

size_x = Integer.parseInt(temp[1]);

size_y = Integer.parseInt(temp[2]);

map =new Tile[size_x][size_y];

player.resetLoc(Integer.parseInt(temp[3]),Integer.parseInt(temp[4]));

//process second line: impassible

str = in.readLine();

temp = str.split("!");

impassable =new Point[temp.length];

for(int m=0;m<temp.length;m++){

tmp_coords = temp[m].split(",");

impassable[m] =new Point(Integer.parseInt(tmp_coords[0]),Integer.parseInt(tmp_coords[1]));

}

read_info =true;

}else{

temp = str.split("!");

for(int i=0;i<size_x;i++){

tmp_coords = temp[i].split(",");

boolean ti =true;

for(int p=0;p<impassable.length;p++){

map[i][j] =new Tile(Integer.parseInt(tmp_coords[0]),Integer.parseInt(tmp_coords[1]),true);

}

}

j++;

}

}

I'm not including everything.. this should make sense. Impassable is a Point[].

I can't say how big the Impassable list will be until it reads the second line of the map file. However, that happens in the first run-through of the file-reading processes.

Is there a try/catch statement I can use to get past this?>

[2800 byte] By [maxfarrara] at [2007-11-26 22:48:37]
# 1

I pasted that wrong. The last for loop, which uses p, is this (I'm pasting a bit outside of it for context)

for(int i=0;i<size_x;i++){

tmp_coords = temp[i].split(",");

boolean ti = true;

for(int p=0;p<1;p++){

if(impassable[p].equals(new Point(Integer.parseInt(tmp_coords[0]),Integer.parseInt(tmp_coords[1])))){

ti = false;

}

}

map[i][j] = new Tile(Integer.parseInt(tmp_coords[0]),Integer.parseInt(tmp_coords[1]),ti);

}

>

maxfarrara at 2007-7-10 12:08:33 > top of Java-index,Java Essentials,Java Programming...
# 2
> I'm getting a variable might not have been initialized errorYour compiler also tells you clearly *which* variable might not have been initialized. Care to tell us what your compiler tried to tell you?kind regards,Jos
JosAHa at 2007-7-10 12:08:33 > top of Java-index,Java Essentials,Java Programming...
# 3
If you set that variable to null you will fix this error.Object obj;to Object obj = null;Beware this may cuase NullPointerExceptions, so you should check this variable for being null before you try to access it.
dunnigan14a at 2007-7-10 12:08:33 > top of Java-index,Java Essentials,Java Programming...
# 4

> If you set that variable to null you will fix this

> error.

>

> Object obj;

>

> to

>

> Object obj = null;

>

> Beware this may cuase NullPointerExceptions, so you

> should check this variable for being null before you

> try to access it.

That's usually the wrong way to approach this. Getting this compiler error is often a sign of a logic error. Only initialize the variable if it makes sense for the variable to have that value. Don't do it just to satisfy the compiler.

jverda at 2007-7-10 12:08:33 > top of Java-index,Java Essentials,Java Programming...
# 5
the big word there is of course "usually".There are situations where the compiler can't figure out that all possible paths leading to the first read of a variable will indeed initialise it, but those are pretty rare.
jwentinga at 2007-7-10 12:08:33 > top of Java-index,Java Essentials,Java Programming...