Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want use @Nullable annotation to eliminate NullPointerExceptions . I found some tutorials on the net, I noticed that this annotation comes from the package javax.annotation.Nullable ; but when I import it a compilation error is generated: cannot find symbol

You need to include a jar that this class exists in. You can find it here

If using Maven, you can add the following dependency declaration:

<dependency>
  <groupId>com.google.code.findbugs</groupId>
  <artifactId>jsr305</artifactId>
  <version>3.0.2</version>
</dependency>

and for Gradle:

dependencies {
  testImplementation 'com.google.code.findbugs:jsr305:3.0.2'
                Why is Google (especially its findbugs artifact) providing types that belong to the javax package? Isn't there an artifact with a javax-prefixed groupId that provides this type?
– Andrew Swan
                Apr 22, 2016 at 4:02
                @AndrewSwan it seems that the author chose the groupId of com.google.code.findbugs because it was being hosted on Google's code hosting solution
– matt b
                Jan 3, 2017 at 17:11
                Google-findbugs is the reference implementation of the jsr305, thus they are kind of allowed to use the javax-packageName I guess.
– icyerasor
                Dec 12, 2019 at 13:36
    <groupId>com.google.code.findbugs</groupId>
    <artifactId>jsr305</artifactId>
    <version>3.0.0</version>
</dependency>
dependencies {
    compile group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.0'
                I tried , and got error: Could not find method compile() for arguments [{group=com.google.code.findbugs, name=jsr305, version=3.0.0}] on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@7b35fdf2.
– kamal
                Jul 1, 2016 at 17:54
                @kamal with the above example, it is generally assuming you have applied the 'java' (or other) plugin that has already created the compile configuration.
– mkobit
                Nov 15, 2016 at 17:08
                Unable to import javax.annotation.Nullable; I have jsr250-api-1.0.jar in my classpath. I even tried updating maven pom <dependency>     <groupId>com.google.code.findbugs</groupId>     <artifactId>jsr305</artifactId>     <version>3.0.0</version> </dependency>  Eclipse gave errors: Missing artifact com.google.code.findbugs:jsr305:jar:3.0.0  Failure to transfer com.google.code.findbugs:jsr305:jar:3.0.0
– Sam-T
                Jan 12, 2017 at 19:12
                It finally worked jsr305-3.0.1.jar - some eclipse cp issue. Yes finally it compiles after external jar import into eclipse
– Sam-T
                Jan 12, 2017 at 19:53
                Because the jar is not needed at runtime, use compileOnly group... instead of just compile group...
– Renato
                Mar 10, 2018 at 9:14

JSR-305 is a "Java Specification Request" to extend the specification. @Nullable etc. were part of it; however it appears to be "dormant" (or frozen) ever since (See this SO question). So to use these annotations, you have to add the library yourself.

FindBugs was renamed to SpotBugs and is being developed under that name.

For maven this is the current annotation-only dependency (other integrations here):

<dependency>
  <groupId>com.github.spotbugs</groupId>
  <artifactId>spotbugs-annotations</artifactId>
  <version>4.2.0</version>
</dependency>

If you wish to use the full plugin, refer to the documentation of SpotBugs.

The spotbugs-annotations module has a compile dependency <dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>jsr305</artifactId> <version>3.0.2</version> </dependency>, see mvnrepository.com/artifact/com.github.spotbugs/… . Thus, if you need only the @Nullable, it is better to use directly the google findbugs dependency. – Julien Kronegg Aug 17, 2022 at 11:06

If anyone has this issue when building a Maven project created in IntelliJ IDEA externally, I used the following dependency instead of the answer:

<dependency>
  <groupId>org.jetbrains</groupId>
  <artifactId>annotations</artifactId>
  <version>15.0</version>
</dependency>

Using this will allow the project to build on IntelliJ IDEA and by itself using Maven.

You can find it here.

beware that org.jetbrains:annotations:15.0 provides @org.jetbrains.annotations.Nullable not @javax.annotation.Generated. This could be a problem in case you use some code generators as openapi-generator-maven-plugin. – danidemi Jan 29, 2020 at 15:11 Including Guava for just for this would be overkill, and to be avoided due to their poor versioning and tendency to make backwards-incompatible changes. – Matthew Read Dec 3, 2020 at 17:37 @Matthew Read Guava shouldn't be avoided. There are features that aren't in plain Java - immutable collections(well it is in java >=9, but this one is better), mapMakers, splitters ....and so on. I think that adding guava brings good stuff. Maybe it is overkill just for this, but still it is better than using "findbugs"or "jetbrains" annotations. – John Tribe Dec 10, 2020 at 8:02 I agree that Guava can be useful for other things, but that doesn't mean that it makes sense to bring in a large library for something tiny. com.google.code.findbugs:jsr305 is named poorly, but it is the official reference implementation. – Matthew Read Dec 15, 2020 at 20:57

In the case of Android projects, you can fix this error by changing the project/module gradle file (build.gradle) as follows:

dependencies { implementation 'com.android.support:support-annotations:24.2.0' }

For more informations, please refer here.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.