在此程序中,您被要求输入S C或T并再次要求Y继续,N结束。 (直到这里它很好)。之后,如果输入A,则添加信息,否则为P以打印信息。当我输入A部分时,它会出现此错误
Exception in thread "main" java.lang.NullPointerException
at Assignment07.JavaMethod.makeChoiceThree(JavaMethod.java:84)
at Assignment07.JavaMethod.makeChoiceTwo(JavaMethod.java:67)
at Assignment07.JavaMethod.makeChoice(JavaMethod.java:34)
at Assignment07.JavaMethod.main(JavaMethod.java:19)
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)
package Assignment07;
import java.util.Scanner;
/**
* Main class
*/
`public class JavaMethod {
Information info = new Information();
Scanner console = new Scanner(System.in);
Service service = new Service();
public static void main(String[] args) {
JavaMethod javaMethod = new JavaMethod();
javaMethod.makeChoice();
}
//method to make choice on S,T or C
public void makeChoice() {
JavaMethod javaMethod = new JavaMethod();
System.out.println("Enter S for Student, T for teacher and C for College");
info.choice = console.nextLine();
switch (info.choice) {
case "S":
javaMethod.makeChoiceTwo();
break;
case "T":
javaMethod.makeChoiceTwo();
break;
case "C":
javaMethod.makeChoiceTwo();
break;
default:
System.out.println("Invalid!!! Please enter again.");
javaMethod.makeChoice();
}
}
//method to make choice Yes or No
public void makeChoiceTwo() {
//object of class JavaMethod
JavaMethod javaMethod = new JavaMethod();
System.out.println("Enter Y to continue and N to exit");
info.yesNo = console.nextLine();
switch (info.yesNo) {
case "Y":
javaMethod.makeChoiceThree();
case "N":
break;
}
}
//method to make choice Add or Print
public void makeChoiceThree() {
System.out.println("Enter P to print or A to Add information");
info.addPrint = console.nextLine();
if (info.addPrint.equalsIgnoreCase("A") && info.choice.equalsIgnoreCase("S")) {
service.setStudentInformation();
} else if (info.addPrint.equalsIgnoreCase("A") && info.choice.equalsIgnoreCase("T")) {
service.setTeacherInformation();
} else if (info.addPrint.equalsIgnoreCase("A") && info.choice.equalsIgnoreCase("C")) {
service.setCollegeInformation();
} else if (info.addPrint.equalsIgnoreCase("P") && info.choice.equalsIgnoreCase("S")) {
service.printStudentInformation();
} else if (info.addPrint.equalsIgnoreCase("P") && info.choice.equalsIgnoreCase("T")) {
service.printTeacherInformation();
} else if (info.addPrint.equalsIgnoreCase("P")&& info.choice.equalsIgnoreCase("C")) {
service.printCollegeInformation();
} else {
System.out.println("Invalid");
makeChoiceThree();
}
}
}`
package Assignment07;
/**
* I've init my variables here
*/
`public class Information {
String choice;
String yesNo;
String addPrint;
}`
package Assignment07;
import java.util.Scanner;
/**
* This is the service class which adds and prints information
*/
`public class Service {
StudentInformation studentInformation = new StudentInformation();
TeacherInformation teacherInformation = new TeacherInformation();
CollegeInformation collegeInformation = new CollegeInformation();
//method to get information
public void setStudentInformation (){
Scanner console = new Scanner(System.in);
System.out.println("Enter the name of Student");
studentInformation.name = console.nextLine();
System.out.println("Enter the address of Student");
studentInformation.address = console.nextLine();
System.out.println("Enter the grade of Student");
studentInformation.grade = console.nextLine();
System.out.println("Enter the Roll Number of Student");
studentInformation.rollno = console.nextLine();
System.out.println("Enter the contact of Student");
studentInformation.contact = console.nextLine();
}
//method to get information
public void setTeacherInformation(){
Scanner console = new Scanner(System.in);
System.out.println("Enter the name of Teacher");
teacherInformation.name = console.nextLine();
System.out.println("Enter the address of Teacher");
teacherInformation.address = console.nextLine();
System.out.println("Enter the grade of Teacher");
teacherInformation.subject = console.nextLine();
System.out.println("Enter the contact of Teacher");
teacherInformation.contact = console.nextLine();
}
//method to get information
public void setCollegeInformation (){
Scanner console = new Scanner(System.in);
System.out.println("Enter the name of College");
collegeInformation.name = console.nextLine();
System.out.println("Enter the address of College");
collegeInformation.address = console.nextLine();
System.out.println("Enter the grade of College");
collegeInformation.contact = console.nextLine();
}
//method to print information
public void printStudentInformation (){
System.out.println("Student Name:"+ studentInformation.name);
}
//method to print information
public void printTeacherInformation (){
System.out.println("Teacher Name:"+ teacherInformation.name);
}
//method to print information
public void printCollegeInformation (){
System.out.println("College Name:"+ collegeInformation.name);
}
}`
为什么我会收到这样的错误?我该怎么解决呢