I am trying to pull some JSON data, parse it and send it as a parameter in order to open this data in a fragment that will display the information.
When receiving the bundle object in the fragment I am getting a NULL pointer exception.
Trying to trace back the error, seems to me that the ArrayList is storing null objects therefore when Passing that array to the fragment is trying to replace an instance with a null value
This is where I get the error in entry= bundle.getParcelableArrayList("Entry");:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the fragment layout file
Bundle bundle= getActivity().getIntent().getExtras();
entry= bundle.getParcelableArrayList("Entry");
Log.d("f", String.valueOf(entry));
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.gallery_fragment_layout,container,false);
initViews(rootView);
setRetainInstance(true);
return rootView;
}
And this is how I am adding the objects to the array:
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
//Getting object feed
JSONObject feed = jsonObj.getJSONObject("feed");
//Getting array node
JSONArray entry = feed.getJSONArray("entry");
for (int i = 0; i < entry.length(); i++) {
JSONObject e = entry.getJSONObject(i);
JSONObject name = e.getJSONObject("im:name");
String n = name.getString("label");
JSONArray image = e.getJSONArray("im:image");
for (int y = 0; y < image.length(); y++) {
JSONObject im = image.getJSONObject(y);
ims = im.getString("label");
}
JSONObject summary = e.getJSONObject("summary");
String label = summary.getString("label");
JSONObject category = e.getJSONObject("category");
JSONObject at = category.getJSONObject("attributes");
String term = at.getString("term");
data = new Entry();
data.setCategory(term);
data.setImage(ims);
data.setName(n);
data.setSummary(label);
Log.d("hola", term);
entries.add(i, data);
//System.out.printf("das",Arrays.toString(entries));
}
Passing data to Fragment:
private void callFragment(GalleryFragment galleryFragment, ArrayList<Entry> entry) {
//create parcel and add
ArrayList<Entry> e= entry;
galleryFragment= new GalleryFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Entry",e);
galleryFragment.setArguments(bundle);
Log.d("s", String.valueOf(entry));
//Initiate transaction
FragmentTransaction transaction= getSupportFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, galleryFragment, "galleryFragment");
transaction.addToBackStack(null);
transaction.commit();
}
and this is the log:
02-02 02:05:17.680 1884-1884/com.blitzar.testgrability E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.blitzar.testgrability, PID: 1884 java.lang.NullPointerException at com.blitzar.testgrability.GalleryFragment.onCreateView(GalleryFragment.java:40)
Why is this happening? any lead that speed me out of this will be highly appreciated, the complete project is in https://github.com/mastakillahBlitzar/Grability