Java 爪哇语;NullPointerException“;在尝试打印数组元素时

Java 爪哇语;NullPointerException“;在尝试打印数组元素时,java,arrays,loops,Java,Arrays,Loops,头等舱哪一个是主舱 public class JavaMethod { public static void main (String[] args){ JavaMethod javaMethod = new JavaMethod(); javaMethod.makeChoice(); } public void makeChoice(){ Scanner console = new Scanner(System.in

头等舱哪一个是主舱

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();
        }
    }
}
存储值的第二类

public class StudentInformation {

    String name;
    String grade;
}
这是我的服务课,我有问题。在运行printStudent()方法时;对于该类,我得到以下错误: 线程“main”java.lang.NullPointerException中出现异常 在stcasignment.Student.printStudent(Student.java:76) 在stcasignment.Student.makeChoice(Student.java:31) 在stcasignment.Student.addStudent(Student.java:66) 在stcasignment.Student.makeChoice(Student.java:28) 在stcasignment.JavaMethod.makeChoice(JavaMethod.java:25) 位于stcasignment.JavaMethod.main(JavaMethod.java:12) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中 位于java.lang.reflect.Method.invoke(Method.java:606) 位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

公共班级学生{
int i=0;
学生信息[]学生信息=新学生信息[0];
int makeChoice(){
扫描仪控制台=新扫描仪(System.in);
i=i+1;
学生信息=新的学生信息[i];
System.out.println(“是否要继续(Y/N”);
字符串yesNo=console.nextLine();
如果(是非同等信号情况(“Y”)){
系统输出打印项次(“添加或打印(应付)”;
字符串addPrint=console.nextLine();
if(addPrint.equalsIgnoreCase(“A”)){
addStudent();
}else if(addPrint.equalsIgnoreCase(“P”)){
打印学生();
}否则{
System.out.println(“无效”);
makeChoice();
}
}否则,如果(是,则不存在同等信号情况(“N”)){
系统出口(0);
}否则{
System.out.println(“无效”);
makeChoice();
}
返回i;
}
公共图书馆(学生){
int i=0;
StudentInformation info=新的StudentInformation();
扫描仪控制台=新扫描仪(System.in);
System.out.println(“输入学生姓名”);
info.name=console.nextLine();
学生信息[i]=信息;
System.out.println(“输入学生成绩”);
info.grade=console.nextLine();
学生信息[i]=信息;
i=i+1;
makeChoice();
}
公共图书馆(学生){

对于(int j=0;j检查
info.name
info.grade
是否可以是空指针:

if (info != null) {
    System.out.println("Student name:"+info.name);
    System.out.println("Student grade:"+info.grade);
} else System.out.println("Student information is null.");

另一种可能是名称和等级未初始化。请确保对所有StudentInformation对象都这样做,例如,在声明时将它们设置为默认值。

由于
字符串名称
为空,如果您在初始化之前尝试使用它,它将抛出NPE。用户可能能够rst在“制作”之前打印学生信息
Student
。你实例化了一个
Student
对象,但在打印时没有给
name
赋值,因为这是你打印的第一件事,它会在到达打印代码的其余部分之前抛出一个NPE。你可以在那里检查
name
或你的类的任何其他字段是否在打印或尝试打印任何内容之前,de>null

您还将首先实例化一个空的
StudentInformation
类,然后创建另一个类。因此,两个
StudentInformation
对象可能为空

在这里:

StudentInformation[]StudentInformation=新的StudentInformation[0];

在这里:


阅读您的说明后:

  • 输入选择要添加的学生、教师或学院信息
  • 然后,您是否要继续(是/否),再次获取输入
  • 再次输入,询问您是否要在学生、学院或教师中添加或打印
  • 添加或打印后,再次询问是否要继续(是/否)
  • 我坚持我先前的意见,即您的代码偏离了基准。我建议您:

  • 创建一个学生类,该类旨在保存单个学生的信息、姓名、身份证号码、成绩或其他所需信息
  • 给它一个构造函数,将此信息传递到类中
  • 给它一些getter方法,也许还有setter方法
  • 不要给它一个StudentInformation数组或任何不属于单个学生的字段
  • 在您的驱动程序类中,使用main方法完成Scanner的所有输入输出和使用

    • 也许你的学生信息课应该改名为学生
    • 您的驱动程序类,即与用户交互的驱动程序类,应该有一个初始化为
      ArrayList
      列表。它应该有一个类似的教师列表
    • 你的学生类不应该有一个数组来保存关于其他学生的信息,而你当前的代码就是这样做的
    • 您当前的代码将重新创建一个新的Student类,该类在每次运行时都会重新创建其数组,因此数组中存储的任何以前的信息都将丢失,这是行不通的
    • 同样,你应该重新开始

    每次创建
    新的学生信息[i]
    时,都会覆盖上一个学生信息数组,但不会复制其值

    这意味着第一个
    makeChoice()
    变量之后的
    studentInformation
    变量将包含:

    [StudentInformation<--some data-->]
    
    [null, StudentInformation<--new data-->]
    
    第十次呼叫后,将显示:

    [null, null, null, null, null,...,StudentInformation<--some new data-->]
    
    [null,null,null,null,null,…,学生信息]
    

    然后,当您打印它时,它将导致NLE。

    这与您的NPE无关,但您的代码看起来可能偏离了轨道。例如,学生类中不应包含输入输出代码,也不应包含您已创建的数组
    [null, StudentInformation<--new data-->]
    
    [null, null, null, null, null,...,StudentInformation<--some new data-->]