我正在尝试使用java JABX解析XML文件并创建一组对象。我有一个类,我想将xml映射到:
$('ul').mousewheel(function(event, delta) {
this.scrollLeft -= (delta * 20);
event.preventDefault();
});
如果在getter方法中我有public class Dash implements Serializable {
private List<ViewBlock> viewBlocks = new ArrayList<ViewBlock>();
public List<ViewBlock> getViewBlocks() {
return viewBlocks; // ---> this works
//return new ArrayList<ViewBlock>(viewBlocks); ---> this doesn't
}
public void setViewBlocks(List<ViewBlock> viewBlocks) {
this.viewBlocks.clear();
this.viewBlocks.addAll(viewBlocks);
}
}
它可以正常工作但是当我使用第二个选项return viewBlocks
时,我的数组列表为空。我需要它来返回数组的副本并解析xml,这可能吗?
答案 0 :(得分:0)
尝试通过添加空检查进行更改,并确保进行初始化。
public class Dash implements Serializable {
private List<ViewBlock> viewBlocks = new ArrayList<ViewBlock>();
public List<ViewBlock> getViewBlocks() {
if (viewBlocks== null) {
numbers = new ArrayList<ViewBlock>();
}
return viewBlocks;
}
public void setViewBlocks(List<ViewBlock> viewBlocks) {
this.viewBlocks.clear();
this.viewBlocks.addAll(viewBlocks);
}
}