簡體   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