0

I usually code stuff like this:

public void myMethod(@NonNull String sName) {
    //noinspection ConstantConditions
    if (sName == null) {
        throw new IllegalArgumentException("Name argument cannot be null");
    }
    [...]
}

I want Android Studio to preventively warn me about calls to myMethod with null parameters. However, if bad things happen and null is passed to the method, I want a clear stack trace as soon as possible, so I do the null check.

However Android Studio keeps complaining that the check will never be null. That is actually wrong. I have to keep adding //noinspection ConstantConditions to suppress the warning and clutter code.

Is there any better way to do this in java?

Reaper
  • 486
  • 3
  • 12
  • 1
    Sounds like this is an IDE inspection. When you hover over the warning, is a popup shown which lets you navigate to this inspection's settings? If so, you can probably disable it. – Slaw Jun 25 '20 at 10:14
  • Yes, but that will disable it everywhere, I don't want that. – Reaper Jun 25 '20 at 13:45
  • If you only want to suppress some instances of the warning but not others then what you're doing is the way to do it. If you want to suppress all instances of the warning, without having to modify the source file, then you need to disable or, if allowed, partially disable the inspection. – Slaw Jun 25 '20 at 22:22

0 Answers0