NullPointerException while adding an object to an

2019-07-21 07:59发布

问题:

Alright so I'm having a problem with some code that is generating a null pointer exception, meaning that my code is attempting to use a null value where it should be using an object (Straight out of the API). What I'm doing is I have a class called Hotel that has a constructor which assigns a string value to a variable and creates an array of type Room, an object. I know this should be straight forward yet for the life of me I cannot figure this out.

I'll try and keep the code as minimalist as possible:

Constructor for an object, Hotel, that creates a new array of size NUM_ROOMS

Here are the included updates, I still am stuck with the Null pointer problem unfortunately

public Hotel(String hotelName)
{
    name = hotelName;
    theRoom = new Room[NUM_ROOMS];
}

This constructor creates an array of type Room, whose parameters are assigned to the variables

public Room(String guestName, int partySize, int bedCountRequired,
        double ratePerDay, int daysStaying)
{
    guest = guestName;
    number = partySize;
    numBeds = bedCountRequired;
    rate = ratePerDay;
    numOfDays = daysStaying;
}

This following method is supposed to:

1) create a new instance of the object Room using the supplied parameters

2) assign an array reference to that instance at the index roomsRented (defined as zero when the code is first initialized then increases by one after)

public void addReservation(String guestName, int partySize,
        int bedCountRequired, double ratePerDay, int daysStaying)
{
    Room roomHolder = new Room(guestName, partySize, bedCountRequired,
            ratePerDay, daysStaying);

    theRoom[roomsRented] = roomHolder;

    roomsRented++;
}

The problem that I am encountering is that

theRoom[roomsRented] = roomHolder;

is appearing to cause a null pointer exception. I am also getting an error from Eclipse that the array variable theRoom is unused despite that I use it in the aforementioned method. This leads me to believe that I am not referencing that object correctly. However, I don't know why that reference is incorrect, even if I substitute in theRoom[0], the compiler still gives me a null pointer. I don't see any problems with my array construction which to me means that the only problem that could cause a null pointer exception is if I don't reference any values in my array.

Any thoughts?

Thanks in advance for any contributions and apologies if the answer is painfully simple.

EDIT*** Thank you for your help, the problem with the variable not being found has been resolved but everytime I try and reference the object in the array I still get a nullpointer exception. Here is an example of how I'm trying to reference the array:

public double getTotalRentalSales()
{
    // Iterate through the array
    for (int i = 0; i < theRoom.length; i++)
    {
        // sum the amount money received per rental
        totalRate = totalRate + theRoom[i].getRate();
    }
    // Return total
    return totalRate;
}

where getRate() is a method that returns the variable rate from the object Room

totalRate = totalRate + theRoom[i].getRate();

is causing the null pointer in this situation. Is there a problem with my object reference?

回答1:

The problem is that in the constructor you're defining in its scope a variable called theRoom, that means you won't be assigning the value new Room[NUM_ROOMS] to your Hotel class field named theRoom but rather to a variable inside the constructor called theRoom.

Just remove the type declaration (Room[]) in the constructor and it will work as expected. I also suggest that in the future use the this keyword to help you avoid this kind of problems.

public Hotel(String hotelName)
{
 this.name = hotelName;
 this.theRoom = new Room[NUM_ROOMS];
}


回答2:

The problem is in Hotel's constructor:

public Hotel(String hotelName)
{
    name = hotelName;
    Room[] theRoom = new Room[NUM_ROOMS];
}

You probably have a theRoom instance field, right? The above assignment to theRoom creates a local variable that "hides" (and is independent from) the theRoom instance field, and is lost once the constructor returns. What you want is to initialize the theRoom instance field:

public Hotel(String hotelName)
{
    name = hotelName;
    theRoom = new Room[NUM_ROOMS];
}