0

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);
}
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
Budaika
  • 121
  • 9

3 Answers3

2

Before setAddress calls setters on this.address, make sure you are initializing this.address.

By default, all the objects will be initialized with null, so you are hitting NullPointerException.

Your constructor should be as follows

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);
}
krisnik
  • 1,406
  • 11
  • 18
1

You should create Location class as follows:

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 ...
}
Giancarlo Romeo
  • 663
  • 1
  • 9
  • 24
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Timothy Truckle Feb 08 '18 at 09:04
1

In your constructor you are assuming that this.address is initialized automatically, whereas, it is not. You haven't intiailized it, that's the reason you are facing NullPointerException.

Change you method setAddress like this :

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());

}

You will have to make similar change for GeoLocation as well.

Gaurav
  • 3,614
  • 3
  • 30
  • 51