简体   繁体   English

使用 CSV 文件将对象添加到 arrayList,但对象的值返回 null

[英]Adding objects to arrayList using a CSV file, but the values of the objects are returning null

I'm trying to use a CSV file to create a list of employee objects, but I'm currently getting null for every value.我正在尝试使用 CSV 文件来创建员工对象列表,但我目前每个值都为 null。 The values being: username, firstname, lastname, email, gender, race, id, and ssn.值是:用户名、名字、姓氏、电子邮件、性别、种族、id 和 ssn。 I can read in the CSV file and parse it fine, but when I try to populate the list with the objects, It fills them but every value is still null.我可以读入 CSV 文件并解析它,但是当我尝试用对象填充列表时,它会填充它们但每个值仍然为空。 The main method:主要方法:

public static void main(String[] args) {
    String csvFile = "employee_data.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    List<Entry> People = new ArrayList<>();
    try {
        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {
            // use comma as separator
            String[] Labels = line.split(cvsSplitBy);                 
            Entry entry = new Entry(Labels[0], Labels[1], Labels[2], Labels[3], Labels[4], Labels[5], Labels[6], Labels[7]);
            People.add(entry);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    System.out.print(People);
}

The Entry class:入门类:

public class Entry {
    private String Username, Firstname, Lastname, Email, Gender, Race, ID, SSN;
    public Entry(String Username, String Firstname, String Lastname, String Email, String Gender, String Race, String ID, String SSN) {
        this.Username=null;
        this.Firstname=null;
        this.Lastname=null;
        this.Email=null;
        this.Gender=null;
        this.Race=null;
        this.ID=null;
        this.SSN=null;
    }
    @Override
    public String toString() {
        return ("Username:"+this.Username);
    }
}

I'm not sure why the Entry objects are being properly added to the List, but the values from the Labels array is not being transferred, so username, firstname, etc are all being labeled null and I cant figure out why我不确定为什么 Entry 对象被正确添加到 List 中,但是 Labels 数组中的值没有被传输,所以用户名、名字等都被标记为 null,我不知道为什么

I think the constructor on your Entry class needs to assign the parameters to the fields in your class.我认为您的 Entry 类上的构造函数需要将参数分配给您类中的字段。 How about the following:以下情况如何:

public class Entry {

    private String Username, Firstname, Lastname, Email, Gender, Race, ID, SSN;

    public Entry(String Username, String Firstname, String Lastname, String Email, String Gender, String Race, String ID, String SSN) {
        this.Username = Username;
        this.Firstname = Firstname;
        this.Lastname = Lastname;
        this.Email = Email;
        this.Gender = Gender;
        this.Race = Race;
        this.ID = ID;
        this.SSN = SSN;
    }

    @Override
    public String toString() {
        return ("Username:" + this.Username);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM