[英]java.lang.NullPointerException while saving bulk documents to couchdb using couchdb4j api in java
我试图保存包含约1,30,000条记录的文档,并且使用bulksavedocument方法保存了文档,但是出现以下错误
java.lang.NullPointerException
at com.fourspaces.couchdb.Database.bulkSaveDocuments(Database.java:280)
这是我用来保存批量文档的代码。
JSONArray json=new JSONArray();
Document[] newdoc = null;
newdoc = new Document[json.size()];
for(int i=0;i<json.size();i++)
{
Document singleDoc = new Document(json.getJSONObject(i));
newdoc[i]=singleDoc;
}
Session s = new Session("localhost",5984);
Database db = s.getDatabase("test");
db.bulkSaveDocuments(newdoc);
当我尝试调试程序以及源代码时出现以下错误
net.sf.json.JSONException: A JSONArray text must start with '[' at character 1 of {"db_name":"item_masters_test","doc_count":0,"doc_del_count":0,"update_seq":0,"purge_seq":0,"compact_running":false,"disk_size":79,"instance_start_time":"1337249297703950","disk_format_version":5,"committed_update_seq":0}
at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:499)
at net.sf.json.JSONArray._fromJSONTokener(JSONArray.java:1116)
at net.sf.json.JSONArray._fromString(JSONArray.java:1197)
at net.sf.json.JSONArray.fromObject(JSONArray.java:127)
at net.sf.json.JSONArray.fromObject(JSONArray.java:105)
at com.fourspaces.couchdb.CouchResponse.getBodyAsJSONArray(CouchResponse.java:129)
at com.fourspaces.couchdb.Database.bulkSaveDocuments(Database.java:282)
at ItemMasterTest4.main(ItemMasterTest4.java:565)
请提出解决方案以摆脱此异常。
我不太了解这个JSON库,但这
JSONArray json=new JSONArray();
可能是大小为0(空)的数组。
因此,您的循环以索引0进入,该索引不存在。 所以
json.getJSONObject(i)
可能返回null。
你在哪里写这个
for(int i=0;i<json.size();i++)
你可能是说
for(int i=0;i<json.size()-1;i++)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.