-1

Below is the code invoked onItemSelected in a spinner. At times I am getting a null pointer exception even when the app is closed. The image of the bug is attached in this post.

        /**
         * Store the selected product
         * @param parent
         * @param view
         * @param position
         * @param id
         */
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            QuickListItem selectedSpinnerProduct = productAdapter.getItem(position);

            if (selectedSpinnerProduct != null) {
                mgrApp.storeSharedPrefLong(ConstantsQuickList.SHARED_PREF_PRODUCT_ID,
                        selectedSpinnerProduct.getId());
            }

            SpannableStringBuilder builder = getPendingCallCount(products,
                    selectedSpinnerProduct);

            ((TextView) view).setText(builder, TextView.BufferType.SPANNABLE);

            //Hide the performance tracker as product will change
            ViewProspects.crdvPerformanceTracker.setVisibility(View.GONE);
            populateSalesTracker(typeOfOperation,ConstantsProspects.INVALID_STRING);

        }

Application cashed bug image

  • For the time being i have put the code (TextView) view).setText(builder, TextView.BufferType.SPANNABLE); in a try catch block. – Yash Sharma May 16 '20 at 11:08

1 Answers1

0

If selectedSpinnerProduct is null, you will have issue. So check everywhere selectedSpinnerProduct is used that it is null or not.

 if (selectedSpinnerProduct != null) {
                mgrApp.storeSharedPrefLong(ConstantsQuickList.SHARED_PREF_PRODUCT_ID,
                        selectedSpinnerProduct.getId());
 SpannableStringBuilder builder = getPendingCallCount(products,
                    selectedSpinnerProduct);

            ((TextView) view).setText(builder, TextView.BufferType.SPANNABLE);
            }
MMG
  • 3,226
  • 5
  • 16
  • 43
  • 2
    Instead of spoonfeeding code to the OP, please consider explaining how your code is different from the original code. – Edric May 16 '20 at 11:25
  • Thanks i guess, I have tried this approach too. – Yash Sharma May 16 '20 at 11:36
  • If your problem is solved, check the correct answer please @YashSharma – MMG May 16 '20 at 11:40
  • It's not yet being solved. Implemented the changes will test and update the correct solution. Thnx – Yash Sharma May 16 '20 at 12:16
  • What is the problem now? @YashSharma – MMG May 16 '20 at 12:19
  • Hi, yes, I add description to my answer. Of course the question is closed. @Edric – MMG May 17 '20 at 07:00
  • There is no problem. Just releasing the code this week and will observe the error. if that comes back will let you guys know of it. else will close the question. Thanks for the suggestions. The crash does not happen all the time. Its a random event. – Yash Sharma May 17 '20 at 09:47