• Register
1 vote


I was working with Java array. Suddenly I got this error message, “exception in thread "main" java.lang.nullpointerexception”. Can somebody help me out urgently, please? I am attaching the declaring part of my code below for your better understanding.
Thanks in advance.

import java.util.Scanner;
public class TestClass {
public TestClass() {
    double percentage;
    double n[] = null;
    double total = 0;
    double var;
    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();

        n[i] = var;

        total = total + n[i];
       //percentage = first * second * third * fourth * fifth * sixth * seventh * 100 / 700;


12 7 7
15,250 points

1 Answer

1 vote
Best answer


Hello Gavin, It seems you are going through some rough times with your codes. The problem was started when you declare an array n and didn’t initialize it. Think again, you have an array and it has ‘’null” value. So what is the purpose it calling in your codes? Makes sense; right?
You could have avoided the error if you declare the array this way:
double a []= new double [length_of_array]

Keep coding.

13 9 6
94,240 points

Related questions

0 votes
1 answer 19 views
0 votes
1 answer 94 views
Problem : I am getting bellow error repeatedly exception in thread "awt-eventqueue-0" java.lang.nullpointerexception
asked Oct 31, 2019 peterlaw 6.9k points
0 votes
1 answer 367 views
Problem : Everytime we compile the GUI , we get bellow error exception in thread "javafx application thread" java.lang.nullpointerexception
asked Oct 19, 2019 peterlaw 6.9k points
1 vote
1 answer 38 views
Problem: I am a begginer in java programming language and I have been coding for just some days. To practice my programming I wrote this code in java: public class Main { public static void main(String[] argv){ int 2count, h-l, a/b, } } But It shows me the error message ... , . expected Syntax error on token ",", ;expected at Main.main(Main.java:3) Whats is wrong in my code can anyone help me out?
asked Apr 6 LizzyM 6.1k points
1 vote
1 answer 16 views
Problem : I have very recently started to learn programming in Java. I am trying to execute my java code but I am facing the below error. &ldquo;Exception in thread "main" java.lang.arrayindexoutofboundsexception:&rdquo; I am unable to understand above error. Please find below my code: ... according to my debugger*/ myodd[j] = input[i]; j++; } return myodd; } Please help me in fixing above error.
asked Mar 30 morrisBson 3.2k points