-1

So i have a 2d array that i create in a tester class and then i am attempting to send it and create a duplicate in the constructor, but a get a null error. Where am i going wrong? The constructor:

public TheaterSeatSeller(int[][] newSeats)
{
   for(int i=0; i<newSeats.length; i++)
  {
   for(int j=0; j<newSeats[i].length; j++)
   {
    seats[i][j]=newSeats[i][j];
   }
  }

}

and then the tester class

public static void main(String[] args){
   //initialize the available seats
   int[][] emptySeats = {
       {10,10,10,10,10,10,10,10,10,10},
       {10,10,10,10,10,10,10,10,10,10},
       {10,10,10,10,10,10,10,10,10,10},
       {10,10,20,20,20,20,20,20,10,10},
       {10,10,20,20,20,20,20,20,10,10},
       {10,10,20,20,20,20,20,20,10,10},
       {20,20,30,30,40,40,30,30,20,20},
       {20,30,30,40,50,50,40,30,30,20},
       {30,40,50,50,50,50,50,50,40,30}};
   TheaterSeatSeller mySeats = new TheaterSeatSeller(emptySeats);
   }
Artem M
  • 3
  • 2
  • What's the exact error? Message? Line number? thanks – alex.b Jan 19 '17 at 18:53
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Keiwan Jan 19 '17 at 18:54
  • note: there are no 2d arrays in Java, you are using an array of arrays – user85421 Jan 19 '17 at 19:25

1 Answers1

0

You have to initialize the seats array before assigning the value. This should fix it.

public TheaterSeatSeller(int[][] newSeats) {
    seats = new int[newSeats.length][newSeats[0].length];
    for (int i = 0; i < newSeats.length; i++) {
        for (int j = 0; j < newSeats[i].length; j++) {
            seats[i][j] = newSeats[i][j];
            System.out.println(seats[i][j]);
        }
    }

}
thetraveller
  • 445
  • 3
  • 10