Why does Android Studio Lint warn when using TypedArray?

119 Views Asked by At

I have this code:

TypedArray mAllowedCountries = application.getResources().obtainTypedArray(R.array.allowed_countries);

    for (int index = 0; index < mAllowedCountries.length(); index++) {
        if(mAllowedCountries.getString(index) != null) {
            if (mAllowedCountries.getString(index).toUpperCase().equals(addressComponentCountry.short_name.toUpperCase())) {
                supported = true;
                break;
            }
        }
    }

I get a Android Studio Lint warning about

Method invocation 'toUpperCase' may produce 'java.lang.NullPointerException'

How can I fix this to get rid of the Lint warning?

1

There are 1 best solutions below

1
Onik On BEST ANSWER

The following should remove the Lint warning:

final String address = addressComponentCountry.short_name.toUpperCase();
String tempStr;
for (int index = 0; index < mAllowedCountries.length(); index++) {
    tempStr = mAllowedCountries.getString(index);
    if(tempStr != null && tempStr.toUpperCase().equals(address)) {
        supported = true;
        break;
    }
}