InARGS
InARGS

Reputation: 31

Assign return value to new Variable (Java)

it's been a while since I've done some java coding.

I need to build an application for a business which requires automation (part of a workshop), which is however irrelevant to my question...

I'm stuck on the line : customerList.add(customer); //(part of the addCustomer method in the WCIA class) Also it's the first time I'm told to "Assign return value to new Variable" as part of an error, so not too sure what that 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("[email protected]");
        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;
    }

}

Upvotes: 3

Views: 13479

Answers (3)

Christian Tapia
Christian Tapia

Reputation: 34166

You should declare the List with an explicit definition of the type of its elements (parametrized list):

private final List<Customer> customerList;

This way you can get rid of casting to Customer in:

Customer cus = customerList.get(x);

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

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

Upvotes: 1

Martin Dinov
Martin Dinov

Reputation: 8825

customerList is null and never initialized. Create an object of type ArrayList and assign it to that variable before you try to add to it.

Upvotes: 1

Kon
Kon

Reputation: 10810

You need to instantiate your ArrayList before you can assign elements to it. You're probably getting a NullPointerException, is my guess.

Change this line:

private final ArrayList customerList = null;

to

private final ArrayList customerList = new ArrayList();

Should solve at least this problem. I did not read the rest of your code so I'm not sure if other problems exist.

Upvotes: 9

Related Questions