Can't find @Nullable inside javax.annotation.*

JavaAnnotationsNullpointerexceptionNullableNull Pointer

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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'
}

Solution 2 - Java

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>

Solution 3 - Java

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'
}

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

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.

Solution 7 - Java

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'

Solution 8 - Java

I am using Guava which has annotation included:

(Gradle code )

compile 'com.google.guava:guava:23.4-jre'

Solution 9 - Java

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.

Solution 10 - Java

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').

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2354035View Question on Stackoverflow
Solution 1 - JavaDavidView Answer on Stackoverflow
Solution 2 - JavajanView Answer on Stackoverflow
Solution 3 - JavamkobitView Answer on Stackoverflow
Solution 4 - JavaBotOfWarView Answer on Stackoverflow
Solution 5 - JavaJanis PeisenieksView Answer on Stackoverflow
Solution 6 - JavacrawtonView Answer on Stackoverflow
Solution 7 - JavaJay DangarView Answer on Stackoverflow
Solution 8 - JavaJohn TribeView Answer on Stackoverflow
Solution 9 - JavaKPandianView Answer on Stackoverflow
Solution 10 - JavaDinushika RathnayakeView Answer on Stackoverflow