• 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,240 points

Related questions

0 votes
1 answer 19 views
Problem: java.lang.NullPointerException is one of the most popular exceptions in java programming. Anybody working in java must have seen this popping out of nowhere in java standalone program as well as java web application. Some of the common reasons for ... an object instance but at runtime the object is null. Accessing variables of an object instance that is null at runtime.
asked Feb 1 Ali 4.3k points
0 votes
1 answer 64 views
0 votes
1 answer 18 views
Problem: I am to make a simple Check Box selection program where the user gets to pick between a variety of choices and depending on their action the image changes. The program itself compiles perfectly but when I run it however it gives me some complications.
asked Feb 14 Ali 4.3k points
0 votes
1 answer 9 views
Problem: I have a method in a client server program which is meant to create an instance of a citylist class, which is just an array list of cities... (This method is in server) public void listCities() { CityList LoadCities = new CityList(); String CityDetails ... dos.writeUTF(CityDetails); } catch (IOException ex) { Logger.getLogger(SocketHandler.class.getName()).log(Level.SEVERE, null, ex); } }
asked Feb 5 Ali 4.3k points
0 votes
1 answer 25 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 108 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 458 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 7 views
Problem: inputs the amount of minutes the user enters and calculates the grand total by adding the CostForMinutes and BasePlanCost. CostForMinutes is calculated by multiplying the minutes the user enters and the BaseCostPerMinute. The out is all the numbers outputted ... this error exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String can anyone help me out?
asked 2 days ago Ali 4.3k points
0 votes
1 answer 5 views
Problem: I'm getting the exception java.lang.ClassNotFoundException when I am trying to run my code, I'm getting Error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native ... ) at java.lang.Class.forName(Class.java:169) at Simple.MyProg.main(MyProg.java:15) What am I doing wrong?
asked 2 days ago Ali 4.3k points