163

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

MrSmith42
  • 9,961
  • 6
  • 38
  • 49

10 Answers10

199

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'
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
David
  • 19,577
  • 28
  • 108
  • 128
40

The artifact has been moved from net.sourceforge.findbugs to

<dependency>
    <groupId>com.google.code.findbugs</groupId>
    <artifactId>jsr305</artifactId>
    <version>3.0.0</version>
</dependency>
jan
  • 2,741
  • 4
  • 35
  • 56
30

If you are using Gradle, you could include the dependency like this:

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.0'
}
mkobit
  • 43,979
  • 12
  • 156
  • 150
  • 1
    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 01 '16 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](https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependency_management). – mkobit Nov 15 '16 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 ` com.google.code.findbugs jsr305 3.0.0 ` 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 '17 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 '17 at 19:53
  • Because the jar is not needed at runtime, use `compileOnly group...` instead of just `compile group...` – Renato Mar 10 '18 at 09:14
  • But is it really *not* needed at runtime? The RetentionPolicy of the @Nullable annotation is set to RUNTIME after all. As the jsr305.jar only includes annotations / interfac-defs the impact on your bundled jar is negligible. – icyerasor Dec 12 '19 at 13:38
17

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.

BotOfWar
  • 588
  • 5
  • 14
  • The `spotbugs-annotations` module has a compile dependency ` com.google.code.findbugs jsr305 3.0.2 `, see https://mvnrepository.com/artifact/com.github.spotbugs/spotbugs-annotations/4.7.1 . Thus, if you need only the `@Nullable`, it is better to use directly the google findbugs dependency. – Julien Kronegg Aug 17 '22 at 11:06
8

In case someone has this while trying to compile an Android project, there is an alternative Nullable implementation in android.support.annotation.Nullable. So take care which package you've referenced in your imports.

Janis Peisenieks
  • 4,938
  • 10
  • 55
  • 85
5

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.

crawton
  • 199
  • 1
  • 5
  • 5
    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 '20 at 15:11
4

you can add latest version of this by adding following line inside your gradle.build.

implementation group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'
Jay Dangar
  • 3,271
  • 1
  • 16
  • 35
2

I am using Guava which has annotation included:

(Gradle code )

compile 'com.google.guava:guava:23.4-jre'
John Tribe
  • 1,407
  • 14
  • 26
  • 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 03 '20 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 '20 at 08:02
  • 2
    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 '20 at 20:57
1

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.

KPandian
  • 1,148
  • 9
  • 8
1

For gradle build I used compile('com.google.code.findbugs:jsr305:3.0.2') . In case of test can use testCompile('com.google.code.findbugs:jsr305:3.0.2').