Reputation: 7216
I have this class:
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
// tag::code[]
@Data
@Document
public class Image {
@Id final private String id;
final private String name;
}
// end::code[]
My understanding is that @Data
should create a constructor for all final fields by default. However when I run my application I get this error:
error: variable id not initialized in the default constructor
@Id final private String id;
Why would this be happening?
Upvotes: 18
Views: 54889
Reputation: 1
this suggestion from Stan worked: Switching from "Processor path" option to "Obtain processors from project classthpath" in "Preferences | Build, Execution, Deployment | Compiler | Annotation Processors -> yourProject"
Upvotes: 0
Reputation: 303
I had the same issue and it appears I haven't added the annotationProcessorPath
to lombok during it's installation, similar to what @runnerpaul mentioned.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
Upvotes: 17
Reputation: 11
I used spring initializer to create the project and I added the lombok dependency using it However adding provided to my pom.xml fixed the problem
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<scope>provided</scope>
</dependency>
Upvotes: 1
Reputation: 43
I've had a similar issue recently.
Switching from "Processor path" option to "Obtain processors from project classthpath" in "Preferences | Build, Execution, Deployment | Compiler | Annotation Processors -> yourProject" has helped in my case.
The initial value in "Processor path" field contained a path like:
/Users/.../.m2/repository/org/projectlombok/lombok/unknown/lombok-unknown.jar
... which was the root cause of the problem I assume.
Upvotes: 2
Reputation: 11
If Still cnt work, you might try select "Obtain processors from project classpath" which is under "Enable annotation processing".
By selecting the "Obtain processors from project classpath" radio button, you ensure that your project uses Lombok's annotation processor from the classpath, which is typically the correct approach when Lombok is added as a Maven or Gradle dependency.
The default option of selecting the "Processor Path" can sometimes cause issues if the path is not correctly resolved or if the Lombok jar file is misplaced. Switching to "Obtain processors from project classpath" resolves such problems since it automatically detects Lombok from the dependencies.
Upvotes: 1
Reputation: 2058
My understanding is that @Data should create a constructor for all final fields by default. Error: variable id not initialized in the default constructor
@Id final private String id;
Why would this be happening?
Yes! you are right! @Data annotation generates a parameterized constructor for final fields, generates setters for all non-final fields and getters for both types of fields.
In your case, your generated constructor should look like this,
public Image(Long id, String name) {
this.id = id;
this.name = name;
}
//getters for both fields
As your constructor not able to initialize the final fields - seems Lombok
is not being set up properly - you can verify it by checking your Image.class
in the target/classes
directory with the same package(as you have it in your src except you have defined the location explicitly through config file). If it's not being generated, verify your dependency, Lombok plugin, you may want to explore Lombok configuration for further set-up.
Upvotes: 8