如何解决在Java中访问对象函数时出现的错误?

huangapple go评论65阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月4日 05:40:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63237316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定