javaandroidnullpointerexceptionexpandablelistviewexpandablelistadapter

How can I fix this error in ExpandableListView?


I want to get data for my exbandableListView from the api. But there is problem in ExpandableListAdapter.

SSS.java

public class SSS extends BaseActivity {
private DrawerLayout mDrawerLayout; /// Drawer Layout
private ExpandableListView listView;
private ExpandapleListAdapter listAdapter;
private ArrayList<SSSDataModel> listDataHeader;
private HashMap<String,List<String>> listHash;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sss);
    /// Expandaple List view
    listView = (ExpandableListView)findViewById(R.id.Exp);
   // initData();

    listAdapter = new ExpandapleListAdapter(this,listDataHeader,listHash);
    listView.setAdapter(listAdapter);
    getSSSRequest();

    private void getSSSRequest(){

    startProgress();
    String authorization = SessionHelper.getCustomerTokenWithBearer();

    Call<SSSModel> call = ApiService.apiInterface.getFAQ(authorization);
    call.enqueue(new Callback<SSSModel>() {
        @Override
        public void onResponse(Call<SSSModel> call, Response<SSSModel> response) {
            stopProgress();

            if (response.isSuccessful()){

                listDataHeader.clear();
                listHash.clear();

                if (response.body() != null && response.body().getData().size() > 0){
                   listDataHeader.addAll(response.body().getData());

                }
            }
            else {
                ApiErrorUtils.parseError(response);
            }
        }

        @Override
        public void onFailure(Call<SSSModel> call, Throwable t) {
            stopProgress();
            DialogHelper.showFailedDialog();

        }
    });

   }

   ExpandapleListAdapter.java

    public class ExpandapleListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<SSSDataModel> listDataHeader;
private HashMap<String,List<String>> listHashMap;

public ExpandapleListAdapter(Context context, ArrayList<SSSDataModel> listDataHeader, HashMap<String, List<String>> listHashMap){
    this.context = context;
    this.listDataHeader = listDataHeader;
    this.listHashMap = listHashMap;
}

@Override
public int getGroupCount() {
    return listDataHeader.size();
}

@Override
public int getChildrenCount(int i) {
    return listHashMap.get(listDataHeader.get(i)).size();
}

@Override
public Object getGroup(int i) {
    return listDataHeader.get(i);
}

@Override
public Object getChild(int i, int i1) {
    return listHashMap.get(listDataHeader.get(i)).get(i1);
}

@Override
public long getGroupId(int i) {
    return i;
}

@Override
public long getChildId(int i, int i1) {
    return i1;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
    String headerTitle = (String)getGroup(i);
    if (view == null)
    {
        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list_group,null);
    }
    TextView lbListHeader = (TextView)view.findViewById(R.id.lblistheader);
    lbListHeader.setTypeface(null, Typeface.BOLD);
    lbListHeader.setText(headerTitle);
    return view;
}

@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
    final String childText = (String)getChild(i,i1);
    if (view == null)
    {
        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list_item,null);
    }
    TextView txtListChild = (TextView)view.findViewById(R.id.listitem);
    txtListChild.setText(childText);
    return view;
}

@Override
public boolean isChildSelectable(int i, int i1) {
    return true;
}
}

Error:

E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@63f15f
                            motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@c92e7ac
                            motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@c92e7ac
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.gokhan.emasapps, PID: 20394
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gokhan.emasapps/com.example.gokhan.emasapps.SSS}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                      at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:158)
                      at android.app.ActivityThread.main(ActivityThread.java:7225)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
                      at com.example.gokhan.emasapps.ExpandapleListAdapter.getGroupCount(ExpandapleListAdapter.java:30)
                      at android.widget.ExpandableListConnector.getCount(ExpandableListConnector.java:397)
                      at android.widget.ListView.setAdapter(ListView.java:508)
                      at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:602)
                      at com.example.gokhan.emasapps.SSS.onCreate(SSS.java:52)
                      at android.app.Activity.performCreate(Activity.java:6876)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
                      at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:158) 
                      at android.app.ActivityThread.main(ActivityThread.java:7225) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Solution

  • Try it:

    private ArrayList<SSSDataModel> listDataHeader = new ArrayList<>();
    

    You have to initialize your ArrayList. I hope this answer helps you.

    Good luck!