android_eng
android_eng

Reputation: 1370

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.

Upvotes: 5

Views: 19668

Answers (4)

O-9
O-9

Reputation: 1779

I just got this error and that's why I'm here. Like said in the accepted answer, code is trying to unbox Integer to int. And will crash if it's NULL

my code was:

private Integer age_limit;

// ...

public int getAge_limit() {
    return age_limit;
  }

so changed it to this

public Integer getAge_limit() {
    return age_limit;
  }

Never knew this would be possible (different data types without any warnings).

Upvotes: 0

Leo240
Leo240

Reputation: 786

Sometimes it can happen when you pass array of type Integer without final keyword to the anonymous inner class (for OnClickListener in my case).

Upvotes: 0

Kelevandos
Kelevandos

Reputation: 7082

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.

Upvotes: 2

Daniel Zolnai
Daniel Zolnai

Reputation: 16920

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.

Upvotes: 14

Related Questions