-3

我得到了我认为是完成的程序,现在 java 正在把它拉到我身上。

我得到的错误如下:

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

主类:

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

这是我的单独课程:

//******************************************************************************
// 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");
    }
}
4

4 回答 4

2

您正在使用5-arg constructor构建您的HireCar实例: -

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

在那个构造函数中,你没有设置 - 的值"carStatus"

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

所以,carStatus还是null。(您应该在此构造函数中设置每个字段。至少references, 因为它们的默认值为null

因此,当您isAvailable为您添加的任何实例调用该方法时array: -

fleet[randomNumber].isAvailable() 

它会产生一个NPE,就像在isAvailable方法中一样,你正在调用equals方法carStatus: -

if(carStatus.equals("Available") || carStatus.equals("Return from Service"))
     ^^^
 This is null here
于 2012-12-03T22:17:58.547 回答
1

堆栈跟踪告诉您 isAvailable 方法正在抛出 NullPointerException,所以让我们看一下。

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

除了不必要的返回真/假之外,这里唯一被取消引用的是汽车状态,那么它什么时候可以为空?

好吧,看看你正在使用的构造函数:有 5 个参数的构造函数。它从来没有设置过状态,所以它保持为空。因此是空指针。

于 2012-12-03T22:18:36.490 回答
0

您正在使用 2-args 在构造函数中进行初始化 carStatus,并且您从不调用该构造函数,因此在carStatus调用方法时仍然为 null 。      equals()isAvailable()

  if(carStatus.equals("可用") || carStatus.equals("从服务返回")) {

   ^^^ This is **null** as it is not initialized yet, Thus **NPE**.

carStatus您还应该在您的 5-args 构造函数中进行初始化,以使您当前的代码能够正常工作。

 public HireCar(String demoManufacturer, String demoRegNumber, 
             int demoMileage, int demoServiceInterval, int lastInterval) { 
         manufacturer = demoManufacturer;
         regNumber = demoRegNumber;
         mileage = demoMileage;     
         carStatus="someval" ; //initialize carStatus here
     }
于 2012-12-03T22:17:56.483 回答
0
  1. carStatus 使用枚举代替字符串
  2. 初始化ALL构造函数中的所有属性(左二carStatus = null)
于 2012-12-03T22:18:19.017 回答