First Class which is the main class
public class JavaMethod {
public static void main (String[] args){
JavaMethod javaMethod = new JavaMethod();
javaMethod.makeChoice();
}
public void makeChoice(){
Scanner console = new Scanner(System.in);
System.out.println("Enter S, T or C");
String stc = console.nextLine();
switch (stc){
case "S":
Student student = new Student();
student.makeChoice();
break;
case "T":
Teacher teacher = new Teacher();
teacher.makeChoice();
break;
case "C":
College college = new College();
college.makeChoice();
break;
default:
System.out.println("Invalid");
makeChoice();
}
}
}
Second class to store values
public class StudentInformation {
String name;
String grade;
}
This is my service class where I've the problem. While running the method printStudent(); of this class i get following error: Exception in thread "main" java.lang.NullPointerException at STCAssignment.Student.printStudent(Student.java:76) at STCAssignment.Student.makeChoice(Student.java:31) at STCAssignment.Student.addStudent(Student.java:66) at STCAssignment.Student.makeChoice(Student.java:28) at STCAssignment.JavaMethod.makeChoice(JavaMethod.java:25) at STCAssignment.JavaMethod.main(JavaMethod.java:12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
public class Student {
int i =0;
StudentInformation[] studentInformation = new StudentInformation[0];
int makeChoice(){
Scanner console = new Scanner(System.in);
i = i +1;
studentInformation = new StudentInformation[i];
System.out.println("Do you want to continue(Y/N");
String yesNo = console.nextLine();
if (yesNo.equalsIgnoreCase("Y")){
System.out.println("Add or Print(A/P)");
String addPrint = console.nextLine();
if (addPrint.equalsIgnoreCase("A")){
addStudent();
} else if (addPrint.equalsIgnoreCase("P")){
printStudent();
} else {
System.out.println("Invalid");
makeChoice();
}
}else if (yesNo.equalsIgnoreCase("N")){
System.exit(0);
} else {
System.out.println("Invalid");
makeChoice();
}
return i;
}
public void addStudent(){
int i = 0;
StudentInformation info = new StudentInformation();
Scanner console = new Scanner(System.in);
System.out.println("Enter Students name");
info.name = console.nextLine();
studentInformation[i]=info;
System.out.println("Enter Students grade");
info.grade = console.nextLine();
studentInformation[i]=info;
i = i + 1;
makeChoice();
}
public void printStudent(){
for (int j =0; j<studentInformation.length; j++){
StudentInformation info = studentInformation[j];
System.out.println("Student name:"+info.name);
System.out.println("Student grade:"+info.grade);
}
makeChoice();
}
}
How to solve it?