Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:58:40+00:00 2026-06-14T20:58:40+00:00

Alright so I’m having a problem with some code that is generating a null

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:58:41+00:00Added an answer on June 14, 2026 at 8:58 pm

    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];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright y'all. I am attempting to write a bit of code that places typographic
Alright, this problem seems to be way above my head! I have this code:
Alright, I understand that this problem has been asked a lot of times at
Alright, I have some data that I need to assign an int type identifier
Alright so i need to open a .txt file that will be created in
Alright I created some custom classes for my project sourced from this tutorial ,
Alright, so I got this code for gluLookAt: lookAt = new Vector3f(-player.pos.x, -player.pos.y, -player.pos.z);
Alright, here I am again trying to write code from scratch and I can't
Alright I'm pretty new to programming and stuff and I'm now trying to code
Alright. I'm attempting to complete a school assignment and cannot for the life of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.