问题描述
我在Java程序中遇到问题。线程main中的异常
I am having a problem at a Java program. Exception in thread "main"
java.lang.NullPointerException
at twoten.TwoTenB.<init>(TwoTenB.java:29)
at javapractice.JavaPractice.main(JavaPractice.java:32)
Java Result: 1
是我得到的错误。我真的可以使用一些帮助,因为我在这个地方被困了几个小时...
is the error that I'm getting. I could really use some help, since i am stuck hours on this spot...
package twoten;
import java.util.Scanner;
public class TwoTenB {
public TwoTenB() {
double percentage;
double a[] = null;
double total = 0;
double var;
System.out.print("\tRESULT\n\n");
Scanner scan = new Scanner(System.in);
//double[] mark = new double[7];
for (int i = 0; i < 7; i++) {
System.out.print("\nMarks in subject " + (i + 1) + "\t:\t");
var = scan.nextDouble();
a[i] = var;
total = total + a[i];
//percentage = first * second * third * fourth * fifth * sixth * seventh * 100 / 700;
}
percentage = total * 100 / 700;
if (a[0] > 35 && a[1] > 35 && a[2] > 35 && a[3] > 35 && a[4] > 35 && a[5] > 35 && a[6] > 35 && percentage > 35) {
if (percentage >= 60) {
System.out.print("\nCongratulation!!! you've got FIRST dividion\n");
} else if (percentage >= 45 && percentage < 60) {
System.out.print("\nCongratulation!!! you've got SECOND dividion\n");
} else if (percentage >= 35 && percentage < 45) {
System.out.print("\nCongratulation!!! you've got THIRD dividion\n");
}
} else {
System.out.print("\nSORRY!!! you've FAILED\n");
}
}
}
推荐答案
这是问题
double a[] = null;
由于 a
是 null , NullPointerException
,直到初始化它为止。所以这个:
Since a
is null
, NullPointerException
will arise every time you use it until you initialize it. So this:
a[i] = var;
将失败。
可能的解决方案会在声明它时初始化它:
A possible solution would be initialize it when declaring it:
double a[] = new double[PUT_A_LENGTH_HERE]; //seems like this constant should be 7
IMO比解决这个例外更重要的是,您应该学会读取堆栈跟踪并理解它所说的内容,这样您就可以发现问题并解决它。
IMO more important than solving this exception, is the fact that you should learn to read the stacktrace and understand what it says, so you could detect the problems and solve it.
此异常表示使用的值为 null
的变量。怎么解决?在使用之前,请确保变量不是 null
。
This exception means there's a variable with null
value being used. How to solve? Just make sure the variable is not null
before being used.
这一行有两部分:
- 首先,显示抛出错误的类和方法。在这种情况下,它是在
< init>
方法中声明的类TwoTenB
包twoten
。当您遇到带有SomeClassName。< init>
的错误消息时,表示在创建类的新实例时抛出了错误,例如执行构造函数(在这种情况下似乎是问题)。 - 其次,显示抛出错误的文件和行号位置,即括号之间。这种方式更容易发现错误出现的位置。所以你必须查看文件TwoTenB.java,第29行。这似乎是
a [i] = var;
。
- First, shows the class and method where the error was thrown. In this case, it was at
<init>
method in classTwoTenB
declared in packagetwoten
. When you encounter an error message withSomeClassName.<init>
, means the error was thrown while creating a new instance of the class e.g. executing the constructor (in this case that seems to be the problem). - Secondly, shows the file and line number location where the error is thrown, which is between parenthesis. This way is easier to spot where the error arose. So you have to look into file TwoTenB.java, line number 29. This seems to be
a[i] = var;
.
从这一行开始,其他行将类似于告诉您错误发生的位置。所以在阅读本文时:
From this line, other lines will be similar to tell you where the error arose. So when reading this:
这意味着你试图在 main $中实例化一个
TwoTenB
对象引用c $ c>您的类的方法 JavaPractice
在 javapractice
包中声明。
It means that you were trying to instantiate a TwoTenB
object reference inside the main
method of your class JavaPractice
declared in javapractice
package.
这篇关于如何解决线程“主”中的异常? java.lang.NullPointerException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!