Hello guys! I'm trying to call a method in main called "buyProduct" which displays all products to the user. I' a getting a NullPointerException though. Here's what I have:
public static void main(String[] args) {
OnlineShopping shop = new OnlineShopping();
ArrayList<Cart> carts = new ArrayList<Cart>();
ArrayList<Order> orders = new ArrayList<Order>();
ArrayList<User> users = new ArrayList<User>();
ArrayList<Product> products = new ArrayList<Product>();
users.add(new User("Alfred Acosta", "alfred.acosta10@gmail.com", "dlsu1234", "Taft Ave, Manila", "09051234567", 500.00));
products.add(new Product("Shadow Blade", "User gains invisibility.", 2800.00, 15));
products.add(new Product("Blink Dagger", "User is able to teleport short distance.", 2250.00, 10));
products.add(new Product("Healing Salve", "Restores 400 health.", 110.00, 20));
products.add(new Product("Hand of Midas", "Gives user 190 gold.", 2050.00, 25));
Scanner sc = new Scanner(System.in);
System.out.println("1. Log-in. ");
System.out.println("2. Register. ");
System.out.print("Input: ");
int resp1 = sc.nextInt();
//User choice to 1.) Login or 2.) Register
switch(resp1){
case 1: shop.displayLogin(users);
break;
case 2: shop.displayRegistration(users);
break;
default: System.out.println("Incorrect input!");
break;
}
shop.displayUserMenu(users);
}
public void displayLogin(ArrayList<User> users){
Scanner sc = new Scanner(System.in);
String username = "", password = "";
System.out.println("-----------------------------");
System.out.print("Input username: ");
username = sc.next();
System.out.print("Input password: ");
password = sc.next();
for(int i=0;i<users.size();i++){
if(users.get(i).getUsername().equals(username) && users.get(i).getPassword().equals(password)){
System.out.println("-----------------------------");
System.out.println("Welcome back "+users.get(i).getName()+"!");
}
else {
System.out.println("Incorrect username or password!");
System.exit(0);
}
}
}
public void displayRegistration(ArrayList<User> users){
Scanner sc = new Scanner(System.in);
String regName,regUserName, regPassword, regAddress, regContactNo;
double regDeposit;
System.out.println("-----------------------------");
System.out.println("User registration");
System.out.println("Full name: ");
regName = sc.nextLine();
System.out.println("Username: ");
regUserName = sc.nextLine();
System.out.println("Password: ");
regPassword = sc.nextLine();
System.out.println("Address: ");
regAddress = sc.nextLine();
System.out.println("Contact number: ");
regContactNo = sc.nextLine();
System.out.println("Intial money deposit: ");
regDeposit = sc.nextDouble();
User user = new User(regName,regUserName, regPassword, regAddress, regContactNo, regDeposit);
for(int i=0;i<users.size();i++){
if(users.get(i).getName().equals(user.getName()) || users.get(i).getUsername().equals(user.getUsername()) || users.get(i).getContactNo().equals(user.getContactNo()))
System.out.println("User exists!");
else
System.out.println("Thank you for registering "+regName+"!");
}
users.add(user);
}
public void displayUserMenu(ArrayList<User> users){
Scanner sc = new Scanner(System.in);
System.out.println("-----------------------------");
System.out.println("1. Edit Profile");
System.out.println("2. Deposit Money");
System.out.println("3. Buy Product");
System.out.println("4. Remove Product");
System.out.println("5. Check-out Cart");
System.out.print("Input: ");
int resp = sc.nextInt();
switch(resp){
case 1:
users.get(0).displayProfile();
users.get(0).editProfile();
break;
case 2: users.get(0).depositMoney();
break;
case 3: users.get(0).buyProduct();
default: System.out.println("Incorrect input!");
break;
}
}
}
And this is my User class:
public class User {
private String name;
private String username;
private String password;
private String address;
private String contactNo;
private double deposit;
private ArrayList<Cart> cart;
private ArrayList<Product> products;
public User(String name, String username, String password, String address, String contactNo, double deposit) {
this.name = name;
this.username = username;
this.password = password;
this.address = address;
this.contactNo = contactNo;
this.deposit = deposit;
}
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public double getDeposit() {
return deposit;
}
public void setDeposit(double deposit) {
this.deposit = deposit;
}
public void displayProfile(){
System.out.println("-----------------------------");
System.out.println("Full Name: " +name);
System.out.println("Username: " +username);
System.out.println("Password: " +password);
System.out.println("Address: " +address);
System.out.println("Contact Number: " +contactNo );
System.out.println("Initial Money Deposit: "+deposit );
}
public void editProfile(){
Scanner sc = new Scanner(System.in);
System.out.println("-----------------------------");
System.out.println("Full Name: ");
this.name = sc.nextLine();
System.out.println("Password: ");
this.password = sc.nextLine();
System.out.println("Address: ");
this.address = sc.nextLine();
System.out.println("Contact Number: ");
this.contactNo = sc.nextLine();
displayProfile();
}
public double depositMoney(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter amount to deposit: ");
double amount = sc.nextFloat();
this.deposit += amount;
return deposit;
}
public void buyProduct(){
for(int i=0; i<products.size(); i++)
products.get(i).getName();
}
public void removeProduct(){
}
public void checkOut(){
}
}