1

I have this code:

int[][] stuGrades = {{97, 64, 75, 100, 21}}; //extract from data file

String[][] HWdata; // original data file
for (int g = 0; g < stuGrades.length; g++) {
    for (int p = 0; p < stuGrades[0].length; p++) {
        int tempScores = stuGrades[g][p];
        if (tempScores <= 100 && tempScores > 98.1) {
            stuGpa[g][p] = 4.0;
        }
        else if (tempScores <= 98 && tempScores > 96.1) {
            stuGpa[g][p] = 3.9;
        }
    }
}

My goal is to convert the grades array {97, 64, 75, 100, 21} to a new GPA array, which will convert the score to 4.0, 3.9 or something else. I got this error:

Exception in thread "main" java.lang.NullPointerException at homework7.main.

How can I resolve this issue?

pushkin
  • 9,575
  • 15
  • 51
  • 95
Mark Yo
  • 49
  • 6
  • Thanks, but I didn't point at nothing. I give specific value to it. – Mark Yo May 06 '18 at 16:27
  • 1. Do you perhaps have a stacktrace for us? 2. Is it possible that you never initialized `stuGpa` or `HWdata`? – Harjan May 06 '18 at 16:52
  • I set both stuGpa and HWdata are null, but HWdata is used to storage data from a file. then I use stuGpa to collect the HWdata I need. I debug HWdata, the data was there. – Mark Yo May 06 '18 at 17:07

1 Answers1

1

You probably didn't initialized stuGpa correctly as MalumAtire832 pointed out.

int[][] stuGrades = {{97, 64, 75, 100, 21}}; //extract from data file

double[][] stuGpa = new double[stuGrades.length][stuGrades[0].length];

String[][] HWdata; // original data file
for (int g = 0; g < stuGrades.length; g++) {
    for (int p = 0; p < stuGrades[0].length; p++) {
        int tempScores = stuGrades[g][p];
        if (tempScores <= 100 && tempScores > 98.1) {
            stuGpa[g][p] = 4.0;
        } else if (tempScores <= 98 && tempScores > 96.1) {
            stuGpa[g][p] = 3.9;
        }
     }
}
Aaron Stein
  • 526
  • 7
  • 18