androidandroid-studiolinttypedarray

Why does Android Studio Lint warn when using TypedArray?


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?


Solution

  • 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;
        }
    }