我在名爲actors的單獨類中有一個名爲「filmList」的ArrayList,我想在切換中使用第2種情況調用newActor.filmList.get(ⅰ);但我不斷收到編譯器的錯誤object may not be initialized
。如果我把同一行放在開關盒1中,它工作正常,但在情況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;
}
}
}
}
請在問題文本中使用格式和英語,但不清楚您要求的內容 – iluxa
_...我的講師在解釋>東西時很糟糕 - 他不是唯一一個 –
道歉,我不確定這裏的格式化是如何工作的。 – Nodstuff