1

Im fairly new to java and I cant figure out why my ArrayList is only returning Nulls!

Im working on an Address Book application that is supposed to store Persons as Objects, and its supposed to store the full name, address, city, state, zip and phone number, but after ive run the program all that was stored were Null values...

Persons Class:

public class Person implements Comparable<Person>{
private String FULLNAME;
private String ADDRESS;
private String CITY;
private String STATE;
private String ZIP;
private String PHONE;

public Person(String FULLNAME, String ADDRESS,  String CITY, String STATE, String ZIP, String PHONE) {

}

//GETTERS
public String getFullName(){
    return FULLNAME;
}
public String getAddress(){
    return ADDRESS;
}
public String getCity(){
    return CITY;
}
public String getState(){
    return STATE;
}
public String getZip(){
    return ZIP;
}
public String getPhone(){
    return PHONE;
}

//SETTERS
public void setFullName(String fullname){
    this.FULLNAME = fullname;
}
public void setAddress(String address){
    this.ADDRESS=address;
}
public void setCity(String city){
    this.CITY=city;;
}
public void setState(String state){
    this.STATE=state;
}
public void setZip(String zip){
    this.ZIP=zip;
}
public void setPhone(String phone){
    this.PHONE=phone;
}

TestAddressBook Class:

public class TestAddressBook {

static Scanner sc= new Scanner(System.in);

static String fullname;
static String address;
static String city;
static String state;
static String zip;
static String phone;
static String in;
static char input;
public static void main(String[] args){
    //Declare Variables to store user input in before its sent to the 
    //Persons Class and stored in the Array List
    //Instance of a Person
    Person person = new Person(fullname, address, city, state, zip, phone);
    //Declaring ArrayList
    ArrayList<Person> AddressBook = new ArrayList<Person>();

    //Launch Menu
    do{
        Menu();
        //gathers user input
        in=sc.nextLine();
        input=in.toUpperCase().charAt(0);
        //decides what to do based on user selection
        switch (input){
        case 'A': 
            addPerson(AddressBook, person);
            break;
        case 'D':
            //deletePerson(AddressBook);
            break;
        case 'M':
            //modifyPerson(AddressBook);
            break;
        case 'S':
            //search(AddressBook);
            break;  
        }
    }while(input!='Q');
    System.out.println("Application Closed");
    System.exit(0);
}

public static void Menu(){
    //User Selection
    System.out.println("Address Book Menu");
    System.out.println("\tEnter A to (A)dd a Person");
    System.out.println("\tEnter D to (D)elete a Person");
    System.out.println("\tEnter M to (M)odify a Person");
    System.out.println("\tEnter S to (S)earch Address Book");
    System.out.println("\tEnter Q to (Q)uit");
    System.out.println("Please enter your choice");
}

public static void addPerson(ArrayList<Person> AddressBook, Person person){
    System.out.println("Please input the person's information (one line per field)");
    //User Input
    System.out.println("\nPlease enter person's full name: ");
    fullname=sc.nextLine();
    person.setFullName(fullname);
    System.out.println("\nPlease enter person's street address: ");
    address=sc.nextLine();
    person.setAddress(address);
    System.out.println("\nPlease enter person's city: ");
    city=sc.nextLine();
    person.setCity(city);
    System.out.println("\nPlease enter person's state: ");
    state=sc.nextLine();
    person.setState(state);
    System.out.println("\nPlease enter person's zip code: ");
    zip=sc.nextLine();
    person.setZip(zip);
    System.out.println("\nPlease enter person's phone number: ");
    phone=sc.nextLine();
    person.setPhone(phone);


    //Set arrayList
    AddressBook.add(new Person(fullname, address, city, state, zip, phone));

    //test to see if values stored
    System.out.println(AddressBook.get(0).getFullName());

}

At the end of adding a person, all that is returned is 'Null'

Any help would be appreciated, Thanks

2
  • You seem to be populating a Person that you pass into the addPerson() method then separately creating a new instance of a person to add to the array list. This new instance is invoked with a constructor that doesn't do anything, so all of its values are set to null. Commented Nov 1, 2013 at 19:31
  • Your Person object appears to meet the JavaBean pattern, In particular, you define getters and setters and you appear to use setters to initialize your object (this is inherently dangerous and/or fragile). If you are going to use setters to initialize your object, then you don't need to pass arguments in your constructor. For the JavaBean pattern, you should use a no-arg constructor. Commented Nov 1, 2013 at 20:31

4 Answers 4

2

The Problem is not the address book, but the person. Don't pass in a person, and don't use the setter methods.

Then, all you to do is set each of the values you are getting to String (or int) so you can pass them in when you create a person.

Finally, you need to set the fields in your constructor.

Sign up to request clarification or add additional context in comments.

Comments

1

Your constructor isn't ever doing anything. You need to assign values to your private instance variables.
For example
this.FULLNAME = FULLNAME;

Comments

1

That's because your constructor isn't doing anything.

You need something like this.

public Person(String fullName, String address,  String city, String state, String zip, String phone) {
    this.fullName = fullName;
    this.address = address;
    ... and so on ...
}

Note that I have changed the parameter names to be camel case. This is to adhere to Java naming conventions.

Comments

0

Your constructor does not initialize fields:

public Person(String FULLNAME, String ADDRESS,  String CITY, String STATE, String ZIP,
   String PHONE) {
}

should be changed to

public Person(String FULLNAME, String ADDRESS,  String CITY, String STATE, String ZIP,
   String PHONE) {

  this.FULLNAME=FULLNAME;
  this.ADDRESS=ADDRESS;
  ...
}

2 Comments

He initializes his object with calls to the setter methods. The constructor is superfluous.
But when he uses: AddressBook.add(new Person(fullname, ... which means new person object, not prepared one...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.