avoiding a possible null in android
-
11-12-2019 - |
Question
i have a situation where i may encounter a possible null, and i don't want my program to crash. it is only happening for a small % of users and i'm not entirely sure why, but i know the section of code that is doing it. basically the isAchieved()
is a check that should return a boolean value, but in a few cases it is returning a null. this is part of a scoreloop library and i have a suspicion the bug might be with their code.
rather than getting a force close and exiting the program is there a way to check if this value is going to return a null and simply skip over this section of code if that is the case?
if (_achievements[ltc].isAchieved()) {
// Do stuff for 99.9% of users here, but skip if .isAchieved() would force close
}
Solution
boolean checker=false;
try{
checker=_achievements[ltc].isAchieved();
}
catch(NullPointerException e){
}
if (checker) {
//content
}
Did you think about doing that?.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow