Saturday, 16 April 2016

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




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



java.lang.NullPointerException
at twoten.TwoTenB.(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");
}
}
}

Answer



This is the problem



double a[] = null;


Since a is null, NullPointerException will arise every time you use it until you initialize it. So this:



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 read the stacktrace and understand what it says, so you could detect the problems and solve it.




java.lang.NullPointerException




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.




at 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 method in class TwoTenB declared in package twoten. When you encounter an error message with SomeClassName., 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:




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




It means that you were trying to instantiate a TwoTenB object reference inside the main method of your class JavaPractice declared in javapractice package.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...