1

我对 java 很陌生,我不知道为什么我的 ArrayList 只返回 Null!

我正在开发一个地址簿应用程序,该应用程序应该将人员存储为对象,并且它应该存储全名、地址、城市、州、邮编和电话号码,但是在我运行程序之后,所有存储的都是 Null 值。 ..

人员类别:

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 类:

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());

}

添加人员结束时,返回的所有内容都是“Null”

任何帮助将不胜感激,谢谢

4

4 回答 4

2

问题不在于地址簿,而在于人。不要传入一个人,也不要使用 setter 方法。

然后,您要做的就是将要获取的每个值设置为 String(或 int),以便在创建人员时将它们传入。

最后,您需要在构造函数中设置字段。

于 2013-11-01T19:30:54.290 回答
1

你的构造函数没有做任何事情。您需要为私有实例变量赋值。
例如
this.FULLNAME = FULLNAME;

于 2013-11-01T19:30:55.407 回答
1

那是因为你的构造函数没有做任何事情。

你需要这样的东西。

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

请注意,我已将参数名称更改为驼峰式大小写。这是为了遵守 Java 命名约定。

于 2013-11-01T19:31:01.853 回答
0

您的构造函数不初始化字段:

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

应该改为

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

  this.FULLNAME=FULLNAME;
  this.ADDRESS=ADDRESS;
  ...
}
于 2013-11-01T19:31:29.203 回答