This is my code in a fragment.
'''
Bundle b= new Bundle();
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
adapter= new PlatformAdapter(data);
recyclerView=view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager( new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
RequestVolley.getInstance(getContext())
.doGetRequest("http://www...... .json", new RequestVolley.OnCompleteCallback() {
@Override
public void onCompleted(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for(int i=0;i<jsonArray.length();i++){
JSONObject object= jsonArray.optJSONObject(i);
if(object!=null){
data.add(Platform.parseJSON(object));
}
}
adapter.notifyDataSetChanged();
}catch(JSONException e){
e.printStackTrace();
}
}
});
b.putSerializable("ListPlatforms", (Serializable) data);
}
then, I try to do this, in another fragment: '''
@Override
public void onLocationChanged(@NonNull Location location) {
if (googleMap != null) {
if (myMarker == null) {
MarkerOptions options = new MarkerOptions();
options.title("My location");
options.position(new LatLng(location.getLatitude(), location.getLongitude()));
myMarker = googleMap.addMarker(options);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10f));
} else {
myMarker.setPosition(new LatLng(location.getLatitude(), location.getLongitude()));
}
}
Bundle b=new Bundle ();
ArrayList<Platform> platforms= (ArrayList<Platform>) b.getSerializable("ListPlatforms");
for(Platform platform:platforms) {
double latitudine = platform.getClatitudine__wgs84__();
double longitudine = platform.getClongitudine__wgs_84__();
double distance = getDistanceinKm(latitudine, longitudine, location.getLatitude(), location.getLongitude());
if (distance >=0.0) {
MarkerOptions options = new MarkerOptions();
options.title(platform.getCdenominazione__());
options.position(new LatLng(latitudine, longitudine));
myMarker = googleMap.addMarker(options);
}
}
} ''' But I have java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference error when I try to do" for(Platform platform:platforms)".. platforms in null. .. why??