0

I’m using retrofit api. Please check the screenshot below: I have category and sub category list in the dialog. On clicking “Ok” button, I have to fetch main category id, subcategory id , and sub category price. I used sparseBooleanArray. I’m able to fetch main_category id, but I get null pointer exception when try to fetch sub_category_id. Please help. enter image description hereAttached code below:

MainAdapter:

 holder.select_category.setTag(position);
        holder.select_category.setChecked(mSparseBooleanArray.get(position));
        holder.select_category.setOnCheckedChangeListener(mCheckedChangeListener);

        sublist = new ArrayList<>();
        sublist=chat_list.get(position).getSubCategory();
        subcategoryAdapter = new SubCategoryAdapter(context, sublist);
        holder.recyclerView.setAdapter(subcategoryAdapter);
        holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));

        holder.category.setText(chat_list.get(position).getMainCategoryName());


    }
    CompoundButton.OnCheckedChangeListener mCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);

        }
    };
    public ArrayList<String> getCheckedItems() {
        ArrayList<String> mTempArry = new ArrayList<String>();

        for(int i=0;i<chat_list.size();i++) {
            if(mSparseBooleanArray.get(i)) {
                mTempArry.add(chat_list.get(i).getMainCategoryId());
                //mTempArry.add(sublist.get(i).getSubCategoryId());
                Log.e("MAINID",chat_list.get(i).getMainCategoryId());
            }
        }

        return mTempArry;
    }

SubAdapter:

  public void onBindViewHolder(final DataObjectHolder holder, final int position) {

        holder.subcategory.setText(sublist.get(position).getSubCategoryName());
        Log.e("Sub Cat Name",sublist.get(position).getSubCategoryName());

        holder.subcat.setTag(position);
        holder.subcat.setChecked(mSparseBooleanArray.get(position));
        holder.subcat.setOnCheckedChangeListener(mCheckedChangeListener);
        cost_per_hr=holder.price.getText().toString();


    }

    CompoundButton.OnCheckedChangeListener mCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
        }
    };
    public ArrayList<String> getSubCheckedItems() {
        ArrayList<String> mTempArry = new ArrayList<String>();

        for(int i=0;i<sublist.size();i++) {
            if(mSparseBooleanArray.get(i)) {
                Log.e("TAG","INSIDE SUB");
                mTempArry.add(sublist.get(i).getSubCategoryId());
                mTempArry.add(cost_per_hr);
                Log.e("SUBID",sublist.get(i).getSubCategoryId()+"::"+cost_per_hr);
            }
        }

        return mTempArry;
    }
    @Override
    public int getItemCount() {
        return sublist.size();
    }

Activity:

 submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                category.dismiss();

                ArrayList<String> selectedItems = categoryAdapter.getCheckedItems();
                try {
                    try {
                        for (int i = 0; i <= selectedItems.size(); i++) {

                            Log.e("Main", selectedItems.get(i));

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }

                ArrayList<String> selectedItems_one = subCategoryAdapter.getSubCheckedItems();
                try {
                    for (int i = 0; i <= selectedItems_one.size(); i++) {

                        Log.e("Sub", selectedItems_one.size()+"");

                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        });

On clicking submit button, i get nullpointer exception for getSubcheckedItems() method.

Error:

     java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.app.qn.adapter.SubCategoryAdapter.getSubCheckedItems()' on a null object reference
        at com.app.qn.activity.EditProfileActivity$3.onClick(EditProfileActivity.java:303)
        at android.view.View.performClick(View.java:5675)
        at android.view.View$PerformClick.run(View.java:22651)
        at android.os.Handler.handleCallback(Handler.java:836)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:208)
        at android.app.ActivityThread.main(ActivityThread.java:6267)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
2019-04-17 17:48:13.793 19440-21355/com.app.qn E/NativeCrypto: ssl=0x774a444680 cert_verify_callback x509_store_ctx=0x772d7e8a28 arg=0x0
2019-04-17 17:48:13.793 19440-21355/com.app.qn E/NativeCrypto: ssl=0x774a444680 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
Elackya
  • 59
  • 1
  • 7
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Selvin Apr 17 '19 at 12:22
  • @Selvin: I know what is null pointer exception. My question is diffrent . please check my question carefully – Elackya Apr 17 '19 at 12:24
  • `ArrayList selectedItems_one = subCategoryAdapter.getSubCheckedItems();` , `subCategoryAdapter` is null at this line I guess . add full activity and exception – Manohar Apr 17 '19 at 12:24
  • @ManoharReddy: Please check the exception, I am unable to post full activity so posted only click event where array is called – Elackya Apr 17 '19 at 12:29
  • If the line 303 on EditProfileActivity is `ArrayList selectedItems_one = subCategoryAdapter.getSubCheckedItems();` then it means `subCategoryAdapter` is not initialized , we cannot know if you initialized it properly or not with out the full activity code . – Manohar Apr 17 '19 at 12:32
  • The question is *What is a NullPointerException, **and how do I fix it?*** so it is duplicate ... it should be easy ... 1. find out what is null 2. find out why 3. fix it ... point 2 need some java's basic knowladge - like variable scopes and some android's basics like component's lifecycle – Selvin Apr 17 '19 at 12:33
  • @ManoharReddy Check SubAdapter class. I have initialized the array. Does that make sense? But i haven't initialized in Activity. Correct me if am wrong please – Elackya Apr 17 '19 at 12:37
  • @Selvin: Alright, if my question is silly to your knowledge downvote my question which you already did. Don't give unfriendly comments please – Elackya Apr 17 '19 at 12:38
  • @Elackya sorry it might hurt you but selvin is right you have no idea what nullpointer exception is or atleast how to fix it . – Manohar Apr 17 '19 at 12:40
  • @ManoharReddy: So i didn't initialize the array in activity. That's the problem? – Elackya Apr 17 '19 at 12:42
  • @ManoharReddy has right ... without MCVE is hard to to guess what is `subCategoryAdapter` in given context ... *I have initialized the array. Does that make sense? But i haven't initialized in Activity. Correct me if am wrong please* ... you are right ... variable scopes (or .. if `MainAdapter` is inner non static class - you are wrong - and it is because timing is wrong(order of code execution)) ... – Selvin Apr 17 '19 at 12:43
  • You didn't initialize `subCategoryAdapter` in activity that the problem . you initialized in MainAdapter . you cannot use it in activity – Manohar Apr 17 '19 at 12:43
  • @Selvin,@Manohar: Thanks, I will check – Elackya Apr 17 '19 at 12:46
  • When you are getting this exception? On clicking "Designer" or any other option ? – Exigente05 Apr 17 '19 at 13:07
  • @Exigente05: I don't get exception on clicking checkboxes, I get when clicking ok button – Elackya Apr 21 '19 at 08:55

1 Answers1

1

I guess you are getting null pointer exception exception when you have selected other option except "Designer" It's because, from your screenshot i can see only designer option have sub category and you have initialized it based on having items.

For other options except "Designer" you don't have any sub category, and your sub category adapter is not being initialized.

This is causing null pointer exception

Your null pointer is saying your sub adapter is null.

To handle nullpointerexception firstly check your sub category adapter is null or not then do other operations.

Surely, your subCategoryAdapter object is not being initialized at the time you are clicking submit button. Make sure your adapter initialization is within the scope.

Exigente05
  • 2,161
  • 3
  • 22
  • 42