<分区>
我已经初始化了HashMap#onCreate
方法,我想在addLocation
方法中使用HashMap
。一切看起来都很好,但我不明白为什么会出现此错误:
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
当我调用 locationList#put
方法时。
这是我的代码:
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;;
}