Assign the return value to the new variable (Java)

It's been a while since I did some java coding

I need to build an application for the business that needs to be automated (part of the workshop), but this has nothing to do with my problem

I'm stuck online: customerlist Add (customer)// (part of the addcustomer method in the wcia class) this is also the first time I was told to "assign the return value to the new variable" as part of the error, so I'm not sure what this means

Code: Main

import java.util.ArrayList;


public class WCIA {

    private final ArrayList customerList = null;

    public static void main(String[] args) {

        short s =002;


        Customer arno = new Customer();
        arno.setName("Arno");
        arno.setId(s);
        arno.setEmail("arnomeye@gmail.com");
        arno.setAddress("Somewhere");
        arno.setPhoneNum("0727855201");
         System.out.printf("%s",arno.getEmail());
     WCIA wcia = new WCIA();

     wcia.addCustomer(arno);
     wcia.displayCustomers();
    }

    public void addCustomer (Customer customer)
    {
        customerList.add(customer); // <---Problem over here
    }
    public void displayCustomers()
    {
        for(int x=0;x<customerList.size();x++)
        {
            Customer cus = (Customer) customerList.get(x);
            cus.DisplayCustomer();
        }
    }
}

Code: customer class:

public class Customer {

   private short id;
   private String name;
   private String email;
   private String phoneNum;
   private String address;

  public Customer()
  {

    System.out.println("Class initiated");
  }




    public void DisplayCustomer()
    {
        System.out.append("Name : "+ name+"\n");
        System.out.append("ID : "+ id+"\n");
        System.out.append("Email : "+ email+"\n");
        System.out.append("Phone Number : "+ phoneNum+"\n");
        System.out.append("address : "+ address+"\n");
    }

    public void setId(short id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public short getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public String getAddress() {
        return address;
    }

}

Solution

You need to instantiate ArrayList before you can assign elements to it You may be getting NullPointerException, which is my guess

Change this line:

private final ArrayList customerList = null;

to

private final ArrayList customerList = new ArrayList();

At least this problem should be solved I didn't read the rest of your code, so I'm not sure if there are other problems

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>