簡體   English   中英

java.lang.NullPointerException同時在Java中使用Couchdb4j API將批量文檔保存到Couchdb

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM