Android - JSON array from asset and NullPointerException

I want to make an application that takes data from a local JSON file and puts it into a custom ExpandableListView. Here is my code:

Parent class:

public class Parent {

    private String name;
    private ArrayList<ChildData> child;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<ChildData> getChild() {
        return child;
    }

    public void setChild(ArrayList<ChildData> child) {
        this.child = child;
    }
}

      

ChildData class:

public class ChildData {
    private String opis1;
    private String opis2;
    private String img_id;

    public String getOpis1() {
        return opis1;
    }

    public void setOpis1(String opis1) {
        this.opis1 = opis1;
    }

    public String getOpis2() {
        return opis2;
    }

    public void setOpis2(String opis2) {
        this.opis2 = opis2;
    }

    public String getImg_id() {
        return img_id;
    }

    public void setImg_id(String img_id) {
        this.img_id = img_id;
    }
}

      

MainActivity class:

public class MainActivity extends ExpandableListActivity {

    private ArrayList<Parent> parents;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getExpandableListView().setGroupIndicator(null);
        getExpandableListView().setDividerHeight(1);
        registerForContextMenu(getExpandableListView());

        //Creating static data in arraylist
        final ArrayList<Parent> dataList = putDataIntoArrays();

        // Adding ArrayList data to ExpandableListView values
        loadHosts(dataList);

    }


    public String loadJSONFromAsset() {
        String json = null;
        try {

            InputStream is = getAssets().open("data.json");

            int size = is.available();

            byte[] buffer = new byte[size];

            is.read(buffer);

            is.close();

            json = new String(buffer, "UTF-8");


        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;

    }

    public ArrayList<Parent> putDataIntoArrays() {
        JSONObject obj = null;

        ArrayList<Parent> list = new ArrayList<>();
        Parent parent = new Parent();
        ChildData cd = new ChildData();

        try {
            obj = new JSONObject(loadJSONFromAsset());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            JSONArray m_jArry = obj.getJSONArray("data");

            for (int i = 0; i < m_jArry.length(); i++) {

                JSONObject jo_inside = m_jArry.getJSONObject(i);

                parent.setName(jo_inside.getString("title"));
                Log.d("Test", "Parent name: " + parent.getName());

                cd.setOpis1(jo_inside.getString("desc"));
                cd.setOpis2(jo_inside.getString("desc2"));
                cd.setImg_id(jo_inside.getString("img"));
                Log.d("Test", "O1: " + cd.getOpis1());
                Log.d("Test", "O2: " + cd.getOpis2());
                Log.d("Test", "IMG: " + cd.getImg_id());

                parent.getChild().add(cd);

                Log.d("Test", "Parent: " + parent.getChild());

                list.add(parent);
                Log.d("Test", "List: " + list.get(i));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    private void loadHosts(final ArrayList<Parent> newParents)
    {
        if (newParents == null)
            return;

        parents = newParents;

        // Check for ExpandableListAdapter object
        if (this.getExpandableListAdapter() == null)
        {
            //Create ExpandableListAdapter Object
            final ExpandableListAdapter mAdapter = new ExpandableListAdapter();

            // Set Adapter to ExpandableList Adapter
            this.setListAdapter(mAdapter);
        }
        else
        {
            // Refresh ExpandableListView data
            ((ExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged();
        }
    }
}

      

And data.json

    {
"data": [
{
    "title": "T1",
    "desc": "D1",
    "desc2": "opis1",
    "img": "1"
},
{
    "title": "T2",
    "desc": "D2",
    "desc2": "opis2",
    "img": "2"
},
{
    "title": "T3",
    "desc": "D3",
    "desc2": "opis3",
    "img": "3"
},
{
    "title": "T4",
    "desc": "D4",
    "desc2": "opis4",
    "img": "1"
}
    ]
}

      

Also when I run my application I don't get any results from the logs:

Log.d("Test", "Rodzic1: " + parent.getChild());
Log.d("Test", "List: " + list.get(i));

      

+3


source to share


1 answer


You have not initialized this ArrayList in your class Parent

.

private ArrayList<ChildData> child;

      

In your constructor, Parent

you must add:



child = new ArrayList<ChildData>();

      

I assume this is yours NullPointerException

.

+3


source







All Articles