[英]"Exception in thread "main" java.lang.NoSuchFieldError:"
I am getting this error:我收到此错误:
Exception in thread "main" java.lang.NoSuchFieldError: num1 at calificacion.main(problema_3.java:17)
线程“主”中的异常 java.lang.NoSuchFieldError: num1 at calificacion.main(problema_3.java:17)
after type the first value on this simple terminal java program:在这个简单的终端 java 程序上输入第一个值后:
import java.io.*;
class num
{
int num1,num2,num3;
}
class calificacion
{
public static void main(String []Args)throws IOException
{
BufferedReader ob1=new BufferedReader(new InputStreamReader (System.in));
num DQ = new num();
System.out.println ("Ingrese primera calificación:");
DQ.num1 = Integer.parseInt(ob1.readLine());
System.out.println ("Ingrese segunda calificación:");
DQ.num2 = Integer.parseInt(ob1.readLine());
System.out.println ("Ingrese tercera calificación:");
DQ.num3 = Integer.parseInt(ob1.readLine());
System.out.println("\n\n");
System.out.println("Calificación final: "+(DQ.num1+DQ.num2+DQ.num3)/3);
}
}
on the other hand this one runs just fine and I am using the same input method in both:另一方面,这个运行得很好,我在两者中都使用相同的输入法:
import java.io.*;
class num
{
int numero;
}
class paroimpar
{
public static void main(String []Args)throws IOException
{
BufferedReader ob1=new BufferedReader(new InputStreamReader (System.in));
num DQ = new num();
System.out.println ("Ingrese un número entero para determinar si es par o impar:");
DQ.numero = Integer.parseInt(ob1.readLine());
System.out.println("\n\n");
if (DQ.numero%2==0){
System.out.println(DQ.numero+" es par.");
} else {
System.out.println(DQ.numero+" es impar.");
}
}
}
Thanks in advance.提前致谢。
As You can read here this error states that You're missing field num1
.正如您可以在此处阅读的,此错误表明您缺少字段
num1
。 There is short Baeldung tutorial regarding NoSuchFieldException
.有关于
NoSuchFieldException
的简短Baeldung 教程。
About Your issue.关于您的问题。 It's not because of the code (I've successfully compiled and run that).
这不是因为代码(我已经成功编译并运行了它)。 I believe it's because of compilation.
我相信这是因为编译。 Do you have those classes in the same directory?
你在同一个目录中有这些类吗? During compilation
javac calification.java
there should be two files out calification.class
and num.class
.在编译
javac calification.java
期间,应该有两个文件calification.class
和num.class
。 When You compile the second one it may override num.class
.当您编译第二个时,它可能会覆盖
num.class
。
How to solve this issue?如何解决这个问题?
num
classes names so they don't interferenum
类名称之一,以免它们干扰BTW.顺便提一句。 In Java we tend to start classes names with capital letter and variables with camelCase, so it's easier to read:)
在 Java 中,我们倾向于以大写字母开头的类名和以驼峰命名的变量,因此更易于阅读:)
Hope it helps!希望能帮助到你! Happy coding!
快乐编码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.