我想要一个类型为Reservation的对象数组(我的项目中的一个类)。要做到这一点,我需要初始化数组,首先将其所有位置初始化为null)然后当某些条件满足时使用此数组充当Room对象的成员变量,但是当我尝试时我不断得到NullPointerException访问该表,让我说明一下:
public class Room {
static int rCode=100;
private int roomID;
//kodikos domatiou paragetai me basi tin roomid
Reservation[] diathes;//here is the table of Reservation objects that i want containing null at first
private int maxAtoma;//final? static?
boolean k;
private double priceatom;//timi domatiou ana atomo
Reservation res;//reference to a reservation object
public Domatio(int maxPeople,double pricePAtom)
{
this.roomID=rCode++; //this.roomid=roomid
this.maxAtoma=maxPeople;
this.priceatom=pricePAtom;
Reservation[] diathes=new Reservation[30];
for(int i=0; i<diathes.length; i++)
{
diathes[i]=null;//i fill the array with null references to Reservation objects
}
}
boolean addreserve(Reservation res )
{
boolean kAdd=false;
int countExit=0;
for(int i=0; i<30; i++)
{
if(this.diathes[i]!=null || res.getAtomaOReserv()>this.maxAtoma){//here is the problem at this line where i access the diathes[] table null PointerException but is required of me to have it initialized with null,throwing the exception wont solve the problem
kAdd=false;
}
else{
for( int z = res.getAfixi();z<res.getAfixi()+res.getMeresDiam(); z++){//if these conditions meet i give a reference to a Reservation object to the analogous table element
this.diathes[z]=res;
countExit++;
}
kAdd=true;
res.antistixisidToDom(this);//antistoixi tou deikti tis reservation ,dom sto current room pou egine i kratisi
}
if(countExit!=0){
break;//exodos apo broxo an egine i kratisi
}
}
return kAdd;
}