0

I am currently trying to create a program that will ask user for number of classes the user is enrolled in, make an array to store that data, and then ultimately display all that information in a print statement. I am not worried about the final print statement but I just cant seem to make the array work. There are no errors in the program but once it asks me to input class number, It returns "Exception in thread "main" java.lang.NullPointerException at GPAreport.main(GPAreport.java:24)"

Heres the code:

import java.util.*;
public class GPAreport {
public static void main (String [] args) { 
int classnumber[] = null; 
double gpa[];
String ClassDescription[] = null;
Scanner myScanner = new Scanner(System.in); 
String LetterGrade = "";
double GradePoints = 0;
double PercentageGrade = 0;
String currentterm, studentname = "";
int numberofclasses;

System.out.println("Please enter the current term (ex: fall 2011):");
currentterm = myScanner.nextLine();
System.out.println("Please enter Student Name:");
studentname = myScanner.nextLine();
System.out.println("Please enter number of classes:");
numberofclasses = myScanner.nextInt();

for(int i = 0; i <= numberofclasses ; i++){ 
    System.out.println("Input Class Number: " + (i+1));
    classnumber[i] = myScanner.nextInt();
    System.out.println("Input class description: " + (i+1));
    ClassDescription[i] = myScanner.nextLine();
    System.out.println("Input percentile grade for: " + classnumber[i]);
    PercentageGrade = myScanner.nextDouble();

        if ((PercentageGrade < 100) && (PercentageGrade >= 93 )){
        LetterGrade = "A";
        GradePoints = 4.0; 
            }
        else if ((PercentageGrade > 100)){ 
        LetterGrade = "A+";
        GradePoints = 4.0;
            }
        else if ((PercentageGrade < 93) && (PercentageGrade >= 90 )){ 
        LetterGrade = "A-";
        GradePoints = 4.0;
            }
        else if ((PercentageGrade < 90) && (PercentageGrade >= 83 )){ 
        LetterGrade = "B";
        GradePoints = 3.0;
            }
        else if ((PercentageGrade < 83) && (PercentageGrade >= 80 )){ 
        LetterGrade = "B-";
        GradePoints = 3.0;
            }
        else if ((PercentageGrade < 80) && (PercentageGrade >= 73 )){ 
        LetterGrade = "C";
        GradePoints = 2.0;
            }
        else if ((PercentageGrade < 73) && (PercentageGrade >= 70 )){ 
        LetterGrade = "C-";
        GradePoints = 2.0;
            }
        else if ((PercentageGrade < 70) && (PercentageGrade >= 63 )){ 
        LetterGrade = "D";
        GradePoints = 1.0;
            }
        else if ((PercentageGrade < 63) && (PercentageGrade >= 60 )){ 
        LetterGrade = "D-";
        GradePoints = 1.0;
            }
        else if ((PercentageGrade < 60) && (PercentageGrade >= 0)){ 
        LetterGrade = "F";
        GradePoints = 0;
            }
        else if (PercentageGrade < 0){ 
        System.out.println("That is not a valid score. Please try again");
            }

}

}

}

Bryce Sickles
  • 13
  • 1
  • 1
  • 2
  • Which line is line 24 of your program? If you've done any searching on NullPointerExceptions, you would know that it gives you the offending line number, here `GPAreport.java:24`, and that that line is where you should look. – Hovercraft Full Of Eels Apr 09 '15 at 16:54
  • OK here's the obvious problem: `String ClassDescription[] = null;`!! You can't use an array until you create it first. And same for the classnumber array. Assign an array to the variable before trying to use it. – Hovercraft Full Of Eels Apr 09 '15 at 16:56
  • Line 24 is classnumber[i] = myScanner.nextInt(); – Bryce Sickles Apr 09 '15 at 16:56

0 Answers0