Exception in thread "main" java.lang.Error: Why can't my parameter variable be resolved to a variable?
I'm trying to create a simple program that creates two objects of the people class, give them names and make the first object ("lisa") be-friend the second object ("mark") and finally display/print out lisa's friend on screen.
But Eclipse displays the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: lisa cannot be resolved to a variable mark cannot be resolved to a variable lisa cannot be resolved to a variable Syntax error, insert ";" to complete Statement The method friend() is undefined for the type People at People.main(People.java:22)
As you can tell, I'm very new to Java, so I cannot understand what the error means and how I can fix it. Your help is greatly appreciated!
Here is my People class:
public class People {
//Constructor
public void name(String name) {
this.name = name;
}
// Instance variables
public String name;
public String friend;
// Instance method
public void addFriend(String name){
name = Object1.friend();
}
Here is my main method:
public static void main(String[] args) {
People Object1 = new People();
Object1.name(lisa);
People Object2 = new People();
Object2.name(mark);
Object1.addFriend(lisa);
System.out.println(Object1.friend());
}
}
Instead of
People Object1 = new People();
Object1.name(lisa);
you should write:
People people = new People();
people.name("lisa");
Note first the quotation marks around "lisa". Without these quotation marks Java will interprete it as variable name and not as String (as required by signature of name()-method in class People. And it is a common convention in Java to write variable names like "Object1" in small letters - for code readability. Here as information the guidelines from Oracle.