Say I have the class:
package school;
public class SchClass {
private Student[] students;
private int numStudents = 0;
public SchClass() {
}
public void addStudent(Student s) {
this.students[this.numStudents] = s;
this.amountStudents++;
}
}
and I am trying to run this in another class:
import school.SchClass;
import school.Student;
import school.Tutor;
public class JavaTesting {
public static void main(String[] args) {
Student s = new Student();
Tutor t = new Tutor();
SchClass shc = new SchClass();
sch.setTutor(t);
sch.addStudent(s);
}
}
When I do this, it reports this NullPointerException:
Exception in thread "main" java.lang.NullPointerException
at school.SchClass.addStudent(SchClass.java:8)
at javatesting.JavaTesting.main(JavaTesting.java:10)
What is wrong? I'm sure I coded this perfect but it still reports an error.