0

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

}

}

3
  • 2
    Read the error - it says everything: the variable "lisa" is not defined! you probably meant to use the string "lisa" as a parameter. BTW name() is NOT a constructor. Commented Feb 9, 2014 at 20:39
  • What lisa is here ?? u didn't defined this variable here... Commented Feb 9, 2014 at 20:44
  • Hey, I realized that, but that is exactly what I'm trying to do with this line of code ( trying to make 'lisa' = name): public String name; and then: Object1.name(lisa); Commented Feb 9, 2014 at 20:44

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

OMG Thank you! Being new to the syntax I didn't realize the importance for quotation marks and thanks for letting me know about writing object names in small letters.
@user3289740 Have added a link to code style guidelines. Happy learning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.