英文:
How to solve error when accessing object function in java?
问题
以下是您提供的代码的翻译部分:
public class Main {
public static void main(String[] args) {
// 创建一个名为"Rushi's Parking lot",大小为100x100的停车场
ParkingLot parkingLot = new ParkingLot("Rushi's Parking lot", 100, 100);
// 创建一辆车,并将其注册到停车场
Vehicle car1 = new Car("License");
parkingLot.registerVehicle();
}
}
public class ParkingLot {
String name;
ParkingSpot[][] parkingGrid;
public ParkingLot(String name, int xSize, int ySize){
this.name = name;
this.parkingGrid = new ParkingSpot[xSize][ySize];
}
public void registerVehicle() {
// 打印第一个停车位是否可用
System.out.println(parkingGrid[0][0].isAvailable());
}
public void getNextSpot(Vehicle vehicle){
}
}
public class ParkingSpot {
private boolean available = true;
private String license;
private int time;
public boolean isAvailable() {
return available;
}
public void reserveSpot(String license){
this.license = license;
this.available = false;
}
}
错误信息翻译如下:
Exception in thread "main" java.lang.NullPointerException
at ParkingLot.registerVehicle(ParkingLot.java:14)
at Main.main(Main.java:7)
如果需要关于错误的解释或修复建议,请告诉我。
英文:
Hi I'm creating a 2D array of objects in java and want to access one of the functions that simply returns the boolean value of the variable but for some reason, its throwing an error. The code looks so simple but I can't figure out what I am doing wrong. My guess is something to do with my implementation of the array, but I think I did it right. My goal is to create individual instances of the class for each value in the 100x100 grid.
Here is the code and error I am getting
public class Main {
public static void main(String[] args) {
ParkingLot parkingLot = new ParkingLot("Rushi's Parking lot", 100, 100);
Vehicle car1 = new Car("License");
parkingLot.registerVehicle();
}
}
public class ParkingLot {
String name;
ParkingSpot[][] parkingGrid;
public ParkingLot(String name, int xSize, int ySize){
this.name = name;
this.parkingGrid = new ParkingSpot[xSize][ySize];
}
public void registerVehicle() {
System.out.println(parkingGrid[0][0].isAvailable());
}
public void getNextSpot(Vehicle vehicle){
}
}
public class ParkingSpot {
private boolean available = true;
private String license;
private int time;
public boolean isAvailable() {
return available;
}
public void reserveSpot(String license){
this.license = license;
this.available = false;
}
}
Error:
Exception in thread "main" java.lang.NullPointerException
at ParkingLot.registerVehicle(ParkingLot.java:14)
at Main.main(Main.java:7)
答案1
得分: 4
创建ParkingLot类型的对象时,数组parkingGrid只包含空引用。您需要实际添加要使用的停车位。
到目前为止,您只指定了parkingGrid的大小为100x100并包含停车位,但个别元素并不存在。
英文:
When you create an object of type ParkingLot, the array parkingGrid only contains null-references. You need to actually add the parking spots you want to use.
So far you only specified that parkingGrid has size 100x100 and contains parking spots, but the individual elements aren't there.
答案2
得分: 0
你在哪里添加了车辆?数组为空,当然会抛出空指针异常,因为其中没有任何数据。您需要添加车辆和注册位置的方法,这样当您添加车辆时就可以移动到下一个位置。
英文:
Where did you added vehicle? Array is empty, of course it will throw null pointer because there is no any data in it. You need method for adding vehicles and registering positions so you can move to next one when you add vehicle also.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论