0
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;

public class ImageLoader {
    public static BufferedImage loadImage(String path)
    {
        try {
            return ImageIO.read(ImageLoader.class.getResource(path));
        } catch(IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        return null;
    }
}

public class Assets {

    BufferedImage BackgroungImage, PlayerSprite, Enemy1, Enemy2, Enemy3, Enemy4;
    BufferedImage[] EnemySprites = new BufferedImage[4];

    public static void init()
    {
        BackgroundImg = ImageLoader.loadImage("/Assets/Images/BG.jpg");

        PlayerSprite = ImageLoader.loadImage("/Assets/Sprites/WaveRider.png");

        Enemy1 = ImageLoader.loadImage("/Assets/Sprites/Enemy1.png");
        Enemy2 = ImageLoader.loadImage("/Assets/Sprites/Enemy2.png");
        Enemy3 = ImageLoader.loadImage("/Assets/Sprites/Enemy3.png");
        Enemy4 = ImageLoader.loadImage("/Assets/Sprites/Enemy4.png");

        EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};        
    }

}

I have been trying to create an array of BufferedImage using the code above but I am getting the error bellow.I am getting the same type of errors for all the variables I am trying to store in the array. I can not trace the errors in the code. I tried assigning the values of the array while declaring but it also gave me the same errors.

Assets.java:19: error: illegal start of expression
        EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};        
                       ^
Assets.java:19: error: not a statement
        EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};        
                        ^
Assets.java:19: error: ';' expected
        EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};        
                              ^
Assets.java:19: error: not a statement
        EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};        
                                ^
Assets.java:19: error: ';' expected
        EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};        
                                      ^     
                                                      ^
  • The array initialization short hand can only be used if you are declaring the array on the same line. – Mark Rotteveel Aug 27 '20 at 15:39
  • @MarkRotteveel initially (with rapid skimming) I've also spotted that problem, but it's not about array in this case. He's actually trying to access the non-static members. – Giorgi Tsiklauri Aug 27 '20 at 15:40
  • @GiorgiTsiklauri The error literaly points to `EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};` , and the cause is that they should use `new BufferedImage[] { ... }`. That they then will have a non-static field access error is the next problem they will have to tackle. In any case, I have also added a duplicate for that problem. – Mark Rotteveel Aug 27 '20 at 15:43
  • @MarkRotteveel yes, I agree, but have a look on the question. His problem prevails for all other variables alike. I will actually add back what I initially had in my first version of the answer (your point). – Giorgi Tsiklauri Aug 27 '20 at 15:44
  • You declare this: `BufferedImage BackgroungImage` but you initialize this: `BackgroundImg = ImageLoader.loadImage("/Assets/Images/BG.jpg");`. The variable names are different. After I fixed that problem, I got this [compile] error: `Cannot make a static reference to the non-static field BackgroundImage` So I don't understand why you are saying that the problem is with the array initializer. Did I miss something? – Abra Aug 27 '20 at 15:51

2 Answers2

1

You have several problems in your code.

Problem 1: Note, also, that array initialization with the {"literal", "array", "value"} is only allowed inline with the definition, i.e. when you define your array.

You will have to initialize it like: new YourType[]{values here..};.


Problem 2: Note, that all these variables:

BackgroungImage, PlayerSprite, Enemy1, Enemy2, Enemy3, Enemy4;

are Instance(non-static) fields, and you are trying to refer them from a Static Context, which you cannot do as instance member belongs to instance, and static member belongs to the class.

You can do either:

  1. create a new instance of Assets and access the members of that instance;
  2. define your fields as static.

Problem 3:

I will call this a problem as well: Please, do NOT use PascalCase for naming your class members (except for nested classes).

Giorgi Tsiklauri
  • 6,699
  • 7
  • 29
  • 54
0

Why don't you try assignments with indices?

EnemySprites[0] = Enemy1;
..
..
EnemySprites[3] = Enemy4;
snr
  • 13,515
  • 2
  • 48
  • 77