-1

I'm very new to Java and have been trying to set-up an ArrayList CustomerList that takes object Customer, where Customer has attributes from class IAddress. When calling the .add method in my main code however, I am given a NullPointerException error, which I assume is being given because my method isn't receiving anything to add to the ArrayList. I thought it was an issue with the attributes being initialised to empty strings, but when editing them to contain some information, the error still occured.

The ArrayList CustomerList

public class CustomerList {

public ArrayList<Customer> Clients;

public CustomerList() {
    Clients = new ArrayList<>();
}

public void add(Customer src) {        
    Clients.add(src);
}

public void remove(Customer src) {
    Clients.remove(src);
}

public void Display(JTextArea jClientsTextArea) {
    for (int i = 0; i < Clients.size(); i++) {
        Clients.get(i).Display(jClientsTextArea);
    }
}
}

Receives Customer from this class

public class Customer {

private String FirstName;
private String Surname;
private IAddress HomeAddress;
public String DOB;

public Customer() {
    FirstName = "";
    Surname = "";
    DOB = "01/01/1900";
    HomeAddress = new IAddress();

    public void Display(javax.swing.JTextArea jAddressTextArea) {
    jAddressTextArea.setLineWrap(true);
    jAddressTextArea.append("First Name: " + FirstName + "\n");
    jAddressTextArea.append("Surname: " + Surname + "\n");
    jAddressTextArea.append("DOB:" + DOB + "\n");
    jAddressTextArea.append("Street: " + HomeAddress.getStreet() + "\n");
    jAddressTextArea.append("House Name: " + HomeAddress.getHouseName() + "\n");
    jAddressTextArea.append("House Number: " + HomeAddress.getHouseNo() + "\n");
    jAddressTextArea.append("Area: " + HomeAddress.getArea() + "\n");
    jAddressTextArea.append("Postcode: " + HomeAddress.getPostCode() + "\n");
    jAddressTextArea.append("Town: " + HomeAddress.getTown() + "\n");
    jAddressTextArea.append("Country: " + HomeAddress.getCountry() + "\n");
}


    public void Edit(String strfirstname, String strsurname, String strDOB, String strStreet, String strHouseName, String strHouseNo, String strHouseArea, String strPostCode, String strTown, String strCountry) {
    FirstName = strfirstname;
    Surname = strsurname;
    DOB = strDOB;
    HomeAddress.setStreet(strStreet);
    HomeAddress.setHouseName(strHouseName);
    HomeAddress.setHouseNo(strHouseNo);
    HomeAddress.setArea(strHouseArea);
    HomeAddress.setPostCode(strPostCode);
    HomeAddress.setTown(strTown);
    HomeAddress.setCountry(strCountry);
}
}

Which receives attributes from IAddress

public class IAddress {

private String Name;
private String Street;
private String HouseNo;
private String HouseName;
private String Area;
private String PostCode;
private String Town;
private String Country;

public IAddress() {
    Name = "";
    Street = "";
    HouseNo = "";
    HouseName = "";
    Area = "";
    PostCode = "";
    Town = "";
    Country = "";
}



public void setName(String strName) {
    Name = strName;
}

public void setStreet(String strStreet) {
    Street = strStreet;
}

public void setHouseNo(String strHouseNo) {
    HouseNo = strHouseNo;
}

public void setHouseName(String strHouseName) {
    HouseName = strHouseName;
}

public void setArea(String strArea) {
    Area = strArea;
}

public void setPostCode(String strPostCode) {
    PostCode = strPostCode;
}

public void setTown(String strTown) {
    Town = strTown;
}

public void setCountry(String strCountry) {
    Country = strCountry;
}

}

I've been banging my head against this problem for hours and am ready for it to be something stupidly simple. Thank you.

  • 5
    Don't assume. Read and post the exception stack trace, and draw conclusions. – JB Nizet Oct 31 '15 at 19:39
  • 1
    And show us the code calling all this please – Mateo Barahona Oct 31 '15 at 19:39
  • 2
    Duplicate (can't vote for now) -> [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Pshemo Oct 31 '15 at 19:41
  • Could you post the exception (the full stacktrace)? – aviad Oct 31 '15 at 19:41
  • It's likely the `Customer` you're passing to `add()` has not been initialized. Could you share your main method as well? – Bethany Louise Oct 31 '15 at 19:44
  • The Display method is defined inside the Customer constructor. That's illegal in java. – Ortomala Lokni Oct 31 '15 at 19:48
  • Welcome to SO. I don't think your question is prohibited, but the reason for the down votes is that it's not entirely on-topic. Check out this link: http://stackoverflow.com/help/on-topic – ryvantage Oct 31 '15 at 20:15
  • Also, I recommend finding a good resource that will help you with Java code conventions. Like English grammar rules, they're not not important. (For example, method names and variable names should generally start with a lower case letter) – ryvantage Oct 31 '15 at 20:16

1 Answers1

1

In your code above the only reason why calling myCustomerList.add(...) could throw is that myCustomerList itself is null. This is because the Clients inside it is initialized in the constructor, and never set to null again. The value of src does not matter as well - the call to Clients.add(src) would succeed even if src is null.

You need to make sure that in your main you do initialize your customer list, like this:

CustomerList list = new CustomerList();
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Was exactly this problem, thank you so much. Sorry for not posting the full error log and main code, this is a small part of a much bigger project I've been working on and I didn't have the time to cut out the relevant section of code. –  Oct 31 '15 at 20:00