-1

This is one of the functions in my ExpendableListAdapter for the listview for the child of a particular list group. groupPosition == 2 means when user expand the third list group, I want to show a different view from the first and second list group which only contain a textview.

public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    if (groupPosition == 2)
    {
        JSONObject jsonObject = (JSONObject) getItem(childPosition);
        Log.d("TestJson rating2", jsonObject.toString()); //line 62 <<<<<<

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.row_detail_review, null);
        }

        TextView username = (TextView) convertView.findViewById(R.id.username);
        RatingBar ratingBar = (RatingBar) convertView.findViewById(R.id.userRating);
        TextView review = (TextView) convertView.findViewById(R.id.review);

        String username2 = "";
        String ratingBar2 = "0";
        String review2 = "";

        if (jsonObject.has("username"))
            username2 = (jsonObject.optString("username"));

        if (jsonObject.has("rate"))
            ratingBar2 = (jsonObject.optString("rate"));

        if (jsonObject.has("comment"))
            review2 = (jsonObject.optString("comment"));



        username.setText(username2);
        ratingBar.setRating(Float.parseFloat(ratingBar2));
        review.setText(review2);

        return convertView;
    }

    else
    {
        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.row_detail_item, null);
        }

        TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }
}

I check the log which shows that there are values in the jsonObject but after that it shows the following NullPointerException error.

04-20 11:14:19.790 1759-1759/com.domain.appname D/TestJson rating2: {"username":"luqman","rate":"5","ratingId":"1","comment":"best","userId":"15"}
04-20 11:14:19.794 1759-1759/com.domain.appname D/TestJson rating2: {"username":"sumak","rate":"4","ratingId":"2","comment":"sedap","userId":"14"}
04-20 11:14:19.798 1759-1759/com.domain.appname D/AndroidRuntime: Shutting down VM
04-20 11:14:19.798 1759-1759/com.domain.appname W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4cfbb20)
04-20 11:14:19.798 1759-1759/com.domain.appname E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.domain.appname, PID: 1759

java.lang.NullPointerException

at com.mycompany.loginregister.ExpandableListAdapter.getChildView(ExpandableListAdapter.java:62)

What cause the error?

2 Answers2

0

Your JSON object is null and so is throwing a NullPointerException in this line:

Log.d("TestJson rating2", jsonObject.toString());

Check what is exactly returning your getItem(childPosition) method for the concrete value of childPostion that causes the error

Jorge
  • 1,136
  • 9
  • 15
0

So I have found the problem. There is another function as shown below..

public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
}

..that gets the number of children for a particular group that is only valid only for the first and second group. My third group require a different code to get the child count. So I modify the getChildrenCount function to this.

public int getChildrenCount(int groupPosition) {
    if (groupPosition == 2)
        return  this.jsonArray.length();

    else
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
}

So the error give a NullPointerException because the getChildView function loops more than the number of children it has.