0

First of all, I did research my issue on Stackoverflow and found an answer as to WHAT the NullPointerException is. However, I do not understand WHY I have a NullPointerException.

This code creates a simple grade tracking program. The following are data fields:

private String name;
private double[] testScores; //relevant to question  
private int x = 0;
private String answer;
private double testAverage;
private double tScore;//Relevant to question

So the Exception that I got occured when a Student object called this method. After I commented out the line with the array in the method definition, the Exception ceased to appear. I replaced the array with double tScore and the program started working fine. I do not understand why the use of an array gave me a NullPointerException. Please help me I am so confused by this issue. I tried to figure it out, but do not understand WHY this happened. Thanks for your help.

public void inputGrades()
  {
       Scanner gradeRead = new Scanner(System.in);
       System.out.println("Please Enter Student Grades.");

       do
       { 
           //testScores[x] = gradeRead.nextDouble(); ---Problem Line---
           tScore = (gradeRead.nextDouble());
           System.out.println("Are There Any More Grades? Enter y or n");
           answer = gradeRead.next();
           x++;
       }
       while(answer.equalsIgnoreCase("Y"));
  }
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Ungeheuer
  • 1,393
  • 3
  • 17
  • 32
  • arraylist is better than array in this case – Madhawa Priyashantha Nov 07 '14 at 01:35
  • @SotiriosDelimanolis what the heck man? I already told you I did my research, but there is no question I found that relates to arrays and data fields. Do me a favor and get rid of the duplicate mark so I can get some answers. – Ungeheuer Nov 07 '14 at 01:37
  • @fastsnail what is an arraylist? I am a high schooler and new to java. I have never heard of an arraylist. – Ungeheuer Nov 07 '14 at 01:38
  • testScores is a array so it should have fixed length.in arraylist you can increase it's length – Madhawa Priyashantha Nov 07 '14 at 01:41
  • The duplicate explains what a `NullPointerException` is and why it gets thrown. You've already identified that your problem happens with the `double[]`. The duplicate answers explain why and how to fix it. Just read them. – Sotirios Delimanolis Nov 07 '14 at 01:48
  • @SotiriosDelimanolis I know what causes the `NullPointerException` but I dont understand what caused it in my scenario. An answer would be helpful and could help someone else as well. At least answer my question and explain WHY I had an Exception before marking this a duplicate. – Ungeheuer Nov 07 '14 at 02:22
  • 1
    If you read the duplicate, you learn that a `NullPointerException` occurs for a single reason, dereferencing a `null` value. So something has to be `null` in your scenario. That something is `testScores`. The linked duplicate question/answer in this case is a _canonical_ question/answer. It is very rarely useful for others to debug one person's problem. It is much more useful to understand what causes a NPE and how to handle it. _Give a man a fish, he'll eat for a day. Teach a man to fish, and he'll eat for a lifetime._ – Sotirios Delimanolis Nov 07 '14 at 02:28
  • 1
    Marking a question as a duplicate does not make the question _bad_. – Sotirios Delimanolis Nov 07 '14 at 02:29
  • @fastsnail how would I utilize the arraylist in this case. I checked out the API, but don't understand how to use it in this case. – Ungeheuer Nov 07 '14 at 02:31
  • @JohnnyCoder do you know the SIZE_YOU_DESIRE ???and it's depend on user input – Madhawa Priyashantha Nov 07 '14 at 02:34
  • @fastsnail in the scenario my teacher gave us, there are only two grades that go into the array. I wanted to make this code as open as possible in case you wanted to put 5 test scores instead of two, which is part of the reason that I did not finish the declaration/instantiation of the array. The original assignment calls for using 2 `double` variables, but I chose an array because of its ability to hold large amounts of data, however, I have no idea how the user can adjust that while running the program. – Ungeheuer Nov 07 '14 at 02:38
  • @JohnnyCoder you can't edit size of a array while runing .that's why i told you use arraylist .but if you are not llowed to use arraylist you can set SIZE_YOU_DESIRE to 5 .but then if user input less then 5 data you are wasting memory . – Madhawa Priyashantha Nov 07 '14 at 02:44
  • @fastsnail I apologize, my comment was misleading. I am going above and beyond the assignment parameters. The assignment says there are only two test grades, but I decided to create an array because there is no teacher in the world that only gives two tests. I did this to make my code as general as possible. I find arrayList very useful in concept, but I need to apply it to my code. I appreciate the suggestion and will use it. – Ungeheuer Nov 10 '14 at 17:44

1 Answers1

3

You have just declared the array and never initialised it as mentioned here:

private double[] testScores; //relevant to question  

You need to initialised it before accessing its elements. Something like:

   testScores = new double[SIZE_YOU_DESIRE];
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136