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
plugin
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 ).
Go to File > Settings > Plugins. Click on Browse repositories... Search for Lombok Plugin. Click on Install plugin.
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.
@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, ...
I didn't put
annotationProcessor 'org.projectlombok:lombok:1.18.12'
in my build.gradle
problem solved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With