【问题标题】:I want to store value by creating object of another class, but its showing null pointer exception我想通过创建另一个类的对象来存储值,但它显示空指针异常
【发布时间】:2021-02-13 02:42:21
【问题描述】:
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)

【问题讨论】:

  • 您应该在使用 Car car[] 之前对其进行初始化。如果汽车数量未知,我建议使用 List 而不是 Array。

标签: java class object inheritance


【解决方案1】:

您的变量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 条件,您将避免异常,因为您的数组具有您之前定义的大小。因此,您必须始终检查当前索引是否超出大小。

【讨论】:

    【解决方案2】:

    首先你的汽车数组没有初始化。这就是你得到null pointer exception 的原因。通过初始化可以解决问题。

    另外提出一些改进建议。

    1. carStore 重命名为 CarStore。 Java 类名应以大写字母开头。

    class CarStore extends Car

    1. 在这种情况下避免使用数组Car car []。使用数组列表List&lt;Car&gt; carList

    2. 创建CarStore 构造函数。在这个里面初始化carList

    public CarStore(){
       List<Car> carList = new ArrayList<Car>(); 
    }
    
    1. 修改addCar方法。在此处将new car 添加到carList
    public void addCar(Car car){
       carList.add(car);
    }
    
    1. 以这种方式修改其余代码。

    它可以提高可读性并组织您的项目。

    【讨论】:

      【解决方案3】:

      您的数组未初始化。尝试初始化数组 Car car[]

      【讨论】:

      • 是的,先生,但这不是真正的问题
      猜你喜欢
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多