2012-05-15 28 views
1

我試圖保存包含大約130,000條記錄的文檔,並使用bulksavedocument方法保存文檔,但出現以下錯誤java.lang.NullPointerException當在java中使用couchdb4j api保存批量文檔時

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) 

請建議的解決方案來擺脫這個異常的沿調試程序。

+0

哪條線是280? –

+0

@ckuetbach *拍額頭* – keyser

+0

不是結果集的第280行,當然是源代碼的第280行。我認爲它是Document singleDoc = new Document ...,但我不知道它。 –

回答

1

我不知道真的很好此JSON lib中,但是這

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++) 
+0

否數據庫不爲空。那麼會有一個不同的StackTrace。第280行有一個數據庫訪問權限。有一個空指針。 –

+0

我的不好;那絕對是我的頭。我需要睡覺,哈哈。 – Vulcan

+0

我用for(int i = 0; i monil