问题描述
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 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 timesRented) {
// super(CarId,timesRented);
//}
int numCar = 0;
// For adding car to the list of car
void addCar(String CarId,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",5,3);
myCarStore.addCar("Nakul",2020,"BMW",12);
myCarStore.addCar("Abhishek",2013,"Audi",6);
myCarStore.addCar("Nipun",7);
//myCarStore.listInventory();
}
}
在这段代码中,我想通过 carStore 类的 add 方法添加 5 辆车,但它抛出空指针异常,说 this.car 为空。谁能告诉我为什么 add 方法没有在汽车对象中添加值并抛出以下错误。
线程“main”中的异常java.lang.NullPointerException
at carStore.addCar(CarLauncher.java:94)
at CarLauncher.main(CarLauncher.java:166)
解决方法
您的变量 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 条件,您将避免异常,因为您的数组具有您之前定义的大小。因此,您必须始终检查当前索引是否未超出大小。
,首先你的汽车数组没有初始化。这就是您得到 null pointer exception
的原因。通过初始化,您可以解决问题。
另外提出一些改进建议。
- 将 carStore 重命名为 CarStore。 Java 类名称应以大写字母开头。
class CarStore extends Car
-
在这种情况下避免使用数组
Car car []
。使用数组列表List<Car> carList
。 -
创建
里面CarStore
构造函数。在这个 initializecarList
public CarStore(){
List<Car> carList = new ArrayList<Car>();
}
- 修改
addCar
方法。在此处将new car
添加到carList
。
public void addCar(Car car){
carList.add(car);
}
- 以这种方式修改其余代码。
它提高了可读性并组织了您的项目。
,您的数组未初始化。尝试初始化数组 Car car[]