我实例化了一个类,以便将要输入的信息存储到其中。但是我很难显示存储的信息。当我编译时会说我还没有初始化变量。
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 = new Student("", Null, dummyObject);