2 views
in Java

Can anyone help me with the exception, given below is my code and it is giving an error:

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;

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 > 35 && a > 35 && a > 35 && a > 35 && a > 35 && a > 35 && a > 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 (26.7k points)

Basically, the problem is:

double a[] = null;

So, it throws an exception of NullPointerException as "a" is null. So, you can try like this:

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

I hope this will help.

Want to become a Java expert? join Java Certification now!!

Want to know more about Java? Watch this video on Java Tutorial for Beginners | Java Tutorial: