线程“main”中的异常java.lang.Error:未解析的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21664727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:56:25  来源:igfitidea点击:

Exception in thread "main" java.lang.Error: unresolved variable

javamethodserror-handlingcompiler-errorslinkageerror

提问by user3289740

Exception in thread "main" java.lang.Error: Why can't my parameter variable be resolved to a variable?

线程“main”中的异常java.lang.Error:为什么我的参数变量不能解析为变量?

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.

我正在尝试创建一个简单的程序来创建 people 类的两个对象,给它们命名并使第一个对象(“lisa”)成为第二个对象(“mark”)的朋友,最后显示/打印出 lisa 的朋友在屏幕上。

But Eclipse displays the following error:

但是 Eclipse 显示以下错误:

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)

线程“main”中的异常java.lang.Error:未解决的编译问题:lisa无法解析为变量标记无法解析为变量lisa无法解析为变量语法错误,插入“;” 完成 Statement 方法friend() 在People.main(People.java:22) 中未定义为People 类型

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!

如您所知,我对 Java 很陌生,所以我无法理解错误的含义以及如何修复它。非常感谢您的帮助!

Here is my People class:

这是我的 People 课程:

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) {

公共静态无效主(字符串 [] args){

    People Object1 = new People();
    Object1.name(lisa);

    People Object2 = new People();
    Object2.name(mark);

    Object1.addFriend(lisa);

    System.out.println(Object1.friend());

}

}

}

采纳答案by Meno Hochschild

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.

首先注意“lisa”周围的引号。如果没有这些引号,Java 会将其解释为变量名而不是字符串(如 People 类中 name() 方法的签名所要求的那样。Java 中的常见约定是用小写字母编写“Object1”等变量名 -为了代码可读性。这里作为信息来自 Oracle 的指导方针