So I'm getting this error from the nullness checker
> Task :compileJava
/Users/calebcushing/IdeaProjects/ppm/scaf/src/main/java/com/xenoterracide/scaf/PebbleTemplateProcessor.java:94: error: [argument.type.incompatible] incompatible argument for parameter obj of requireNonNull.
var console = Objects.requireNonNull( System.console() );
^ ^
this is complaining that System.console()
could be null, which upon reading the javadoc is true. So I wrapped it in Objects.requireNonNull()
now it's complaining that the argument to requireNonNull
can't be null, which is obviously not true.
How do I tell NullnessChecker
to ignore Objects.requireNonNull()
? I'm fine with NPE's that are explicit, it's just the accidental ones I don't want. I believe checker already ships with a stub for this.
Edit: The below answers were written for the original post, which had 3 unrelated questions in it. (The poster later edited the original post.) I have no clue why this answer was downvoted, since it accurately answers all 3 questions, and gives links for additional information.
Please ask one question per post.
This is answered in the introduction to the Checker Framework manual.
The goal of the Nullness Checker is to warn you if your program might throw a
NullPointerException
. If the argument torequireNonNull()
isnull
, then your program throws aNullPointerException
. Therefore, passingnull
torequireNonNull()
leaves your program no more correct than passingnull
to any other routine that might dereference it, and the Nullness Checker warns about it.You tried to silence the warning by writing
requireNonNull()
. As noted above, this has no effect: the program still throwsNullPointerException
. Instead, it is better to correct the underlying problem. Your program should check for null and issue a user-friendly message rather than crashing -- whether the crash is in your own code or inrequireNonNull()
.This is also answered in the introduction to the Checker Framework manual.
The Nullness Checker reads annotations, not English Javadoc comments. In order for the Nullness Checker to know that
toBoolean()
can take a null argument, its signature oftoBoolean()
needs to be annotated as:As explained in the manual, you can write that annotation so that the Nullness Checker will use it. You can also contribute commons-lang annotations back to the community, so that others can also benefit from them.
You need to write
@SuppressWarnings
where the warning is being issued, which is on the declaration of theApplication
class. You wrote@SuppressWarnings
on a different line of your program.