1

我有这个代码:

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

我收到关于Android Studio Lint的警告

方法调用“toUpperCase”可能会产生“java.lang.NullPointerException”

如何解决此问题以消除Lint警告?

4

1 回答 1

1

以下应删除Lint警告:

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;
    }
}
于 2018-03-15T20:23:28.927 回答