I'm in my first year of computer science and am having a hell of a time with a project. I was given the code that creates a list of room numbers via an array, and I am to create a method that allows the user to pick a room, enter in their information, and reserve the room.
My issue seems to lie in creating the method that actually reserves the room... which is as follows...
public void bookRoom (Room[] room, int numberOfRooms)
{
//Get settings for room (smoking, occupied, guest name/number)
displayRoomsInfo(room, numberOfRooms);
Room room1 = new Room(false, false, null, null, numberOfRooms);
//user input,store each value to temp variables
Scanner scan=new Scanner(System.in);
System.out.println("Please choose a room");
int guestRoomNum=scan.nextInt();
room1.setRoomNumber(guestRoomNum);
//set room[i] occupied = true;
room1.setOccupied(true);
//set room[i] guestname to name
System.out.println("Enter Guest Name");
String dickshit = scan.next();
String[] nameList = {dickshit};
room1.setGuestName(nameList);
//set room[i] guestnumber to phone
System.out.println("Enter Guest Phone Number");
String phoneInput = scan.next();
room1.setGuestPhone(phoneInput);
}
I keep getting a null pointer exception on
Room room1 = new Room(false, false, null, null, numberOfRooms);
Which is the line where I actually create an instance of the custom class that I was provided. I don't have a freaking clue as to why or how I can go about preventing this, since those appear the default constructor values.
The Room class code is as follows:
public class Room {
private boolean smoking;
private boolean occupied;
private String[] guestName=new String[4];
private String guestPhone;
private int roomNumber;
public Room (boolean smoking,boolean occupied,String[] guestName, String guestPhone,int roomNumber)
{
this.smoking=smoking;
this.occupied=occupied;
for (int i=0;i<4;i++)
this.guestName[i]=guestName[i];
this.guestPhone=guestPhone;
this.roomNumber=roomNumber;
}
public void setGuestName(String[] guestName)
{
for (int i=0;i<4;i++)
this.guestName[i]=guestName[i];
}
public String[] getGuestName()
{
String[] tempGuestName=new String[4];
for (int i=0;i<4;i++)
tempGuestName[i]=this.guestName[i];
return tempGuestName;
}
public void setOccupied(boolean isFull)
{
this.occupied=isFull;
}
public boolean getOccupied()
{
return occupied;
}
public void setSmoking(boolean canSmoke)
{
this.smoking=canSmoke;
}
public boolean getSmoking()
{
return smoking;
}
public void setGuestPhone(String phoneNumber)
{
this.guestPhone=phoneNumber;
}
public String getGuestPhone()
{
return guestPhone;
}
public void setRoomNumber(int roomNum)
{
this.roomNumber=roomNum;
}
public int getRoomNumber()
{
return roomNumber;
}
}
this.guestName[i]=guestName[i];guestNameis passednullin the constructor.