I have created three initializer arrays that must be used in the 'students' array, which should be an array of student objects. Every time it runs, a NullPointer error appears.
class School
{
private Student[] students;
private int size;
public School (int s)
{size = s;}
public void addData()
{
String[] name = {"Tom", "Ann", "Bob", "Jan", "Joe", "Sue", "Jay", "Meg", "Art", "Deb"};
int[] age = {21, 34, 18, 45, 27, 19, 30, 38, 40, 35};
double[] gpa = {1.685, 3.875, 2.5, 4.0, 2.975, 3.225, 3.65, 2.0, 3.999, 2.125};
for(int i = 0; i < size; i++){
students[i] = new Student(name[i], age[i], gpa[i]);
}
}
public String toString()
{
String s = "";
for(int i = 0; i < size; i++){
s = s + students[i].toString();
}
return s;
}
}
class Student
{
private String name;
private int age;
private double gpa;
public Student (String n, int a, double g)
{
name = n;
age = a;
gpa = g;
}
public String getName() { return name; }
public int getAge() { return age; }
public double getGPA() { return gpa; }
public String toString()
{
String temp = name + " " + age + " " + gpa + "\n";
return temp;
}
}
Where does the problem exactly begin and how would it be resolved?