I've initialized HashMap#onCreate
method and i want to use HashMap
in addLocation
method. Everything looks okay but I don't understand why I get this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.put(java.lang.Object, java.lang.Object)' on a null object reference
when I call locationList#put
method.
Here's my code:
public class InitLocationsOnStartup extends Application implements LocationListener {
HashMap < String, Double[] > locationList;
List < StationViewModel > stations;
@Override
public void onCreate() {
super.onCreate();
stations = new ArrayList < StationViewModel > ();
locationList = new HashMap < > (); //
}
public void getStations(List < StationViewModel > list) {
this.stations = list;
addLocation(locationList);
}
public HashMap < String, Double[] > addLocation(HashMap < String, Double[] > locationList) {
for (int i = 0; i < stations.size(); i++) {
latLong = new Double[] {
Double.parseDouble(stations.get(i).getLatitude()),
Double.parseDouble(stations.get(i).getLongitude())
};
if (stations.get(i).getStationName() != null && latLong != null) {
locationList.put(stations.get(i).getStationName(), latLong);
}
}
return locationList;;
}