I have this code:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (position < getItemCount()) {
holder.bind(mServiceInfo.agencies.get(position));
}
}
@Override
public int getItemCount() {
return mServiceInfo != null && mServiceInfo.agencies != null ? mServiceInfo.agencies.size() : 0;
}
Android Studio warns me in the onBindViewHolder
method, saying that my mServiceInfo.agencies.get
call might raise a NPE.
There is no way this can happen, because position
is always a positive integer. I've even changed the test condition to: position >= 0 && position < getItemCount()
but the warning is still shown.
Here's what it looks like:
How can I configure the IDE not to raise a warning here?
Please note that:
- I don't want to disable the lint check or disable it for this statement. I know that lint checks can be disabled, that's not the point of my question here. Also, I like to avoid disabling checks because in maintenance the code can be changed and the comment asking the IDE not to care about one check may remain, which would cause undesired/unexpected behavior.
- Apart from concurrency, there is no condition which would cause an NPE in the
onBindViewHolder
, so the IDE warning is not valid here - I do care about warnings, especially when it's about checking NPEs, because they can easily show where I did wrong and avoid a possible bug. Warnings are very important and I tend to have zero warnings when compiling, especially about when they detect possible bugs.
- It is possible to change how the IDE behaves vs. NPE, this kind of answer is exactly what I had in mind when posting this question