【问题标题】:java.lang.NullPointerException while saving bulk documents to couchdb using couchdb4j api in javajava.lang.NullPointerException,同时在java中使用couchdb4j api将批量文档保存到couchdb
【发布时间】:2012-05-15 07:57:50
【问题描述】:

我尝试保存包含大约 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)

请提出解决此异常的解决方案。

【问题讨论】:

  • @ckuetbach * 拍打额头 *
  • 不是结果集的第 280 行,当然是源代码的第 280 行。我认为是 Document singleDoc = new Document...,但我不知道。

标签: java database nosql couchdb


【解决方案1】:

我不太了解这个 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++)

【讨论】:

  • 无数据库不为空。那时会有不同的 StackTrace。第280行有对Database的访问,有一个空指针。
  • 我的错;这完全超出了我的想象。我要睡觉了,哈哈。
  • 我用 for(int i=0;i
  • com.fourspaces.couchdb.Database.bulkSaveDocuments(Database.java:280) github.com/mbreese/couchdb4j/blob/master/src/java/com/…
  • 如果没有 OP 提供的有关 CoucheDB4j 版本的更多信息,我们无法参考此源代码行以了解 shure。不过是一个好的方向……
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多