views:

37

answers:

3

I've written the following code:

for(int layer = 0; layer <countLayers; layer++);
{
    List<Sprite> spritesInLayer = sceneGraph.getLayer(layer);
}

when i compile this snippet, I get an error, that in the line within the for-Loop, eclipse complains that 'layer' is an unknown symbol [... = sceneGraph.getLayer(layer);] and wants me to introduce the field / variable / ... 'layer'.

But when using this snippet, it works.

int layer = 0;
for(layer = 0; layer <countLayers; layer++);
{
    List<Sprite> spritesInLayer = sceneGraph.getLayer(layer);
}

does anybody know, what I'm missing in the first code? Or might this be some kind of a bug of eclipse / the java compiler?

I'm using Java 6 JDK Update 20 64 bit on Win 7 64-bit Home Premium and Eclipse Helios 64-bit (build 20100617-1415)

+3  A: 

Change

for(int layer = 0; layer <countLayers; layer++);

to

for(int layer = 0; layer <countLayers; layer++)

The spurious semicolon means that the for loop has an empty body. The following {....} is interpreted as a separate statement. And of course, layer is out of scope within that block.

Stephen C
*duh*I didn't see this - when working on my laptop, I press sometimes accidentially on the touchpad and overwrite some lines, or insert code at the wrong position - this is how that semicolon came in there (at least I think so, as there are some lines missing, which should be below the for-Loop)Thanks!
Elvith
+2  A: 

delete the semicolon after the for line! The contents of the braces are not looped in your example, and therefore layer is undefined...

That's why eclipse is useful!

Sanjay Manohar
+1  A: 

Please remove semicolon ";" from following line.

for(int layer = 0; layer < countLayers; layer++);

for statement doesn't require ;

YoK