如何解决线程“main”中的异常 java.lang.NullPointerException 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19467202/
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-12 17:31:15  来源:igfitidea点击:

How can I solve Exception in thread "main" java.lang.NullPointerException error

java

提问by Roshan

I am having a problem at a Java program. Exception in thread "main"

我在 Java 程序中遇到问题。线程“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");
    }
    }
}

采纳答案by Luiggi Mendoza

This is the problem

这就是问题

double a[] = null;

Since ais null, NullPointerExceptionwill arise every time you use it until you initialize it. So this:

因为ais null,NullPointerException每次使用它都会出现,直到你初始化它。所以这:

a[i] = var;

will fail.

将失败。

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 more important than solving this exception, is the fact that you should learn to readthe stacktrace and understandwhat it says, so you could detect the problems and solve it.

IMO 比解决这个异常更重要的是,你应该学会阅读堆栈跟踪并理解它所说的内容,这样你就可以检测问题并解决它。

java.lang.NullPointerException

java.lang.NullPointerException

This exception means there's a variable with nullvalue being used. How to solve? Just make sure the variable is not nullbefore being used.

这个异常意味着有一个带有null值的变量正在被使用。怎么解决?null在使用之前确保变量不在。

at twoten.TwoTenB.(TwoTenB.java:29)

在twoten.TwoTenB.(TwoTenB.java:29)

This line has two parts:

这条线有两部分:

  • First, shows the class and method where the error was thrown. In this case, it was at <init>methodin class TwoTenBdeclared in package twoten. When you encounter an error message with SomeClassName.<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;.
  • 首先,显示抛出错误的类和方法。在这种情况下,它位于 package中声明的类中的<init>方法中。当您遇到带有 的错误消息时,意味着在创建类的新实例时抛出了错误,例如执行构造函数(在这种情况下,这似乎是问题所在)。TwoTenBtwotenSomeClassName.<init>
  • 其次,显示抛出错误的文件和行号位置,在括号之间。这种方式更容易发现错误发生的地方。所以你必须查看文件 TwoTenB.java,第 29 行。这似乎是a[i] = var;.

From this line, other lines will be similar to tell you where the error arose. So when reading this:

从这一行开始,其他行将类似地告诉您错误发生的位置。所以在阅读本文时:

at javapractice.JavaPractice.main(JavaPractice.java:32)

在 javapractice.JavaPractice.main(JavaPractice.java:32)

It means that you were trying to instantiate a TwoTenBobject reference inside the mainmethod of your class JavaPracticedeclared in javapracticepackage.

这意味着您试图在包中声明的类TwoTenBmain方法内实例化对象引用。JavaPracticejavapractice