I am working on a Spring Boot project in IntelliJ IDEA and encountered the following error when trying to create an instance of a class and call its getter method:
Check image to get idea what was my question & error
Error:
java: constructor User in class com.example.HealthCare.User cannot be applied to given types;
required: no arguments
found: java.lang.String
reason: actual and formal argument lists differ in length
I am using Lombok annotations (@Data, @NoArgsConstructor, and @AllArgsConstructor) in my User class to automatically generate constructors and getter/setter methods. Here’s the relevant code:
User.java
package com.example.HealthCare;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String name;
}
HealtCareApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HealthCareApplication {
public static void main(String[] args) {
SpringApplication.run(HealthCareApplication.class, args);
User user = new User("Shubham");
System.out.println(user.getName());
}
}
Steps I have tried:
Verified that annotation processing is enabled in IntelliJ IDEA.
Checked the Lombok plugin version, Spring Boot version, and my system (Ubuntu). All are properly set up and up-to-date.
Cleaned and rebuilt the project multiple times.
When I clone a working project from another source, the same setup works. However, when I create a new project from scratch with similar configurations, this issue occurs.
My Environment:
OS: Ubuntu 24.04
Spring Boot Version: 3.4.0
Lombok Version: 1.18.30
IDE: IntelliJ IDEA 2024.3.1
Why does Lombok not generate constructors and getter methods in my project, despite everything being correctly configured? Could there be a configuration or setup issue specific to a newly created project?
2
Answers
Check your version of Lombok, and ensure that the plugin configuration in the pom.xml file remains unchanged. Sometimes invalidating caches in IntelliJ is ineffective.
The issue might be related to your project dependencies or how Lombok is configured. To ensure your project is set up correctly, try replacing your
pom.xml
file with the content provided in this gist.This updated
pom.xml
includes proper Lombok configurations and other necessary dependencies that might resolve the problem. After updating the file, follow these steps:Reload Maven Project:
Maven > Reload Project
.Enable Annotation Processing:
File > Settings > Build, Execution, Deployment > Compiler > Annotation Processors
and check "Enable annotation processing."Project Properties > Java Compiler > Annotation Processing
.Rebuild the Project:
Verify Lombok Setup:
If the problem persists, check for any conflicting dependencies or versions in your
pom.xml
. Also, ensure the class you’re trying to use Lombok annotations in is compiled without errors.