Alright, I’ve been getting a NPE that I can’t figure out and it’s driving me absolutely batty. I’ve got a linked list of Reservation objects and a 2d array of booleans to keep track of the available seats:
class ResList
{
private Reservation head;
private boolean [][] seats;
ResList()
{
head = null; //empty list
boolean[][] seats = new boolean[5][25];
}
I’ve also got a method isAvailable() to determine whether a seat is available:
boolean isAvailable(int f, int s)
{
if(f<0 || f>4 || s < 0 || s > 24)
return false;
else
return !seats[f][s]; // this line throws the NPE
}
But when I do this:
jcbSeat = new JComboBox();
for(int i = start; i <= stop; i++)
{
if(list.isAvailable(selectedFlight, i))
jcbSeat.addItem(i+1);
}
I get a NPE where noted. I added some debugging lines to the ResList constructor, and can access seats[][] there just fine, but when I execute the method, ka-boom: NPE. What is going on here?
Your constructor is initializing a local variable
seats, not the instance variableseats.Try with :