Lombok的getter和setter找不到符号。

huangapple go评论91阅读模式
英文:

Lombok getter setter cannot find symbol

问题

我正在使用Intellij并尝试将lombok应用于项目中
但是它一直显示无法找到符号”。
以下是我的代码的简要示例



import lombok.*;

@Data
public class Product {

    private String name;
    private Integer price;

    public Product(String name, Integer price){
        this.name = name;
        this.price = price;
    }
}

主类

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionMain {
    public static void main(String[] args) {

        Collection<Product> products = new ArrayList<>();
        Product door = new Product("DOOR",90);
        Product bed = new Product("BED",60);
        Product ipad = new Product("iPad",15);

        products.add(door);
        products.add(bed);
        products.add(ipad);

        final Iterator<Product> productIterator = products.iterator();

        while(productIterator.hasNext()){
            Product product = productIterator.next();
            System.out.println(product.getPrice());
        }

    }
}


错误信息如下

CollectionMain.java:23: 错误: 找不到符号
            System.out.println(product.getPrice());
                                      ^
  符号:   方法 getPrice()
  位置: 类型为Product的变量product

我已经启用了注解处理器

(图片部分无法在文本中精确呈现,建议您在原始环境中查看图片以获得完整的视觉信息。)

英文:

I'm using Intellij and trying to apply lombok to the project.
But it keeps saying "cannot find symbol".
Here's a quick sample of my code.

Class

import lombok.*;
@Data
public class Product {
private String name;
private Integer price;
public Product(String name, Integer price){
this.name = name;
this.price = price;
}
}

Main

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionMain {
public static void main(String[] args) {
Collection&lt;Product&gt; products = new ArrayList&lt;&gt;();
Product door = new Product(&quot;DOOR&quot;,90);
Product bed = new Product(&quot;BED&quot;,60);
Product ipad = new Product(&quot;iPad&quot;,15);
products.add(door);
products.add(bed);
products.add(ipad);
final Iterator&lt;Product&gt; productIterator = products.iterator();
while(productIterator.hasNext()){
Product product = productIterator.next();
System.out.println(product.getPrice());
}
}
}

and the error says

CollectionMain.java:23: error: cannot find symbol
System.out.println(product.getPrice());
^
symbol: method getPrice()
location: variable product of type Product

I have enabled the annotation processor
Lombok的getter和setter找不到符号。

plugin

Lombok的getter和setter找不到符号。

答案1

得分: 18

我没有在我的 build.gradle 中添加

annotationProcessor 'org.projectlombok:lombok:1.18.12'

问题解决。

英文:

I didn't put

annotationProcessor &#39;org.projectlombok:lombok:1.18.12&#39;

in my build.gradle

problem solved.

答案2

得分: 2

我遇到了相同的问题。但我的解决方案有点不同。

我的项目使用的是 Java 8,但 IDEA 的 SDK 设置为 Java 17。一旦我将其更改为 Java 8,问题就解决了。

  1. 进入 文件 > 项目结构 > 项目
  2. 更改 SDK
  3. 应用 > 确定
  4. 文件 > 使无效并重新启动
英文:

I had the same issue. But my solution was bit different.

My project is on java 8 but IDEA SDK was set to java 17. Once I changed it to java 8 issue was solved.

  1. Go to File > Project Structure > Project
  2. Change SDK
    Lombok的getter和setter找不到符号。
  3. Apply > OK
  4. File > Invalidate and Restart

答案3

得分: 0

由于某种原因,Maven仓库只为Gradle构建器提供了'compileOnly'依赖项。
然而,如果您查阅Lombok文档,您会发现您还需要使用'annotationProcessor'。

https://projectlombok.org/setup/gradle

英文:

For some reason the Maven repository only provides you with the 'compileOnly' dependency for the Gradle builder.
However, if you look in the Lombok documentation you will find that you also need to use the 'annotationProcessor'.

https://projectlombok.org/setup/gradle

答案4

得分: 0

我在尝试构建我的项目(使用gradle)时遇到了相同的错误。我在项目中使用了jdk-15,但后来在我的计算机上安装了jdk-17(即使在项目中没有更改sdk),然后出现了这个问题。
为了解决这个问题,我从计算机中卸载了jdk-17(仅在项目中删除sdk是不够的)。

英文:

I faced the same error when tried to build my project (gradle). I used jdk-15 on my project, but then installed jdk-17 on my computer (even without changing sdk in the project) and the problem happened.
To fix the issue I uninstalled jdk-17 from the computer (deleting sdk on project is not enough)

答案5

得分: 0

已添加编译时依赖和注解处理器。
图像描述

英文:

Added both compile only and annotationProcessor
enter image description here

huangapple
  • 本文由 发表于 2020年3月15日 23:37:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/60694561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定