0

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;
}

}

1
  • this.guestName[i]=guestName[i]; guestName is passed null in the constructor. Commented Apr 22, 2014 at 6:43

5 Answers 5

1

Why not get user input, and then make room for it? After user has entered the data (and you saved it to variables)

Room room1 = new Room(false, true, nameList, phoneInput, roomNumber);
Sign up to request clarification or add additional context in comments.

Comments

0

That's because in your Room() constructor, you're doing this.

for (int i=0;i<4;i++)
    this.guestName[i]=guestName[i];

But you're passing null for the guestName as the parameter, which throws a NPE as your guestName array is null.

If you want to pass a null, remove the for loop from the constructor. If not, pass a blank String[] to the constructor.

Room room1 = new Room(false, false, new String[4], null, numberOfRooms);

Note that even when you do the above, each element in the String[] is still a null reference only and you need to explicitly initialize them in your program later.

Comments

0

guestName is null in

 Room room1 = new Room(false, false, null, null, numberOfRooms);

but your constructor requires it not to be null

for (int i=0;i<4;i++)
    this.guestName[i]=guestName[i];

You should either add a test in your constructor :

if(guestName != null)

or pass a valid array when you call the constructor

Comments

0

You are trying access guest name in constructor Which is null. Take care that , You Should not do any operation on on values those are passed/assigned/turned Null.

Comments

0

In your for loop

for (int i=0;i<4;i++)
    this.guestName[i]=guestName[i];

The Argument guestName is null. Since you do not provide any value to it:

    Room room1 = new Room(false, false, null, null, numberOfRooms);

See the 3rd arugment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.