Skip to content
Advertisement

I have an error in android studio with Bundle and ArrayList

This is my code in a fragment.

”’

Bundle b= new Bundle();

@Override

JavaScript

then, I try to do this, in another fragment: ”’

@Override

JavaScript

} ”’ 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??

Advertisement

Answer

  • 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

JavaScript
  • 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):
JavaScript
  • 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.
JavaScript

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement