创建弹出启动ConfigurationProperties Map时,我收到空指针异常。属性类是
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController : CreatePostScrollViewController = segue.destinationViewController as! CreatePostScrollViewController
if dateText != "" {
DestViewController.startTimeDateButton.setTitle(dateText, forState: .Normal)// Error Here
} else if dateText == "" {
DestViewController.startTimeDateButton.setTitle(currentDate, forState: .Normal)// Error Here
}
}
ThingLayout是一个简单的pojo,具有属性文件指定的正确属性:
@Component
@ConfigurationProperties(prefix = "thing")
public class ThingProperties {
private Map<String, ThingLayout> layouts;
public Map<String, ThingLayout> getLayouts() {
return layouts == null ? new HashMap<>() : layouts;
}
public void setLayouts(final Map<String, ThingLayout> layouts) {
this.layouts = new HashMap(layouts);
}
}
意图是绑定属性,我将能够执行
之类的操作things.layouts:
3042:
itemPageIndex: 1
itemX: 1960
itemY: 890
itemWidth: 400
itemHeight: 70
itemPattern: dd MMMM yyyy
2151:
itemPageIndex: 0
itemX: 2000
itemY: 796
itemWidth: 500
itemHeight: 70
itemPattern: dd/MM/yyyy
我得到的例外是
thingProperties.getLayouts().get(3042).getItemX();
答案 0 :(得分:3)
经过大量调试后,解决方案最终变得非常简单。如果我将我的属性类更改为以下它可以工作。
@Component
@ConfigurationProperties(prefix = "thing")
public class ThingProperties {
private Map<String, ThingLayout> layouts = new HashMap<>();
public Map<String, ThingLayout> getLayouts() {
return layouts;
}
public void setLayouts(final Map<String, ThingLayout> layouts) {
this.layouts = new HashMap(layouts);
}
}
请注意,我现在正在初始化getter之外的布局对象。
Spring试图在
中为每个地图键创建一个默认值org.springframework.beans.AbstractNestablePropertyAccessor#setDefaultValue(org.springframework.beans.AbstractNestablePropertyAccessor.PropertyTokenHolder)
默认值创建工作正常,但该值未在
中正确设置到地图中org.springframework.beans.AbstractNestablePropertyAccessor#setPropertyValue(org.springframework.beans.AbstractNestablePropertyAccessor.PropertyTokenHolder, org.springframework.beans.PropertyValue)