I'm trying to test my class Location
that uses two other classes as attributes Address
and Geolocation
However when constructing the object from main I get a pointer error.
This is what I have in 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());
}
}
}
and I have getters and setters for the two classes I'm using inside of Location
this is their set method
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());
}
I have a feeling that the problem is here, I'm not sure
The error is
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)
The constructor
public Location(Address address, Geolocation geolocation, int id, String name) {
setAddress(address);
setGeolocation(geolocation);
setId(id);
setName(name);
}