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.
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:
points
variablemCurrentUser.getInt(Constants.TABLE_POINTS)
value to itInteger
object with AppSingleton.sPointsLookupMap.get(pointsKey)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With