-3

Eclipse is telling me that, "The type of the expression must be an array type but it resolved to Theater"(The class for the object I created for my 2D array) on the last line of code I have below. Specifically here --> a[row]

This is just one small piece of a larger project I am working on in my Java class. You all may be familiar with it, I have to print and implement a theater seating chart using a 2D array. I have to write methods for searching by price, searching by seat, etc. Right now I am just trying to initialize the 2D array, put some values in it then print them out. Any help is much appreciated.

public class Theater {
//int[][] x = new int[9][10];   
int y[][];

    public Theater(){
        //Initialize array
        for (int row = 0; row < 3; row++)
            for (int column = 0; column < 10; column++)
                y[row][column] = 10;
    }

    public static void main(String[] args){
        Theater a = new Theater();

        for(int i = 0; i < 3; i++)
            for (int row = 0; row < 9; row++)
                for (int column = 0; column < 10; column++)
                    System.out.println(a[row][column]);
  • `a` is not an array, it's just an Object of type `Theater`. – Kon Jul 28 '16 at 22:05
  • Make sure you initialize `y` too. You can't assign to the index values of `y` if it's still `null`. – 4castle Jul 28 '16 at 22:11
  • [`Arrays.deepToString(Object[])`](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#deepToString-java.lang.Object:A-) is what you want. And, you need to access the array `y` (so, `a.y`). – Elliott Frisch Jul 28 '16 at 22:14

1 Answers1

0

The [] operator needs to be applied to a expression that is an array. Since Theater is not an array, you get a compile time error. You probably wanted to access the y field of the Theater instance:

...
System.out.println(a.y[row][column]);

Furthermore you need to create the y array before using it:

public Theater(){
    this.y = new int[9][10];
    ...

Otherwise this will result in a NullPointerException when trying to write to write to the array (y[row][column] = 10;).

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Thanks guys. Once I initialized the y array and made it a.y instead of just a it let me print. Now I'm trying to figure out how to loop it through only ten times. But thats another question. Thanks again :-) – John White Jul 29 '16 at 02:37