views:

328

answers:

5
static BufferedImage img1[];
for(int i=0;i<60;i++)
   {
     img1[i] = new BufferedImage((int) (width), (int) (height), BufferedImage.TYPE_INT_RGB);

    }

It shows an error on the line above this code : Syntax error on token ";", { expected after this token

and below this code as: Multiple markers at this line - Method breakpoint:Video [entry] - main(String[]) - Syntax error on token ")", ; expected - Syntax error on token "(", ; expected

+1  A: 

One problem here is that you can't declare a variable which is in the scope of a method to be static. (Or, alternatively, you can't write a for loop which is neither in a method nor a static block.) Fix one of these.

This is a significant difference between C/C++ and Java: In C/C++, you can declare static variables inside functions, and those variables will retain their values across function calls. Java doesn't have that. If you want a variable to retain its value this way, you need to make it a (possibly static) member of a class.

uckelman
thanks for your reply. I removed the static declaration but Im kinda still lost. could you please help me figure out my mistake?
You probably want the for loop inside a static initialiser (surround with `static {` and `]`). But it's a bad idea to have static mutable data.
Tom Hawtin - tackline
+1  A: 

I assume you've got an error in some of your other code. You've also got an error in this code -- you need to declare the length of the img1 array before using it...

BufferedImage img1[] = new BufferedImage[60];
Kaleb Brasee
A: 

Your syntax is incorrect. You have to declare an array right after the type.

static BufferedImage[] img1 = new BufferedImage[2];

is the correct syntax.

Here's the edited code from eclipse. This does compile.

import java.awt.image.BufferedImage;


public class Test {
    static BufferedImage[] img1 = new BufferedImage[60];
    static{
        for(int i=0;i<60;i++)
        {
        int width = 20;
        int height = 20;
        img1[i] = new BufferedImage((int) (width), (int) (height), BufferedImage.TYPE_INT_RGB);

        }
    }
}

The following problems we're also found:

  1. width and height undeclared. I initialized them to meaningless numbers
  2. put the for loop in a static block. Code can't exist in the class unless it's in a method or a static block of a class.
  3. This also necessitated the creation of a class Test.

I'm sure that most of this was due to the fact you only submitted a code snippet. I've included it for completeness. This code will create 60 Buffered Images of size 20x20

Randaltor
Im struggling to read multiple images into an array so that i can acces them.I used this line for a single image,static BufferedImage img = new BufferedImage((int) (width), (int) (height), BufferedImage.TYPE_INT_RGB);it works perfectly. but to make it an array im having trouble.. If i comment out the array iniatlizating and go ahead with single declaration theres no problem in my code and works fine. am I doing wrong? BufferedImage img1[] = new BufferedImage[60]; for(int i=0;i<60;i++) { img1[i] = new BufferedImage((int) (width), (int) (height), BufferedImage.TYPE_INT_RGB);}
The square brackets need to be after the type and not after the name of the variable. That's a c syntax. Try making it like the above example. I was able to put your code in eclipse and make it work after making those mods.
Randaltor
A: 

Im struggling to read multiple images into an array so that i can acces them.I used this line for a single image, static BufferedImage img = new BufferedImage((int) (width), (int) (height), BufferedImage.TYPE_INT_RGB);

it works perfectly. but to make it an array im having trouble.. If i comment out the array iniatlizating and go ahead with single declaration theres no problem in my code and works fine. am I doing wrong?

BufferedImage img1[] = new BufferedImage[60]; for(int i=0;i<60;i++)
{ 
img1[i] = new BufferedImage((int) (width), (int) (height), BufferedImage.TYPE_INT_RGB);
}

what am I doing wrong?

+1  A: 

Setting aside questions of style and encapsulation, I suspect the problem is related to where you're doing this in a class.

To instantiate the array in a method, you could do something like this:

class MyClass1 {
  public void initImages(int width, int height) {
    BufferedImage img1[] = new BufferedImage[60];
    for (int i = 0; i < img1.length; i++) {
      img1[i] = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }
  }
}

The array doesn't escape the method, so it will die there.

To instantiate the array as a static member, you could something like this:

class MyClass2 {
  static int width = 100;
  static int height = 100;
  static BufferedImage img1[] = new BufferedImage[60];
  static {
    for (int i = 0; i < img1.length; i++) {
      img1[i] = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }
  }
}

This uses a static initialization block.

...or this:

class MyClass3 {
  static BufferedImage img1[] = initImages(100, 100);

  public static BufferedImage[] initImages(int width, int height) {
    BufferedImage img1[] = new BufferedImage[60];
    for (int i = 0; i < img1.length; i++) {
      img1[i] = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }
    return img1;
  }
}

This uses a static method to initialize a static member.

McDowell