I already defined the array of objects size. But whenever I try to set name for a customer an error happens . Here is my code :
public static void main(String[] args) {
int customerNum, mainChoice;
System.out.println("Enter Total Number of Customers Using your System : ");
Scanner input = new Scanner(System.in);
customerNum = input.nextInt();
Customer[] customersArray = new Customer[customerNum];
mainMenue();
mainChoice = input.nextInt();
if (mainChoice == 1) {
String askLoop = null;
for (int i = 0; i < customerNum; i++) {
System.out.println("Enter customer name : ");
customersArray[i].setName(input.next());
System.out.println("Enter customer phone number : ");
customersArray[i].setPhoneNumber(input.next());
System.out.println("Do you want to Add another Customer (y/n)");
askLoop = input.next();
if (askLoop == "y") {
continue;
} else {
break;
}
}
}
class Customer{
private String name;
public void setName(String name){
this.name = name;
}
}
When I run it then I input the name the program stops and this error is displayed :
Exception in thread "main" java.lang.NullPointerException at ba_1316855_p4.BA_1316855_P4.main(BA_1316855_P4.java:30)
How can I solve this ?