26

When I use the Java 8 method reference double colon operator (::) with new operator (e.g. MyType::new), I get this error in Eclipse of Spring Tool suite (STS):

The type org.eclipse.jdt.annotation.NonNull cannot be resolved. It is indirectly referenced from required .class files

How to get rid of this error?

luboskrnac
  • 21,083
  • 9
  • 71
  • 84

3 Answers3

19

Error description is provided in Stephan Herrmann's comment. There is open Eclipse issue to make this issue more user friendly.

Solution is to include following dependency:

<dependency>
    <groupId>org.eclipse.jdt</groupId>
    <artifactId>org.eclipse.jdt.annotation</artifactId>
    <version>2.0.0</version>
</dependency>
luboskrnac
  • 21,083
  • 9
  • 71
  • 84
  • 3
    Right, the error indicates that the Eclipse project is configured to perform annotation based null analysis, but no null annotations are available on the classpath. Making null annotations available (by whichever dependency mechanism your project is using) is not just a workaround but actually the proper solution. The Eclipse bug is about suitably reporting this configuration issue (better than the resolve error in the question). – Stephan Herrmann Nov 28 '15 at 21:38
  • @StephanHerrmann, thanks for clarification. I updated my answer. – luboskrnac Dec 15 '15 at 09:29
  • This guff isn't needed at runtime, so why not declare it a "provided" dependency? (Since we have to use a dependency manager to kludge this to start with) – Phil Nov 20 '18 at 02:57
  • I fully agree that this should not or rather must not(!) be fixed by fiddling with dependencies in the pom! In our code we got said error and we are nowhere using any NonNull-related eclipse annotation (nor does any code that we depend on). It seems that the simple presence of a .filter(Objects::nonNull) expression as part of a longer stream chain causes this issue and IMHO that's a clear bug! – mmo Nov 28 '19 at 12:17
16

Eclipse has a feature called annotation-based null analysis, which provides compile-time checks using annotations (e.g. @NonNull or @Nullable). You get this error when annotations are missing in the classpath.

If you don't plan to use the annotation-based null analysis feature, you can just disable it in Eclipse.

Open the global or project settings and go to Java > Compiler > Warnings. In the Null analysis category, uncheck Enable annotation-based null analysis. Then rebuild the workspace and those errors won't show again.

kapex
  • 26,163
  • 5
  • 97
  • 111
3

I have solved it by changing the "Use default annotation for null specifications"

I have enter the both class names:

javax.annotation.Nonnull
javax.annotation.Nullable
Horcrux7
  • 21,867
  • 21
  • 85
  • 134