• 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

Please log in or register to answer this question.

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,260 points

Related questions

0 votes
1 answer 8 views
Problem: I have a form with a button and an icon assigned to the button. This code was generated by NetBeans. I get an 'Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" error when trying to run the application. I created a brand new Gradle project with ... and an icon in the button. This exact same process worked for an Ant project, but won't work with my new Gradle project.
asked Apr 25 sumaiya simi 43.9k points
0 votes
1 answer 87 views
0 votes
1 answer 5 views
Problem: Hey guys I get the errors Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at P6.itemStateChanged(P6.java:215) at javax.swing.AbstractButton.fireItemStateChanged(AbstractButton.java:2 023) at javax.swing.AbstractButton$Handler.itemStateChanged(AbstractButton.ja va: ... trying to figure this out, but no luck. Any help please? Line 215 is: if(this.jrbArc.isSelected())
asked May 2 anika11 32.2k points
0 votes
1 answer 8 views
Problem: Why I got the error > Matlab exception in thread awt eventqueue 0 java lang nullpointerexception
asked May 2 ummeshani 10.8k points
1 vote
1 answer 7 views
Problem: I am trying to find the solution for this; Exception in thread thread 0 java lang nullpointerexception.
asked Apr 26 PkGuy 23.5k points
0 votes
1 answer 43 views
Hello I'm a new programmer at an high school level as a result I do not know much about programming and am getting quite a few errors which have been resolved while others I completely do not understand.
asked Dec 7, 2020 TeamScript 13.5k points
0 votes
1 answer 119 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 528 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
0 votes
1 answer 2 views
Problem: Has anyone faced this before? > Exception in thread main java lang noclassdeffounderror
asked 7 hours ago Priscilla Gurpreet 3.7k points
0 votes
1 answer 3 views
Problem: Where can I look for a solution to this? > Exception in thread main java.lang.classcastexception .
asked 22 hours ago Isac Christiaan 7.6k points