user2998603
user2998603

Reputation: 21

unknown error for array of objects

Im making a program to play pacman and have several classes, i have a Wall class, a Goast class a Pacman class a Pellot class and a Board class. all of them are compiled without errors except board, it is not complete but what i have is not compiling, what i have is

import java.awt.*;
import javax.swing.*;
public class Board{
    private int xDim_=1000;
    private int yDim_=900;
    private Pacman pacman_=new Pacman(475,525,0);
    private Goast greenredGoast_=new Goast(525,350,2,Color.RED);
    private Goast onageGoast_=new Goast(525,275,1, Color.ORANGE);
    private Goast pinkGoast_=new Goast(425,350,2,Color.MAGENTA);
    private Goast blueGoast_=new Goast(425,275,0,Color.CYAN);
    Wall[] walls = new Wall[22];
    walls[0]=new Wall(50,850,900,50);  
    walls[1]=new Wall(0,0,50,900);
    walls[2]=new Wall(50,0,900,50);
    walls[3]=new Wall(125,125,50,150);

im getting 100 errors that are on every entry in creating a new Wall, the errors are as follows:

 walls[0]=new Wall(50,850,900,50);  
          ^
Board.java:12: error: ';' expected
    walls[0]=new Wall(50,850,900,50);  
           ^
Board.java:12: error: illegal start of type
    walls[0]=new Wall(50,850,900,50);  
            ^
Board.java:12: error: <identifier> expected
    walls[0]=new Wall(50,850,900,50);  
             ^
Board.java:12: error: ';' expected
    walls[0]=new Wall(50,850,900,50);  
                ^
Board.java:12: error: illegal start of type
    walls[0]=new Wall(50,850,900,50);  
                     ^
Board.java:12: error: <identifier> expected
    walls[0]=new Wall(50,850,900,50);  
                      ^
Board.java:12: error: <identifier> expected
    walls[0]=new Wall(50,850,900,50);  
                         ^
Board.java:12: error: illegal start of type
    walls[0]=new Wall(50,850,900,50);  
                            ^
Board.java:12: error: <identifier> expected
    walls[0]=new Wall(50,850,900,50);  
                             ^
Board.java:12: error: <identifier> expected
    walls[0]=new Wall(50,850,900,50);  
                                 ^
Board.java:12: error: illegal start of type
    walls[0]=new Wall(50,850,900,50);  
                                   ^
Board.java:12: error: <identifier> expected
    walls[0]=new Wall(50,850,900,50);  
                                    ^
Board.java:12: error: ';' expected
    walls[0]=new Wall(50,850,900,50);  
                                     ^

and im getting that for every single Wall. i cant figure out what is wrong someone PLEASE help!! thanks

Upvotes: 0

Views: 65

Answers (3)

Monicka Akilan
Monicka Akilan

Reputation: 1549

Create a method and put your array codes inside that the following code will work

import java.awt.*;
import javax.swing.*;
public class Board{
    private int xDim_=1000;
    private int yDim_=900;
    private Pacman pacman_=new Pacman(475,525,0);
    private Goast greenredGoast_=new Goast(525,350,2,Color.RED);
    private Goast onageGoast_=new Goast(525,275,1, Color.ORANGE);
    private Goast pinkGoast_=new Goast(425,350,2,Color.MAGENTA);
    private Goast blueGoast_=new Goast(425,275,0,Color.CYAN);

    public void run() { // create a method
    Wall[] walls = new Wall[22];
    walls[0]=new Wall(50,850,900,50);  
    walls[1]=new Wall(0,0,50,900);
    walls[2]=new Wall(50,0,900,50);
    walls[3]=new Wall(125,125,50,150);
    }
}

Upvotes: 0

Pshemo
Pshemo

Reputation: 124225

You can't invoke walls[0]=new Wall(50,850,900,50); at class level.

public class Board{
    ...
    Wall[] walls = new Wall[22];
    walls[0]=new Wall(50,850,900,50);
    walls[1]=new Wall(0,0,50,900);
    ...
}

You need to place it in either:

initialization block,

public class Board{
    ...
    Wall[] walls = new Wall[22];
    {
        walls[0]=new Wall(50,850,900,50);
        walls[1]=new Wall(0,0,50,900);
        ...
    }
    ...
}

constructor

public class Board{
    ...
    Wall[] walls = new Wall[22];

    public Board(){
        walls[0]=new Wall(50,850,900,50);
        walls[1]=new Wall(0,0,50,900);
        ...
    }
    ...
}

or some method

public class Board{
    ...
    Wall[] walls = new Wall[22];

    public void fillBoard(){
        walls[0]=new Wall(50,850,900,50);
        walls[1]=new Wall(0,0,50,900);
        ...
    }
    ...
}

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201447

Assuming you only need one array of walls then I might use a static block to initialize my array of Wall(s) like this -

private static Wall[] walls = new Wall[22];

static {
  walls[0]=new Wall(50,850,900,50);  
  walls[1]=new Wall(0,0,50,900);
  walls[2]=new Wall(50,0,900,50);
  walls[3]=new Wall(125,125,50,150);
  ...
}

Upvotes: 1

Related Questions