Ok, this is the partial of my code:
At the main function, I've declared the student array. When I actually try to initialize that object (as an element of an array), it gives me the nullpointer exception. Can anybody help me?
This is the error message:
Just to add something, student class works just fine and the program compiles gracefully.
Code:
import javax.swing.JOptionPane;
import java.io.*;
public class Problem
{
private static final int START=0, MAXIMUMSTUDENTS=25;
private static int getOption,numOfStudents;
private static Student students[];
public static void main(String[] args)
{
Student[] students = new Student[MAXIMUMSTUDENTS];
numOfStudents=0;
insert();
}
public static void insert()
{
int x,ID,age;
double GPA;
char gender;
String name;
ID = Integer.parseInt(JOptionPane.showInputDialog("Enter the studentID: "));
x = ID;
name = JOptionPane.showInputDialog("Name: ");
gender = JOptionPane.showInputDialog("Gender: ").charAt(0);
age = Integer.parseInt(JOptionPane.showInputDialog("Age: "));
GPA = Double.parseDouble(JOptionPane.showInputDialog("GPA: "));
students[numOfStudents] = new Student(name, gender,age, ID, GPA);
numOfStudents++;
}
}
At the main function, I've declared the student array. When I actually try to initialize that object (as an element of an array), it gives me the nullpointer exception. Can anybody help me?
This is the error message:
Exception in thread "main" java.lang.NullPointerException
at Problem.insert(Problem.java:36)
at Problem.main(Problem.java:18)
Just to add something, student class works just fine and the program compiles gracefully.