Lombok: RequiredArgsConstructor is not working
Asked Answered
E

7

32

It seems that @RequiredArgsConstructor not working in the code below. Why is it?

import java.io.Serializable;

import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor
public class User implements Serializable {

    private String username;

    /*public User(String username) {
        this.username = username;
    }*/

    private static final long serialVersionUID = 8043545738660721361L;
}

I get the error:

javax.faces.el.EvaluationException: java.lang.Error: Unresolved compilation problem: 
    The constructor User(String) is undefined

For some reason seems it does work for other domain class in which no constructor defined but instead used the @RequiredArgsConstructor annotation.

Ellyellyn answered 7/6, 2016 at 5:50 Comment(0)
B
60

According to Documentation, Required arguments are final fields and fields with constraints such as @NonNull.

You need to make username as @NonNull

@NonNull private String username;

And you need to make them final too.

Blinding answered 7/6, 2016 at 5:57 Comment(3)
And final fields! Don't forget final fields. final fields often don't get much love (repetition done on purpose =D)Flaunt
If you've already used final and/or @NonNull, try using a manual constructor to help narrow down the issue. A strategy for debugging can be found here: janac.medium.com/…Iodize
Thanks, @JanacMeena , I had to create the constructor, and that fixed my issue.Outdoor
R
15

It's also worth noting for future readers that @Data also provides @RequiredArgsConstructor, so using both annotations isn't necessary :)

Realistic answered 26/7, 2018 at 7:59 Comment(1)
When you use @Data in combination with another constructor annotations like @NoArgsConstructor then @RequiredArgsConstructor won't be generated automatically.Muscadine
K
5

Did you installed Lombok plugin in IntelliJ?

If not then

File -> Settings -> Plugins: Search for Lombok (CodeStream) version.

Restart the IDE and it should be fixed.

Double Check:

  • You have Lombok library installed using Maven or Gradle.
  • Enabled Annotation Processors from IntelliJ IDE from File -> Settings: Search for Annotation Processors
Kunlun answered 17/10, 2019 at 1:51 Comment(1)
I didn't know about plugin. Thanks, this works for me.Impressible
H
4

@RequiredArgsConstructor

> Generates a constructor with required arguments. Required arguments
 are final fields and fields with constraints such as @NonNull.
> Complete documentation is found at the project lombok features page
for @Constructor.
> Even though it is not listed, this annotation also has the
 *`onConstructor`* parameter. See the full documentation for more details.

Lombok library

To use the @RequiredArgsConstructor, the variable has to be final and it will create the values in constructor automatically

private final String username;
Humor answered 11/5, 2021 at 10:49 Comment(0)
L
3

Try changing project/module JDK to 1.8.

Project Structure -> Project Settings->Project SDK and Project Language Level

Lipid answered 18/2, 2020 at 1:50 Comment(1)
this helped indeed, even after switching back to 14 the error is goneAmalgamate
D
2

The argument fields for @RequiredArgsConstructor annotation has to be final. So this fix will work:

private final String username;

The IDE IntelliJ makes the variable grey (inactive status) when final keyword missed, which is very helpful to detect this kind of mistake.

Doyon answered 17/12, 2020 at 3:44 Comment(0)
S
0

if all lombok annotations are not working try downloading the "installer" from lombok (you will have to point it out where is your IDE) and that solved for me the problem Here is the installer for java https://projectlombok.org/setup/javac

Strum answered 13/6, 2024 at 7:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.