gpt4 book ai didi

java - HashMap: java.lang.NullPointerException: 尝试调用虚拟方法 'java.lang.Object java.util.HashMap.put'

转载 作者:行者123 更新时间:2023-11-29 23:59:55 26 4
gpt4 key购买 nike

<分区>

我已经初始化了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;;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com