-2

I am fairly new to Java, I've looked all over the Internet and stackoverflow itself to understand what I am doing wrong to no luck. Can someone please tell me why I get this error? I think I've initialized the array the right way and I think I've instantiated it right as well.

public class Grid {

 private Slot[][] array = new Slot[12][12];

 public Grid(){};


 public void printarray(){   

    for(int i = 1 ; i <= 10 ; i++){
        System.out.print(i + "|" + "");
        for(int j = 1 ; j <= 10 ; j++){     

            System.out.print(array[i][j].getStatus());

        }//end inner for        

        System.out.print("\n");
    }//end outer for 
    System.out.print("\n");

};

}//end of class




public class Slot {


    public Slot(){};


    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    private int status = 0 ;
    private int id = 0 ;

}//end of slot

public static void main(String[] args) {
    Grid myGrid = new Grid();
    myGrid.printarray();
}//end of main
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Lucawuz
  • 1
  • 1
  • 3

2 Answers2

3

You've initialized the array properly, but it's still empty. You need to create objects of type Slot and populate the array before you can pull objects out and call methods on them.

System.out.print(array[i][j].getStatus());

That line is trying to pull a Slot from position i, j of your array, but there's nothing there, so it returns null. Calling getStatus() on a null reference is causing the exception.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • You mean I would need to create 144 Slot Objects? I've done the same thing in c++ and the grid constructor calls the constructor for each slot so that there is no need to create them manually, does Java treat this cases differently? – Lucawuz Nov 24 '14 at 18:30
  • If you want a 12x12 array of Slot objects, then yes you need to create 144 Slot objects. – takendarkk Nov 24 '14 at 18:31
  • @Lucawuz Yes, you need a loop or some other code to create those objects and add them to your array. `new Slot[12][12]` in Java just creates the empty array for you. – Bill the Lizard Nov 24 '14 at 18:31
  • Oh, I see, thank you. So new Slot[12][12] doesn't call the constructor of Slot? – Lucawuz Nov 24 '14 at 18:34
  • @Lucawuz No, it only creates the array. It doesn't call the `Slot` constructor. – Bill the Lizard Nov 24 '14 at 18:37
0

The problem is your array is initialized with nulls at every location. You should initialize it with values in your constructor if you don't want the values to be null. Also in your print statement you are not printing all the elements only the first 10 elements of the first 10 rows.

Since you declare it at the global level:

private Slot[][] array = new Slot[12][12];

every position array[number][number] will have null as its initial value. You could initialize in your constructor like:

public Grid(){
   for(int i = 0 ; i < 12 ; i++){
       for(int j = 0 ; j < 12 ; j++){
           array[i][j] = new Slot(0, 0);
       }
   }
}

public Slot(int id, int status){
     this.id = id;
     this.status = status;
}
brso05
  • 13,142
  • 2
  • 21
  • 40
  • For the printing part that's what I want it to do, I don't get why my elements are not initialized, shouldn't the grid constructor call the slot constructor 144 times on its own? – Lucawuz Nov 24 '14 at 18:33
  • @Lucawuz No you will have to initialize each element of the array like above I posted an example constructor for the Grid class. – brso05 Nov 24 '14 at 18:35
  • @Lucawuz I gave you an example of what your 2 constructors could look like. – brso05 Nov 24 '14 at 18:37