Home > OS >  I have an error in android studio with Bundle and ArrayList
I have an error in android studio with Bundle and ArrayList

Time:09-22

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??

CodePudding user response:

  • since you didn't share how do you navigate between fragments I will assume you're using Navigation graph with actions.

  • so if we assume that this is your code navigate from fragment A to B

 NavHostFragment.findNavController(FragA.this)
                .navigate(R.id.action_FragA_FragB); //navigate from FragA to FragB action
  • then you should pass the bundle that you used b.putSerializable("ListPlatforms", (Serializable) data); on from the first fragment as a second parameter in the navigation code like this (notice I'm now including b as a second parameter to the method):
NavHostFragment.findNavController(FragA.this)
                .navigate(R.id.action_FragA_FragB,b); //navigate from FragA to FragB action
  • then in the second fragment you should be getting this b bundle using the getArguments() method which could be called from anyplace in a fragment.
Bundle FragA_b = getArguments();
ArrayList<Platform> platforms= (ArrayList<Platform>) FragA_b.getSerializable("ListPlatforms");

N.B : don't get confused with the savedInstanceState bundle that gets sent as a parameter in OnCreate,onViewCreated,onStateRestored and onSaveState life cycle fragments methods, these savedInstanceState bundle has nothing to do with the one that you send during navigation and get using getArguments() method.

  • Related