Syntax Error
Cannot find symbol: method add(int, Person) in class com.example.Person
What This Error Means
This error occurs when the Java compiler cannot find a method or field that is supposed to exist in a class. This is often due to a simple typo or incorrect method name.
Why It Happens
In this case, the error is caused by trying to call a method named 'add' with parameters 'int' and 'Person' on a class named 'Person'. However, the actual method name might be 'addPerson' or 'addPersonWithId'. The Java compiler is unable to find a method that matches the name and parameters specified.
How to Fix It
- 1To fix this error, check the method name and parameters in the code and make sure they match the actual method name and parameters. Here's a step-by-step guide:
- 21. Review the class definition of 'Person' and look for the method that matches the name and parameters.
- 32. If you find the correct method, check if you have imported the correct class.
- 43. If the method does not exist, create the method with the correct name and parameters.
- 5// Before (broken code)
- 6public class Main {
- 7 public static void main(String[] args) {
- 8 Person person = new Person();
- 9 person.add(1, new Person());
- 10 }
- 11}
- 12// After (fixed code)
- 13public class Main {
- 14 public static void main(String[] args) {
- 15 Person person = new Person();
- 16 person.addPerson(1, new Person());
- 17 }
- 18}
Example Code Solution
public class Person {
public void add(int id, Person person) {} // This method does not exist
}public class Person {
public void addPerson(int id, Person person) {}
}Fix for Cannot find symbol: method add(int, Person) in class com.example.Person
Browse Related Clusters
Related JAVA Errors
java.lang.StackOverflowError at com.example.Main.main(Main.java:15) -
Error executing SQL query: Cannot insert explicit value for identity c
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
Cannot find symbol: variable 'name' in void main(String[] args)
Related JAVA Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error