Assign return value to new variable (Java)

some time has passed since I made several java encodings.

I need to create a business application that requires automation (part of a workshop), which, however, is not relevant to my question ...

I was stuck on the line: customerList.add (client); // (part of the addCustomer method in the WCIA class) Also for the first time I was told "Assign return value to new variable" as part of the error, so it’s not very sure what that means.

Code: Home

    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;
    }

}
+3
source share
3 answers

You need to instantiate your ArrayList before you can assign elements to it. You probably get NullPointerException, I think.

Change this line:

private final ArrayList customerList = null;

to

private final ArrayList customerList = new ArrayList();

. , , .

+9

customerList null . ArrayList , .

+1

You must declare Listwith an explicit definition of the type of its elements ( parameterized list ):

private final List<Customer> customerList;

This way you can get rid of casting before Customerin:

Customer cus = customerList.get(x);

Finally, as a good practice, initialize it in the constructor:

public WCIA()
{
    customerList = new ArrayList<>();
}
0
source

All Articles