Can a row returned by a view be null? Can its document?

I have a NullPointerException that happens sometimes on the first line of this loop,

    ViewResponse rows = this.client.query(view, query);
    for (ViewRow row : rows)
    {
      PsekKboxReport report = (PsekKboxReport)JsonUtil.convertObject(row.getDocument().toString(), PsekKboxReport.class);
 
      reports.add(report);
    }

There are a bit too many method calls on this line, but I can rule out that JsonUtil is null because convertObject() is static.

I checked that convertObject(null,..) does not throw a NullPointer. That leaves only the possibility that row is null or row.getDocument() returns null.

How is this possible? Can it be avoided? How should I deal with it?

1 Answer

« Back to question.

Hello,

Be sure you include the document in your query, what I mean by that is to call the
- query.setIncludeDocs(true); see JavaDoc

When you query a view it returns by default only the key and values. Like that we do not consume too much network when it is not needed.
If you want to get the document you call this method, in the query, the Java SDK will do a client.get(row.id) for you automatically an return the document.

HTH

Regards
Tug
@tgrall

I did set includeDocs to true. Here is the complete query.

  public List<PsekKboxReport> findReports(long fromMillis, long toMillis)
  {
    View view = this.client.getView("psek_kbox", "by_looptimestamp");
    Query query = new Query();
    query.setIncludeDocs(true);
    query.setStale(Stale.FALSE);
    query.setRangeStart(String.valueOf(fromMillis));
    query.setRangeEnd(String.valueOf(toMillis));
    query.setDescending(false);
    query.setInclusiveEnd(false);
    List<PsekKboxReport> reports = new ArrayList();
 
    ViewResponse rows = this.client.query(view, query);
    for (ViewRow row : rows)
    {
      PsekKboxReport report = (PsekKboxReport)JsonUtil.convertObject(row.getDocument().toString(), PsekKboxReport.class);
 
      reports.add(report);
    }
    return reports;
  }

Most often this method works, but from time to time a document is missing. I'm still looking at this, but it seems that something might be wrong with what's written in couchbase.