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

  • Home
  • SEARCH
  • 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 8932129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:16:20+00:00 2026-06-15T09:16:20+00:00

I got to what I thought was a finished program and now java is

  • 0

I got to what I thought was a finished program and now java is pulling this on me.

The error I get is as follows:

Exception in thread "main" java.lang.NullPointerException
    at hirecardemo.HireCar.isAvailable(HireCar.java:68)
    at hirecardemo.HireCarDemo.runSimulation(HireCarDemo.java:49)
    at hirecardemo.HireCarDemo.main(HireCarDemo.java:25)
Java Result: 1

Main Class:

package hirecardemo;
import java.util.Random;

public class HireCarDemo {

    public static void main(String[] args) {
        HireCar car0 = new HireCar("Toyota", "AV77 FGJ", 6000, 12300, 41500);
        HireCar car1 = new HireCar("Mercedes", "DI99 FTZ", 6700, 7000, 91800);
        HireCar car2 = new HireCar("Toyota", "FG82 FTP", 25000, 12000, 72000);
        HireCar car3 = new HireCar("Vauxhall", "TW56 LTS", 10000, 11000, 19001);
        HireCar car4 = new HireCar("Ford", "TD85 LTU", 13000, 12300, 12000);
        HireCar car5 = new HireCar("Susuki", "GU12 UTJ", 12000, 10000, 50000);

        HireCar[] fleet = {car0, car1, car2, car3, car4, car5};
        int minMileage = 1000;
        int maxMileage = 60000;
        int numberOFevents = 12;

        String [] results = HireCarDemo.runSimulation(fleet, numberOFevents,
                minMileage, maxMileage);

        for(int i = 0; i < numberOFevents; i++) {
            System.out.println(results[i]);
        }
    }

    /**
    * @param fleet the fleet of hire cars
    * @param numberOFevents the size of the events table to be generated
    * @param minMileage the assumed minimum mileage driven by any hired
    * car
    * @param maxMileage the assumed maximum mileage driven by any hired
    * car
    * @return table of events generated during the simulation
    */
    public static String[] runSimulation(HireCar [] fleet, int numberOFevents, 
            int minMileage, int maxMileage) {
        int n = fleet.length; // Number of cars in the fleet.
        Random carGenerator = new Random();
        String [] events = new String [numberOFevents];
        for(int i = 0; i < numberOFevents; i++) {
            int randomNumber = carGenerator.nextInt(n-1);
            if(fleet[randomNumber].isAvailable() == true)
            {
                fleet[randomNumber].hireOut();
                events[i] = fleet[randomNumber].getRegNumber() + " <HIRE OUT>";
            }

            else if(fleet[randomNumber].isOnHire() == true)
            {
                Random mileage = new Random();
                int randomMileage = mileage.nextInt(maxMileage - minMileage); 

                if(fleet[randomNumber].isBeingServiced() == true)
                {
                    events[i] = fleet[randomNumber].getRegNumber() + 
                            " <RETURN FROM HIRE>" + " <SEND FOR SERVICE>";
                } else {
                    events[i] = fleet[randomNumber].getRegNumber() + 
                            " <RETURN FROM HIRE>";                    
                }
            }
            else 
            {
                fleet[randomNumber].makeAvailable();
                events[i] = fleet[randomNumber].getRegNumber() + 
                        " <RETURN FROM SERVICE>";
            }
        }
        return events;  
    }
}

Here is my separate class that goes along with this:

//******************************************************************************
// HireCar.java                                             Author: Ryan Holder
// 
// Represents the car hire company's fleet of cars and the information on them.
//******************************************************************************
package hirecardemo;


public class HireCar {

    private String manufacturer, regNumber, carStatus;
    private int mileage, serviceInterval, lastService; // All in miles.    

    private boolean serviceDue() { 
        if((mileage- lastService) >= serviceInterval) {
            this.sendForService();  
            return true;
        } else { 
            return false;
        }
    }

