Ask Your Question
1

What is the meaning of Eclipse @NonNullByDefault error when using @Value with Lombok class?

asked 2023-07-03 20:08:21 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-03 20:09:02 +0000

plato gravatar image

When using Lombok's @Value annotation in a Java class, Eclipse may show an error message saying "@NonNullByDefault is applied to the module default package" or "@NonNullByDefault in package-info.java does not affect source files" or similar.

This error occurs because @NonNullByDefault is a Java 8 annotation that specifies that all parameters and return values in a package or module are non-null by default unless explicitly annotated otherwise. However, Lombok's @Value annotation generates a constructor and getters that do not use nullability annotations, so Eclipse doesn't know whether the generated code is supposed to be non-null or nullable by default.

To resolve this error, you can add @NonNull annotations to the fields of the Lombok class, or annotate the Lombok class with @lombok.NonNull, or use @Nullable annotations to denote nullable fields or arguments. Another solution would be to explicitly declare @NonNullByDefault at the top of the file or in the package-info.java file.

For example, if you want all fields in a package to be assumed as non-null by default, you can add @NonNullByDefault to the package-info.java file like this:

@NonNullByDefault
package com.example;

import org.eclipse.jdt.annotation.NonNullByDefault;

This will tell Eclipse that all fields in the com.example package are non-null by default, and the @Value-annotated Lombok classes in that package will not trigger the error message.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-03 20:08:21 +0000

Seen: 7 times

Last updated: Jul 03 '23