Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 实例化对象用于显示另一个类中存储的信息_Java_Oop_Instantiation - Fatal编程技术网

Java 实例化对象用于显示另一个类中存储的信息

Java 实例化对象用于显示另一个类中存储的信息,java,oop,instantiation,Java,Oop,Instantiation,我实例化了一个类,以便将要输入的信息存储到其中。但是我在显示存储的信息时遇到了困难。当我编译时,它会说我还没有初始化变量 public class REPORTS { public static void main(String[]args) { Scanner input = new Scanner(System.in); int x=choices(); STUDENT stud; EQUIPMENT eq

我实例化了一个类,以便将要输入的信息存储到其中。但是我在显示存储的信息时遇到了困难。当我编译时,它会说我还没有初始化变量

    public class REPORTS
{
   public static void main(String[]args)
   {
      Scanner input = new Scanner(System.in);
        int x=choices();

         STUDENT stud;
         EQUIPMENT equip;
         RESERVATION reserve;
        switch(x)
        {
            case 1:
            { 
              // Code here for input

               stud = new STUDENT(Studid,Studname,Studcourse,Studlevel);

               break;
            }
            case 2:
            { 
               // Code also here for input
               equip = new EQUIPMENT(eqpmntid,qty,eqpmntname);
               break;
            }
            case 3:
            {
               // Same goes for here input
               reserve = new RESERVATION(studentid,equipid1,reservationdate,returndate);
               break;
            }
           case 4:
            {

               stud.display(); // error here variable might not have been initialized
               break;
            }      
           case 5:
            {

               equip.display(); // same goes here
               break;
            }
         case 6:
            {
               reserve.display(); // and also here
               break;           
            }
        }
以下是详细的错误消息:


看来你必须改变你的程序行为;有一种可能的语法方法可以在代码开始时将新对象强制转换为Student,但可能会出现运行时错误。若要更改行为,您可以使用null或空的参数初始化对象,或者根据构造函数获得的参数初始化虚拟对象,然后在相关的开关情况下将字段设置为适当的值;例如,您的第一行应该如下所示:

Student stud=新学生(“”,Null,dummyObject);

这将导致运行时错误。表示它无法转换或无法将类对象强制转换为学生