nitrite-java icon indicating copy to clipboard operation
nitrite-java copied to clipboard

Null pointer exception when querying data

Open lahiruchandima opened this issue 7 years ago • 6 comments

I got following exception when querying a collection.

java.lang.NullPointerException: 
  at org.dizitart.no2.util.DocumentUtils.getFieldValue (DocumentUtils.java:80)
  at org.dizitart.no2.internals.SearchService.sortIdSet (SearchService.java:152)
  at org.dizitart.no2.internals.SearchService.setFilteredResultSet (SearchService.java:137)
  at org.dizitart.no2.internals.SearchService.find (SearchService.java:99)
  at org.dizitart.no2.internals.NitriteService.find (NitriteService.java:336)
  at org.dizitart.no2.internals.DefaultNitriteCollection.find (DefaultNitriteCollection.java:200)
  at org.dizitart.no2.objects.DefaultObjectRepository.find (DefaultObjectRepository.java:203)
  at com.my.app.DatabaseHelper.getReceipts (DatabaseHelper.java:457)

Following the code I use for querying.

    public List<Receipt> getReceipts() {
        return receiptRepository.find(not(eq("status", Receipt.Status.COMPLETED)), FindOptions.sort("createdTimestamp", SortOrder.Descending)).toList();
    }

What could be the reason for this?

lahiruchandima avatar Sep 03 '19 14:09 lahiruchandima

Maybe it would be a good idea to rewrite the code so that each method call is on a different line. Then, run it again and the stack trace should tell you exactly which method call is causing the exception.

kennethvt437 avatar Sep 17 '19 17:09 kennethvt437

So based on your input, I have create a test below

@Test
public void testIssue185() {
    ObjectRepository<Receipt> repository = db.getRepository(Receipt.class);
    Receipt r1 = new Receipt(org.dizitart.no2.NitriteTest.Receipt.Status.COMPLETED, 123456789L);
    Receipt r2 = new Receipt(org.dizitart.no2.NitriteTest.Receipt.Status.COMPLETED, 198746321L);
    Receipt r3 = new Receipt(org.dizitart.no2.NitriteTest.Receipt.Status.InProgress, 999999999L);
    Receipt r4 = new Receipt(org.dizitart.no2.NitriteTest.Receipt.Status.InProgress, 111111111L);
    repository.insert(r1, r2, r3, r4);
    List<Receipt> receipts = repository.find(not(eq("status", Receipt.Status.COMPLETED)), FindOptions.sort("createdTimestamp", SortOrder.Descending)).toList();
    assertEquals(receipts.size(), 2);
    assertEquals(receipts.get(0).getCreatedTimestamp().longValue(), 999999999L);
}


@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Receipt {
    public enum Status {
        COMPLETED,
        InProgress,
    }

    private Status status;
    private Long createdTimestamp;
}

but unfortunately, I could not reproduce this issue. So it would be better if you can give me a complete reproducible code.

anidotnet avatar Sep 17 '19 19:09 anidotnet

This is the only report of this issue from google play crash reports. I don't know any certain way to reproduce this.

lahiruchandima avatar Sep 18 '19 00:09 lahiruchandima

@lahiruchandima as I said, this info is not sufficient for me to reproduce the issue. Once thing you can do is to take the test code I provided here and modify it as per your application code and try to reproduce the issue.

anidotnet avatar Sep 18 '19 04:09 anidotnet

@anidotnet OK I could recreate this. Looks like it is a concurrency issue.

Following method recreates the issue.

public void testMethod() {
    Receipt receipt = new Receipt();
    receipt.clientRef = "111-11111";
    receipt.status = Receipt.Status.PREPARING;

    new Thread(() -> {
        for (int i = 0; i < 1000; ++i) {
            receiptRepository.update(receipt, true);
            try {
                Thread.sleep(50);
            } catch (InterruptedException ignored) {
            }
            receiptRepository.remove(receipt);
            try {
                Thread.sleep(50);
            } catch (InterruptedException ignored) {
            }
        }
    }).start();

    for (int i = 0; i < 1000; ++i) {
        receiptRepository.find(not(eq("status", Receipt.Status.COMPLETED)), FindOptions.sort("createdTimestamp", SortOrder.Descending)).toList();
        try {
            Thread.sleep(50);
        } catch (InterruptedException ignored) {
        }
    }
}

lahiruchandima avatar Sep 21 '19 16:09 lahiruchandima

Fix should be available in 3.3.0-SNAPSHOT

anidotnet avatar Sep 29 '19 19:09 anidotnet