我们正在使用XContentBuilder,因为它抛出了NullPointerException
private int count=0;
public void outerCall(){
XContentBuilder contentBuilder = jsonBuilder();
contentBuilder.startObject();
contentBuilder.field("key1", "value1");
contentBuilder.field("key2", "value2");
innerCall(contentBuilder, 0);
contentBuilder.field("key3", "value3");
contentBuilder.close();
log.info("OUTER "+contentBuilder.string());
}
private XContentBuilder innerCall(XContentBuilder contentBuilder, int count2) throws Exception{
//XContentBuilder contentBuilder = jsonBuilder().startObject();
contentBuilder.startArray("specifications");
contentBuilder.startObject();
contentBuilder.field("key", "value");
if(count<2){
innerCall(contentBuilder, count++);
}
else{
contentBuilder.endObject().endArray().endObject();
return contentBuilder;
}
log.info(contentBuilder.string());
return contentBuilder;
}
这是示例代码和定位NPE at contentBuilder.field(“key3”,“value3”);
如果尝试调试代码获取NPE contentBuilder.field(“key”,“value”);
帮我解决问题..
答案 0 :(得分:0)
将方法代码更改为:
private XContentBuilder innerCall(XContentBuilder contentBuilder, int count2) throws Exception{
//XContentBuilder contentBuilder = jsonBuilder().startObject();
contentBuilder.startArray("specifications");
contentBuilder.startObject();
contentBuilder.field("key", "value");
if(count<2){
innerCall(contentBuilder, count++);
} else {
log.info(contentBuilder.string());
}
contentBuilder.endObject().endArray().endObject();
return contentBuilder;
}