使用包含另一个类的对象的构造函数作为属性

时间:2018-02-08 08:51:01

标签: java

我正在尝试测试我的类Location,它使用另外两个类作为属性AddressGeolocation但是当从main构造对象时,我得到一个指针错误。

这就是我的主要内容

import java.util.ArrayList;
public class LocationTest {
    public static void main(String[] args) {

        ArrayList<Location> locationList = new ArrayList<>();
        locationList.add(new Location(new Address(1, "Abubakr rd Almorsalat", "Riyadh", "Saudi Arabia"), new Geolocation(24.7136, 46.6753, 612), 1, "Prince Sultan University"));
        locationList.add(new Location(new Address(1, "Nassria st", "Sfax", "Tunisia"), new Geolocation(34.7478, 10.7662, 20), 2, "Second Location"));
        locationList.get(1).getGeolocation().setAltitude(20);
        locationList.get(0).getAddress().setStreetNumber(15);
        for(Location i : locationList) {
            System.out.println(i.getGeolocation());
        }

    }
}

我在Location内部使用的两个类的getter和setter这是他们的set方法

public void setAddress(Address address) {
    this.address.setStreetNumber(address.getStreetNumber());
    this.address.setStreetName(address.getStreetName());
    this.address.setCity(address.getCity());
    this.address.setCountry(address.getCountry());
}

public void setGeolocation(Geolocation geolocation) {
    this.geolocation.setLatitude(geolocation.getLatitude());
    this.geolocation.setLongitude(geolocation.getLongitude());
    this.geolocation.setAltitude(geolocation.getAltitude());
}

我觉得问题出在这里,我不确定

错误是

Exception in thread "main" java.lang.NullPointerException
at quiz01.fall2016.Location.setAddress(Location.java:59)
at quiz01.fall2016.Location.<init>(Location.java:20)
at quiz01.fall2016.LocationTest.main(LocationTest.java:13)

构造函数

public Location(Address address, Geolocation geolocation, int id, String name) {
    setAddress(address);
    setGeolocation(geolocation);
    setId(id);
    setName(name);
}

3 个答案:

答案 0 :(得分:2)

setAddress调用this.address上的设置者之前,请确保您正在初始化this.address

默认情况下,所有对象都将使用null进行初始化,因此您将遇到NullPointerException。

您的构造函数应如下所示

public Location(Address address, Geolocation geolocation, int id, String name) {
    // Initialize objects.
    this.address = new Address();
    this.geolocation = new Geolocation();
    setAddress(address);
    setGeolocation(geolocation);
    setId(id);
    setName(name);
}

答案 1 :(得分:1)

您应该按如下方式创建Location课程:

public class Location {

    private Address address;
    private Geolocation geolocation;
    private int id;
    private String name;

    public Location() {
        super();
    }

    public Location(Address address, Geolocation geolocation, int id, String name) {
        super();
        this.address = address;
        this.geolocation = geolocation;
        this.id = id;
        this.name = name;
    }

    // ... other methods ...
}

答案 2 :(得分:1)

在构造函数中,您假设this.address是自动初始化的,而事实并非如此。你还没有把它搞砸,这就是你面临NullPointerException的原因。

更改方法setAddress,如下所示:

public void setAddress(Address address) {

    this.address = new Addess();

    this.address.setStreetNumber(address.getStreetNumber());
    this.address.setStreetName(address.getStreetName());
    this.address.setCity(address.getCity());
    this.address.setCountry(address.getCountry());

}

您还必须对GeoLocation进行类似的更改。