0

Below is the android app firebase crash stack trace and I'm unable identify the cause of that crash. This might be a stupid question but this crash count is spiking and I need a fix. What exactly is this == null means?

Fatal Exception: java.lang.NullPointerException: this == null at com.pure.indosat.care.fragments.buy.BuyPackagesFragment.lambda$parsePackagesCategories$1(BuyPackagesFragment.java:175) at com.pure.indosat.care.fragments.buy.BuyPackagesFragment.lambda$parsePackagesCategories$1$BuyPackagesFragment(BuyPackagesFragment.java:11) at com.pure.indosat.care.fragments.buy.-$$Lambda$BuyPackagesFragment$CcYVviVqJQdnZlEmW30Nw6EKMQA.run(-.java:11) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7948) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

I am not getting any crashes in the BuyPackagesFragment java class when I tested it. Below code is the one at line no. 175 as per crash

private void parsePackagesCategories(JSONObject jsonObject) {

    Handler handler = new Handler();

    handler.post(() -> {

        Iterator<String> keys = jsonObject.keys();

        tabs.clear();

        while (keys.hasNext()) {

            String key = keys.next();

            if (jsonObject.opt(key) instanceof JSONObject) {

                JSONObject jsonObject1 = (JSONObject) jsonObject.opt(key);

                if (jsonObject1 != null) {

                    String title = jsonObject1.optString("title");

                    JSONArray jsonArray = jsonObject1.optJSONArray("categories");

                    TabInfo item = new TabInfo();

                    **Line No: 175 : JSONArray categoriesArray = mActivity.getJSONArray(jsonObject1, "categories");**

                    try {
                        String id = categoriesArray.length() > 0
                                ? categoriesArray.getJSONObject(0).optString("category_id")
                                : "";

                        item.setId(id);
                    } catch (JSONException e) {
                        TraceUtils.logException(e);
                    }

                    item.setTitle(title);

                    item.setData(jsonArray);

                    item.setOrder(jsonObject1.optInt("order"));

                    tabs.add(item);

                }

            }

        }

        Collections.sort(tabs, (z1, z2) -> {
            if (z1.getOrder() > z2.getOrder())
                return 1;
            else if (z1.getOrder() < z2.getOrder())
                return -1;
            return 0;
        });

        parseTabsData();

    });

}

Here I thought "mActivity" might be null but if that's the case then the crash should happen at the beginning itself as per the code.

If someone can tell me what exactly this this == null means then it would be helpful.

Thank you.

public JSONArray getJSONArray(JSONObject jsonObject, String key) {

    JSONArray jsonArray = new JSONArray();

    if (jsonObject == null || TextUtils.isEmpty(key)) return jsonArray;

    try {

        if (jsonObject.opt(key) instanceof JSONObject) {

            jsonArray.put(jsonObject.optJSONObject(key));

            return jsonArray;
        }

        if (jsonObject.opt(key) instanceof JSONArray) {

            jsonArray = jsonObject.optJSONArray(key);

            return jsonArray;

        }

    } catch (Exception e) {

      TraceUtils.logException(e);

    }

    return jsonArray;
}
sHaRkBoY
  • 481
  • 4
  • 8
  • Since mActivity is not null, according to you, the only other option would be jsonObject1 being null. I'd assume that maybe it got missed somewhere along your code? Is there a case in which you might fail to initialize it with any value? – Dan Baruch Jul 26 '21 at 14:05
  • I have updated question. Please check that jsonObject1 null check is already being done before coming to that block. @DanBaruch – sHaRkBoY Jul 26 '21 at 14:08
  • Is it possible that jsonObject1 does not contain "category" then? – Dan Baruch Jul 26 '21 at 14:10
  • mActivity.getJsonArray() function assures that we avoid any such null exceptions and give empty JsonArray instead. I have added that function code also. – sHaRkBoY Jul 26 '21 at 14:12
  • In that case, is it possible to see getJsonArray implementation? I highly doubt it's there since if the exception was thrown there you would've seen it in the stacktrace, but you can also add a mActivity != null condition to your if clause and see what happens then – Dan Baruch Jul 26 '21 at 14:14
  • 1
    Which of those lines is the 175? Could you flag it with a code comment please. – Augusto Carmo Jul 26 '21 at 14:15
  • I have added "Line no 175" before the line in code. Please check it @AugustoCarmo – sHaRkBoY Jul 26 '21 at 14:17
  • What happens if you change it to: jsonObject1.getJSONArray("categories") – Augusto Carmo Jul 26 '21 at 14:20
  • Yeah, sounds like a threading issue. Not enough code here to give you a proper answer though – Blundell Jul 26 '21 at 14:20
  • Yes that function is indeed running in a handler thread. I have updated question again. So, should I use Handler with mainLooper thread instead? – sHaRkBoY Jul 26 '21 at 14:22
  • Why are you posting that bit of code? Just a guess, you could use: `if(activity.isFinishing()` to quit early though – Blundell Jul 26 '21 at 14:24
  • @AugustoCarmo this crash is not happening in the devices I tested with current live build code which is same as above code. So, not able to identify crash – sHaRkBoY Jul 26 '21 at 14:24
  • @Blundell That makes sense. Yes I will do that and see if it's fixed in next build. I do this for dialogs and everything else but I will add the check here too and see how it goes. Thank you for your responses guys. – sHaRkBoY Jul 26 '21 at 14:26
  • @Andy I have added code with thread call. Please see if you can identify the issue in code. – sHaRkBoY Jul 26 '21 at 14:30

0 Answers0