0

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

Jhonycage
  • 759
  • 3
  • 16
  • 36

2 Answers2

0

I suggest you to use:

JSONObject feed = jsonObj.optJSONObject("feed");

instead of:

JSONObject feed = jsonObj.getJSONObject("feed");

Which omit the value in case response is null from server

Same way use String n = name.optString("label");

JSONArray image = e.optJSONArray("im:image");

So that may be the case you get null from the server and because of null it can't parse the data and not stored values in your ArrayList<Entiry> entries

Pranav Darji
  • 744
  • 3
  • 13
  • Actually I am printing the data before adding it to the Array and is retrieving all the data correctly, so I think is not that – Jhonycage Feb 02 '17 at 07:21
0

Special thanks to @Piyush who pointed out my mistake at transferring the data from one array to another

e.addAll(entry);

I fixed the issue by decoding the array which I didn't realize before this is the resulting code:

    Bundle bundle = getArguments();
    entry= bundle.getParcelableArrayList("Entry");
    e=new Entry[entry.size()];
    for(int x=0; x<entry.size();x++) {
        e[x]=entry.get(x);
        Log.d("testing",e[x].getName());
Jhonycage
  • 759
  • 3
  • 16
  • 36