First of all, thank you for taking the time to read!
So, I created a superclass called Phone and made three subclasses LG, Samsung and Huawei. I was trying to do this MVC-style so I went to the controller package to test out my code, imported all created classes so I could instantiate them with entering a String which would specify what subclass to create (did this using a switch). After running the program and entering names correctly, I got a NullReference. I opened debug and found that 'p', the variable used to loop, never got instantiated.
How comes? Any help is greatly appreciated!
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Phone[] phones = new Phone[3];
for(Phone p : phones){
System.out.println("What brand?");
String inputBrand = scan.nextLine();
switch(inputBrand) {
case "LG" : p = new LG();
break;
case "Samsung" : p = new Samsung();
break;
case "Huawei" : p = new Huawei();
break;
default : System.out.println("You're an idiot");
break;
}
}
for(Phone p : phones){
System.out.println(p.toString());
}
scan.close();
}