0
 import java.util.HashMap;
 import java.util.Scanner;
 import java.io.*;

 public class FillATable {
 static Scanner console = new Scanner(System.in);
 static Scanner input = new Scanner(System.in);
 static String BaseGrades;
 static String BaseInstructor;
 static HashMap<String, Integer> Grades;
 static HashMap<String, String> Instructor;
 static double GPA;
 static int ID;
 static String status;
 static int Number;
 static File html;

    public static void main(String[] args) throws IOException {
    System.out.println("Full Name ?");
    fullname = console.nextLine();

    System.out.println("Number of courses taken ?");
    Number = console.nextInt();
    System.out.println("Please input the courses taken along with the 
    respective grades ");
    for (int i = 0; i <= Number; i++) {
        Grades.put(console.next(), console.nextInt());
    }

    System.out.println("Please input the courses taken along with the 
    respective instructors");
    for (int i = 0; i < Number; i++) {
        Instructor.put(console.nextLine(), console.nextLine());
    }

    System.out.println("GPA ?");
    GPA = input.nextDouble();

    System.out.println("ID number ?");
    ID = input.nextInt();

    for (String course : Instructor.keySet()) {
        String value = Instructor.get(course);
        BaseInstructor = BaseInstructor + "( " + course + "," + value + ")";

    }
    for (String course : Grades.keySet()) {
        Integer value = Grades.get(course);
        BaseGrades = BaseGrades + "( " + course + "," + value + ")"

      }

I get a null pointer exception when when i enter the course and grade any ideas why ? It seems straight forward there must be something i am missing but I have no idea what it is . For the purpose of my work the variables at the beginning should stay static at all times

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

0

You need to have this line somewhere before you attempt to interact with Grades:

grades = new HashMap<String, Integer>();

I would put it right at the start of your main method or when your first declare the variable. This is true for all other class-level variables you haven't initialised yet such as Instructor.

This is because any object you have declared but not set to an initial value will have a value of null. This is true for basically every variable you declare in any Java you write.

If you attempt to use a method or member of an object that has a value of null, you will get a NullPointerException.

Hope this helps!

Brett Jeffreson
  • 573
  • 5
  • 19