Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird parse crash android - Attempt to invoke virtual method 'int java.lang.Integer.intValue()

So basically I have tried everything but I keep getting this crash. It crashes only sometimes. Here is my code:

if (mCurrentUser != null && mCurrentUser.containsKey(Constants.TABLE_POINTS)) {
    int points = mCurrentUser.getInt(Constants.TABLE_POINTS) + AppSingleton.sPointsLookupMap.get(pointsKey);
                            mCurrentUser.put(Constants.TABLE_POINTS, points);
                            mCurrentUser.saveInBackground();
}

Stacktrace:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
at com.titlesource.ts_foodsource.fragments.KitchenFragment$10.done(KitchenFragment.java:654)
at com.titlesource.ts_foodsource.fragments.KitchenFragment$10.done(KitchenFragment.java:649)
at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:115)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Now I found that the intValue() is being used inside the getInt() method (Inside the ParseObject class):

 public int getInt(String key) {
        Number number = this.getNumber(key);
        return number == null?0:number.intValue();
    }

Why am I getting this exception. I have tried everything. Added null checks and also verified that the field is actually present in the mCurrentUser object but all in vain.

If down voting this question, please leave a reason.

like image 710
android_eng Avatar asked Jan 15 '16 13:01

android_eng


2 Answers

You have an Integer which is null, which you are trying to unbox (making a simple int from it).

Since the the getInt(String key) method of ParseObject already checks if it is null, the exception is not happening there.

Instead, it will be at AppSingleton.sPointsLookupMap.get(pointsKey), so you are trying to get an object from the map which does not exist.

Make sure it exists, or add correct error handling to that part of the code.

like image 130
Daniel Zolnai Avatar answered Oct 19 '22 04:10

Daniel Zolnai


Replace your code with this:

if (mCurrentUser != null && mCurrentUser.containsKey(Constants.TABLE_POINTS)) {
    int points = mCurrentUser.getInt(Constants.TABLE_POINTS);
    Integer theOtherVariable = AppSingleton.sPointsLookupMap.get(pointsKey);
    if (theOtherVariable != null)
        points += theOtherVariable;
    mCurrentUser.put(Constants.TABLE_POINTS, points);
    mCurrentUser.saveInBackground();
}

It should fix the problem and make the code Exception-safe :-)

The first line (after the if) did something like this:

  1. You create the points variable
  2. You assign the mCurrentUser.getInt(Constants.TABLE_POINTS) value to it
  3. You obtain an Integer object with AppSingleton.sPointsLookupMap.get(pointsKey)
  4. You add the obtained Integer object to points, obtaining its value with .intValue() call.

All this happened behind the scenes in this one line of code and if the obtained Integer object was a null, the Exception was thrown.

like image 25
Kelevandos Avatar answered Oct 19 '22 02:10

Kelevandos