首页 > 解决方案 > 我想通过创建另一个类的对象来存储值,但它显示空指针异常

问题描述

class Car{
    // Instance variables
    String CarId;
    int CarModel;
    String CarBrand;
    int timeRented;
    boolean flag;
    int userRating;
    
    // GETERS and SETTERS
    public String getCarId() {
        return CarId;
    }
    public void setCarId(String carId) {
        this.CarId = carId;
    }
    public int getCarModel() {
        return CarModel;
    }
    public void setCarModel(int carModel) {
        this.CarModel = carModel;
    }
    public String getCarBrand() {
        return CarBrand;
    }
    public void setCarBrand(String carBrand) {
        this.CarBrand = carBrand;
    }
    public int getTimeRented() {
        return timeRented;
    }
    public void setTimeRented(int timeRented) {
        this.timeRented = timeRented;
    }
    public String isFlag() {
        if(flag) return "Car is not Available";
        else return "Car is Available for renting";
    }
    public void setFlag(boolean flag) {
        this.flag = flag;
    }
    public int getUserRating() {
        return userRating;
    }
    public void setUserRating(int userRating) {
        this.userRating = userRating;
    }
    
    // CONSTRUCTOR 
//      Car(String CarId, int CarModel, String CarBrand, boolean flag, int userRating, int timesRented){
//          this.CarId = CarId;
//          this.CarModel = CarModel;
//          this.CarBrand = CarBrand;
//          this.flag = flag;
//          this.userRating = userRating;
//          this.timeRented = timesRented;      
//      }   
//  
    // To print the car details
    public String toString() {
        return "Car Id is: "+this.CarId+
                "\nCar Model is: "+this.CarModel+
                "\nCar Brand is: "+ this.CarBrand+
                "\nCar is: "+ this.isFlag()+
                "\nCar is rented: "+this.timeRented+" times"+
                "\nUser rating of Car is"+ this.userRating;
    }   
}

class carStore extends Car {

//  carStore(String CarId, int CarModel, String CarBrand, boolean flag, int userRating, int timesRented) {
//      super(CarId, CarModel, CarBrand, flag, userRating, timesRented);
//          car[numCar].CarId = CarId;
//          car[numCar].setCarBrand(CarBrand);
//          car[numCar].setCarModel(CarModel);
//          car[numCar].setFlag(flag);
//          car[numCar].setTimeRented(timesRented);
//          car[numCar].setUserRating(userRating);
//      
//  }
    
    Car car [] ;
    
    //car = new Car(super.CarId, super.CarModel, super.CarBrand, super.flag, super.timeRented, super.userRating);
    //  carStore(String CarId, int CarModel, String CarBrand, boolean flag, int userRating, int timesRented) {
    //      super(CarId, CarModel, CarBrand, flag, userRating, timesRented);
    //}
        
    int numCar = 0; 
    // For adding car to the list of car
    void addCar(String CarId, int CarModel, String CarBrand, boolean flag, int userRating, int timesRented){    
        car[numCar].CarId = CarId;
        car[numCar].setCarBrand(CarBrand);
        car[numCar].setCarModel(CarModel);
        car[numCar].setFlag(flag);
        car[numCar].setTimeRented(timesRented);
        car[numCar].setUserRating(userRating);
        numCar += 1;
    }
    
    // For checking the Availability of the car 
    String checkOut(String carId){
        for(int i=0; i<5; i++) {
            if (car[i].CarId == carId) {
              car[i].isFlag();
             }
        }
        return "Car ID deos not Exist";
    }
    
    // For returning of the car
    String returnCar(String carId){
        for(int i=0; i<5; i++) {
            if (car[i].CarId == carId) {
              car[i].setFlag(false);
              return "Car returned successfully";
             }
        }
        return "Car ID deos not Exist";
    }
    // Give car a rating
    String recieveRating(String carId, int rating) {
        for(int i=0; i<5; i++) {
            if (car[i].CarId == carId) {
              car[i].setUserRating(rating);
              return "Rating given successfully";
             }
            
        }
        return "Car ID deos not Exist";
    }
    // Rent a car based on the car id
    String rentACar(String carId){
        for(int i=0; i<5; i++) {
            if (car[i].CarId == carId) {
              car[i].setFlag(true);
              System.out.println("Car Rented Successfully");
             }
        }
        return "Car ID deos not Exist";
    }
    
    // List of the car Store
    void listInventory() {
        for(int i=0; i<5; i++) {            
         System.out.println(car[i].getCarId()+" "+
                            car[i].getCarModel()+" "+
                            car[i].getCarBrand()+" "+
                            car[i].getTimeRented()+" "+
                            car[i].getUserRating()+" "+
                            car[i].isFlag());
         }
    }
    
}


public class CarLauncher {
  
   public static void main(String[] args) {
    // Creating object of the store
    carStore myCarStore = new carStore();
    // Adding 5 different cars 
    myCarStore.addCar("Abhinav", 2016, "Huyndai", false, 4, 10);
    myCarStore.addCar("Nipun", 2017, "Baleno", false, 5, 3);
    myCarStore.addCar("Nakul", 2020, "BMW", false, 5, 12);
    myCarStore.addCar("Abhishek", 2013, "Audi", false, 4, 6);
    myCarStore.addCar("Nipun", 2016, "Huyndai", false, 4, 7);
    
    //myCarStore.listInventory();
    
  }
   
}

在这段代码中,我想通过 carStore 类的 add 方法添加 5​​ 辆汽车,但它抛出空指针异常说 this.car 为空。谁能告诉我为什么 add 方法没有在汽车对象中添加值并引发以下错误。

线程“主”java.lang.NullPointerException 中的异常

at carStore.addCar(CarLauncher.java:94)
at CarLauncher.main(CarLauncher.java:166)

标签: javaclassobjectinheritance

解决方案


您的变量Car car[]未初始化。您必须定义它的大小:

Car car[] = new Car[yourSize];

然后,当您调用该addCar方法时,您必须实例化当前位置:

 void addCar(String CarId, int CarModel, String CarBrand, boolean flag, int userRating, int timesRented)
{

 if(numCar < car.length)
    {
        car[numCar] = new Car();
        car[numCar].CarId = CarId;
        car[numCar].setCarBrand(CarBrand);
        car[numCar].setCarModel(CarModel);
        car[numCar].setFlag(flag);
        car[numCar].setTimeRented(timesRented);
        car[numCar].setUserRating(userRating);
        numCar += 1;
    }
}

使用 if 条件,您将避免异常,因为您的数组具有您之前定义的大小。因此,您必须始终检查当前索引是否超出大小。


推荐阅读