I have an ArrayList called "filmList" in a separate class called actors, I want to use case 2 in the switch to call newActor.filmList.get(i); but I keep getting object may not be initialized
errors from the compiler. If I put the same line in switch case 1 it works fine, but in case 2 i get the error. Can someone please tell me how I can call a method on the object from outside where the constructor creates the newActor object, I will be eternally grateful, it is doing my head in and my lecturer is terrible at explaining things. Thanks.
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;
}
}
}
}