我在一个名为 actor 的单独类中有一个名为“filmList”的 ArrayList,我想在 switch 中使用 case 2 来调用 newActor.filmList.get(i); 但我不断object may not be initialized
从编译器收到错误。如果我将同一行放在 switch case 1 中它工作正常,但在 case 2 中我得到错误。有人可以告诉我如何从构造函数创建 newActor 对象的外部调用对象上的方法,我将永远感激不已,它正在努力工作,而我的讲师在解释事情方面很糟糕。谢谢。
public class ActorsMain {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String fName="";
String lName="";
String address="";
int age;
String filmEntry="";
String code="";
ArrayList actors = new ArrayList();
ArrayList films = new ArrayList();
int choice=0;
while(choice!=3){
System.out.println("Make your selection.\n1. Add Actor\n2. List Actors");
choice=kb.nextInt();
switch (choice){
case 1:
System.out.println("\nPlease enter the actors first name:");
fName=kb.next();
System.out.println("Please enter the actors second name:");
lName=kb.next();
System.out.println("Please enter the actors address:");
address=kb.next();
System.out.println("Please enter the actors age:");
age=kb.nextInt();
kb.nextLine();
for(int a=0;a<10;a++){
System.out.println("Please enter the actors film.\nType 'Stop' to stop entering films.");
filmEntry=kb.nextLine();
//kb.nextLine();
if(filmEntry.equalsIgnoreCase("stop")){
break;
}
else {
Films filmObject = new Films(filmEntry,code);
films.add(filmObject);
}
}
Actors newActor = new Actors(fName,lName,address,age);
actors.add(newActor);
newActor.setFilm(films);
films.clear();
break;
case 2:
break;
}
}
}
}