繁体   English   中英

为什么在这种情况下没有将数据设置到列表对象中?

[英]why data is not being set into the List Object in this case?

我正在将两个系列的数据(以冒号分隔的:)传递给一个方法,并希望在我的自定义类CountryDTO中进行设置

这是我的CountryDTO课程

public class CountryDTO {

    public CountryDTO(String a , String b , String c)
    {

    }
public String value1;
public String value2;
public String value3;

// setters and getters 

}



This is my Main class 

public class Test {

    public static void main(String args[]) throws Exception {

        Test test = new Test();
        List list = (List) test.extract("IND,US,UK : WI,PAK,AUS");

        Iterator itr = list.iterator();

        while (itr.hasNext()) {
            CountryDTO ind = (CountryDTO) itr.next();
            System.out.println(ind.getValue1());
        }
    }

    public List<CountryDTO> extract(final String v) throws Exception {
        String[] values = v.split(":");
        List<CountryDTO> l = new ArrayList<CountryDTO>();
        for (String s : values) {
            String[] vs = s.split(",");
            l.add(new CountryDTO(vs[0], vs[1], vs[2]));
        }
        return l;
    }
}

发生的事情是,我得到的输出为null(未设置CountryDTO)

谁能帮我

因为在CountryDto构造函数中,您永远不会设置value1 ,也不会设置其他值,所以它们将保持为null

抱歉,您的DTO不正确。 我认为它应该像这样:

public class CountryDTO {

    private final String value1;
    private final String value2;
    private final String value3;

    public CountryDTO(String a , String b , String c) {
        this.value1 = ((a != null) ? a : "");
        this.value2 = ((b != null) ? b : "");
        this.value3 = ((c != null) ? c : "");
    }

    // just getters; no setters 

}

我无法说出您的代码还有什么其他地方做错了,但这肯定是不合时宜的。

暂无
暂无

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

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