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.
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.