For a long time already IntelliJ IDEA can detect probable NPE’s in your code. You need to annotate your methods/fields/variables with @Nullable/@NotNull
annotations and IntelliJ IDEA will highlight possible contract violations. Good. But the annotations only begin to work when there is enough of them in the code. You do need to annotate your code to get the benefits.
IntelliJ IDEA 10 can do it for you.
Just run Analyze | Infer Nullity… and choose a scope where you want annotations to be inferred. IntelliJ IDEA uses many rules to analyze nullity and make a decision. Here are just 2 examples:
1) The IDE detects parameters that are used without checking for null
:
and presumes that they are @NotNull
:
2) Variables that are checked for null
are assumed as @Nullable
, etc.
As a result you can benefit from static code analysis without paying an entrance fee.

Looks like the second screenshot should include “@Nullable” for the bar() instead of @NotNull?
This is a great feature!
Is there any chance you could support the equivalent annotations defined in JSR-305 (in the javax.annotations namespace), rather than requiring the annotations in the org.jetbrains.annotations namespace?
This would allow better integration with third-party tools like FindBugs, and I think organisations are happier to have ‘import javax.annotations’ in their code than ‘import org.jetbrains.annotations’, especially for dev-teams which use a mixture of IDEs.
I do agree with Jon. It really is a brillaient feature, but being on an eclipse project, i won’t import a vendor-specific annotation all over the place even though i really lové intellij…
+1 for this. We are not using this IDEA feature because of the jetbrains imports. If the javax.annotations would be supported, we would use it.
@KIR
nope, @Nullable would produce NPE (null.substring(0))
Another +1. It should be very easy for IDEA to support javax.annotations instead of JetBrains specific annotations.
I understand that JSR-305 is not final and all, but this is just a compile time dependency, and a possible impact from future changes in those annotations is pretty small.
+1
Forcing users to depend on a jetbrains jar (and include it in their maven repository, etc) does not work in practice.
Please, vote for http://youtrack.jetbrains.net/issue/IDEA-19564
Thank you
yes. Strongly agreed that Intellij should refrain adding such features that require proprietary library.
@Anna: please take into account that there are several similar issues when you evaluate the popularity of this feature:
http://youtrack.jetbrains.net/issue/IDEABKL-4959
http://youtrack.jetbrains.net/issue/IDEA-10924
Just counting votes on IDEA-19564 might not be accurate.
+1 to allowing support for the javax.annotation.Nullable version.
Is there a way to use class level defaults for this? I’d like for everything to be @NotNull by default unless specified to be @Nullable.
Would it be possible to enable to infer nullity for libraires used in a project? Would help a lot when using some libraries with sources.
Could you please patch code and throw IllegalArgument / AssertionErrors for javax.validation.constraints.NotNull as it done for jetbrains NotNull?