Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lombok getter setter cannot find symbol

Tags:

java

lombok

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<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());
        }

    }
}

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 enter image description here

plugin

enter image description here

like image 581
chiefpika Avatar asked Mar 15 '20 15:03

chiefpika


People also ask

How do you make a getter setter with Lombok?

You can annotate any field with @Getter and/or @Setter , to let lombok generate the default getter/setter automatically. A default getter simply returns the field, and is named getFoo if the field is called foo (or isFoo if the field's type is boolean ).

Can not find Lombok plugin in IntelliJ?

Go to File > Settings > Plugins. Click on Browse repositories... Search for Lombok Plugin. Click on Install plugin.

Should we use @data Lombok?

Also, I really discourage using any Lombok annotation that modifies the code. If you take @Data, @Getter, @Setter, @AllArgsConstructor adds new codes into the existing source code without modifying the code we wrote.

What does @data annotation of Lombok do?

@Data is a convenient shortcut annotation that bundles the features of @ToString , @EqualsAndHashCode , @Getter / @Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, ...


1 Answers

I didn't put

annotationProcessor 'org.projectlombok:lombok:1.18.12'

in my build.gradle

problem solved.

like image 182
chiefpika Avatar answered Oct 08 '22 23:10

chiefpika