   private void sendForService() {
       carStatus = "Servicing";      
   }

    //--------------------------------------------------------------------------
    // Default Constructor: Sets information for a new car.
    //--------------------------------------------------------------------------    
    public HireCar(String demoManufacturer, String demoRegNumber) {
        manufacturer = demoManufacturer;
        regNumber = demoRegNumber;
        carStatus = "Available";
        serviceInterval = 0;        
        lastService = 0;
        mileage = 0;                 
    }  

      public HireCar(String demoManufacturer, String demoRegNumber, 
             int demoMileage, int demoServiceInterval, int lastInterval) { 
         manufacturer = demoManufacturer;
         regNumber = demoRegNumber;
         mileage = demoMileage;     
     }

    public void setMileage(int demoMileage) {
        mileage = demoMileage;
    }    

    public void setServiceInterval(int demoServiceInterval) {
        serviceInterval = demoServiceInterval;
    }

    public void setLastService(int demoLastService) {
        lastService = demoLastService;    
    }

    public String getRegNumber() {
        return regNumber;
    }

    public void makeAvailable() {
        carStatus = "Available";
    }


    public boolean isAvailable() {
        if(carStatus.equals("Available") || carStatus.equals("Return from Service")) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isOnHire() {
        if(carStatus.equals("On Hire")) {
            return true;
        } else {
            return false;
        }
    }


    public boolean isBeingServiced() {
        if(carStatus.equals("Being Serviced")) {
            return true;
        } else {
            return false;
        }
    }


 public void hireOut() {
        carStatus = "On Hire"; 
    }

    public void returnFromHire() {
        if(this.serviceDue() == true) {
            carStatus = "Being Serviced";            
        } else {
            carStatus = "Available For Hire";
        }        
    }

    public void returnFromService() {
        carStatus = "Return From Service";
    }

    public String ToString() {
        return("Manufacturer: <" + this.manufacturer + ">/n "
                + "Registration Number: <" + this.regNumber + ">/n" 
                + "Mileage: <" + this.mileage + ">/n" 
                + "Service Interval: <" + this.serviceInterval + "</n"
                + "Last Service: <" + this.lastService + "</n"
                + "Status: <" + this.carStatus + "</n");
    }
}
  • 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-15T09:16:21+00:00Added an answer on June 15, 2026 at 9:16 am

    You are using 5-arg constructor to construct your HireCar instance: –

    new HireCar("Toyota", "AV77 FGJ", 6000, 12300, 41500);
    

    And in that constructor, you haven’t set the value for – "carStatus".

     public HireCar(String demoManufacturer, String demoRegNumber, 
             int demoMileage, int demoServiceInterval, int lastInterval) { 
         manufacturer = demoManufacturer;
         regNumber = demoRegNumber;
         mileage = demoMileage;     
     }
    

    So, carStatus is still null. (You should set every field in this constructor. At least the references, because their default value is null)

    So, when you invoke the isAvailable method for any of the instance you added in your array: –

    fleet[randomNumber].isAvailable() 
    

    It will result in a NPE, as in isAvailable method, you are invoking equals method on carStatus: –

    if(carStatus.equals("Available") || carStatus.equals("Return from Service"))
         ^^^
     This is null here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this really simple piece of code that I thought was the correct
I've got a MediaPlayer activity that I thought I was finished with until I
I thought the following script will create div element but I got nothing output
I've got an asp.net control inside a masterpage, so I thought the following selector
I got this tasks table that has TODO items. We are retrieving the todo
This is probably obvious, so bear with me. YES, I KNOW THAT java.io.File has
EDIT: Clarifying. fout is is a FILE*. (I thought this was irrelevant since that
I am 95% finished with this (I hope). I am working on an accordion
Hi I just thought I would share this here I am new to cakephp
And just when I thought I am finished with the Internationalization and timezones and

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.