How to solve the exception in thread “main” Java Lang.nullpointerexception error

See English answers > what is a NullPointerException, and how do I fix it? 12

java.lang.NullPointerException
at twoten.TwoTenB.<init>(TwoTenB.java:29)
at javapractice.JavaPractice.main(JavaPractice.java:32)
Java Result: 1

It was my mistake I can really use some help because I've been trapped in this place for hours

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");
    }
    }
}

Solution

That's the problem

double a[] = null;

Since a is null, NullPointerException will appear every time it is used until it is initialized So this:

a[i] = var;

Will fail

One possible solution is to initialize it when it is declared:

double a[] = new double[PUT_A_LENGTH_HERE]; //seems like this constant should be 7

IMO is more important than solving this exception. You should learn to read the stack trace and understand what it says, so that you can find and solve the problem

This exception indicates that there is a variable with a null value How? Before using, make sure that the variable is not null

This line has two parts:

>First, show the class and method that threw the error In this case, it is located in the method of class twotenb declared by < init > in the package twoten When you encounter an error message for someclassname< Init > means that an error is thrown when creating a new instance of the class, such as executing the constructor (which seems to be a problem in this case). > Secondly, the file and line number that caused the error are displayed, that is, between parentheses In this way, it is easier to find the location of the error So you have to check the file twotenb Java, line 29 This seems to be [i] = VaR

Starting from this line, the other lines are similar to telling you where the error occurred So when you read this:

This means that you are trying to instantiate a twotenb object reference in the main method of the class javapractice declared in the javapractice package

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>