简体   繁体   中英

java.lang.NullPointerException while saving bulk documents to couchdb using couchdb4j api in java

I tried to save document consisting around 1,30,000 records, and I used the bulksavedocument method to save the document, but I am getting the following error

 java.lang.NullPointerException
at com.fourspaces.couchdb.Database.bulkSaveDocuments(Database.java:280)

Here is the code I used to save bulk documents.

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);

when I tried to debug the program along with the source code getting the following error

   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)

Please suggest the solution to get rid of this exception.

I don't know really well this JSON lib, but this

JSONArray json=new JSONArray(); 

is probably an array with size 0 (empty).

So your loop enters with index 0, that does'nt exists. So

json.getJSONObject(i)

propably returns null.


Where you write this

for(int i=0;i<json.size();i++)

You probably mean that

for(int i=0;i<json.size()-1;i++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM