CountryCodePickerProject icon indicating copy to clipboard operation
CountryCodePickerProject copied to clipboard

Fatal Exception: java.lang.NullPointerException getNameCode() on a null object reference

Open SerggioC opened this issue 3 years ago • 6 comments

Fatal Exception: java.lang.NullPointerException Attempt to invoke virtual method 'java.lang.String com.hbb20.CCPCountry.getNameCode()' on a null object reference com.hbb20.CountryCodePicker.setCustomDefaultLanguage (CountryCodePicker.java:1064) com.hbb20.CountryCodePicker.changeDefaultLanguage (CountryCodePicker.java:1996)

Please add null check in selectedCCPCountry.

SerggioC avatar May 11 '21 08:05 SerggioC

Any updates on this fix?

SerggioC avatar May 26 '21 22:05 SerggioC

It should go away with 2.5.2? Is it still happening in 2.5.2?

hbb20 avatar May 29 '21 05:05 hbb20

Hi, It isn't solved because you added null check in some other variable that wasn't null. Null check should be in selectedCCPCountry variable. You can then add a default value in the case it is null.

SerggioC avatar May 29 '21 17:05 SerggioC

Right. I missed one variable in 2.5.1 but then after you pointed out, I put another check in https://github.com/hbb20/CountryCodePickerProject/pull/488 and released 2.5.2. If it's still happening, not sure what's wrong.

hbb20 avatar May 29 '21 17:05 hbb20

Ok #488 should solve the issue. So probably I'm still getting reports from old versions. I'll keep an eye on that.

Just checked crashlytics again and I have another user with 14 crashes with:

CountryCodePicker.java line 1650
com.hbb20.CountryCodePicker.getDefaultCountryNameCode
Fatal Exception: java.lang.NullPointerException
Attempt to read from field 'java.lang.String com.hbb20.CCPCountry.nameCode' on a null object reference
com.hbb20.CountryCodePicker.getDefaultCountryNameCode (CountryCodePicker.java:1650)
com.hbb20.CountryCodePicker.resetToDefaultCountry (CountryCodePicker.java:1657)
com.hbb20.CountryCodePicker.detectLocaleCountry (CountryCodePicker.java:2328)
public String getDefaultCountryNameCode() {
        return getDefaultCountry().nameCode.toUpperCase();    
}

I guess this function is returning null somehow. defaultCCPCountry can be null sometimes in situations I can't explain...

private CCPCountry getDefaultCountry() { 
       return defaultCCPCountry;    
}

Best solution would be to always have a default country in the defaultCCPCountry instance variable, (maybe add a World country for this situations?) and check for null in this function, so it doesn't crash where it's called.

Thanks

SerggioC avatar May 29 '21 18:05 SerggioC

Hi, Check out the pull request #489 to see if we can solve this.

Thanks.

SerggioC avatar May 31 '21 22:05 SerggioC