[JAVA-2985] Mongo DB throwing NullPointerException on find() Created: 20/Sep/18  Updated: 11/Sep/19  Resolved: 20/Sep/18

Status: Closed
Project: Java Driver
Component/s: Async, Query Operations
Affects Version/s: 3.8.1
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Ashish Tomer Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu Linux operating system.



 Description   

I am retrieving the documents from the MongoDB with the collection.find().into() to generate the documents. The following is the code -

 

List<Document> scrapingInformation = new LinkedList<>();
database.getCollection(scrapingInformationByClientidJobid)
 .find(and(eq("clientId", clientId), eq("jobId", jobId)))
 .into(scrapingInformation, (res, ex) ->
{ 
    ex.printStackTrace(); System.out.println("Exception while iterating over the finditerable and adding to document list: " + ex.getMessage()); }
);
System.out.println("Total populated documents in list: " + scrapingInformation.size());{{ }}

 

Data is there in the database. The find() method reads that data as well. The problem comes when I try to iterate on the data. I tried iterating over the FindIterable as well. There was also the NullPointerException.

The version I installed is 4.0 and driver 3.8.1. To remove the possibility of version conflict I downgraded to 3.6.1 and still, there is the problem.

Following is the stacktrace:

java.lang.NullPointerException
	at com.fastscraping.dao.mongo.ScrapingInformationDB.lambda$getScrapingInformation$2(ScrapingInformationDB.java:71)
	at com.mongodb.async.client.MongoIterableImpl$2$2.onResult(MongoIterableImpl.java:110)
	at com.mongodb.async.client.MongoIterableImpl$2$2.onResult(MongoIterableImpl.java:104)
	at com.mongodb.async.client.MongoIterableImpl$4.onResult(MongoIterableImpl.java:171)
	at com.mongodb.async.client.MongoIterableImpl$4.onResult(MongoIterableImpl.java:166)
	at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:141)
	at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:100)
	at com.mongodb.async.client.MongoIterableImpl.loopCursor(MongoIterableImpl.java:166)
	at com.mongodb.async.client.MongoIterableImpl.access$000(MongoIterableImpl.java:34)
	at com.mongodb.async.client.MongoIterableImpl$4.onResult(MongoIterableImpl.java:177)
	at com.mongodb.async.client.MongoIterableImpl$4.onResult(MongoIterableImpl.java:166)
	at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:136)
	at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:100)
	at com.mongodb.async.client.MongoIterableImpl.loopCursor(MongoIterableImpl.java:166)
	at com.mongodb.async.client.MongoIterableImpl.access$000(MongoIterableImpl.java:34)
	at com.mongodb.async.client.MongoIterableImpl$2.onResult(MongoIterableImpl.java:99)
	at com.mongodb.async.client.MongoIterableImpl$2.onResult(MongoIterableImpl.java:93)
	at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
	at com.mongodb.async.client.OperationExecutorImpl$1$1.onResult(OperationExecutorImpl.java:82)
	at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
	at com.mongodb.operation.FindOperation$3.onResult(FindOperation.java:806)
	at com.mongodb.operation.OperationHelper$ReferenceCountedReleasingWrappedCallback.onResult(OperationHelper.java:364)
	at com.mongodb.operation.CommandOperationHelper$2.onResult(CommandOperationHelper.java:405)
	at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
	at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor$2.onResult(DefaultServer.java:227)
	at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
	at com.mongodb.internal.connection.CommandProtocolImpl$1.onResult(CommandProtocolImpl.java:85)
	at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection$1.onResult(DefaultConnectionPool.java:461)
	at com.mongodb.internal.connection.UsageTrackingInternalConnection$2.onResult(UsageTrackingInternalConnection.java:111)
	at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
	at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:379)
	at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:356)
	at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:651)
	at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:618)
	at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:494)
	at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:491)
	at com.mongodb.connection.netty.NettyStream.readAsync(NettyStream.java:236)
	at com.mongodb.internal.connection.InternalStreamConnection.readAsync(InternalStreamConnection.java:491)
	at com.mongodb.internal.connection.InternalStreamConnection.access$1000(InternalStreamConnection.java:74)
	at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback.onResult(InternalStreamConnection.java:608)
	at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback.onResult(InternalStreamConnection.java:593)
	at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:494)
	at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:491)
	at com.mongodb.connection.netty.NettyStream.readAsync(NettyStream.java:236)
	at com.mongodb.connection.netty.NettyStream.handleReadResponse(NettyStream.java:266)
	at com.mongodb.connection.netty.NettyStream.access$600(NettyStream.java:66)
	at com.mongodb.connection.netty.NettyStream$InboundBufferHandler.channelRead0(NettyStream.java:325)
	at com.mongodb.connection.netty.NettyStream$InboundBufferHandler.channelRead0(NettyStream.java:322)
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:341)
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926)
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:129)
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:642)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:565)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:479)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:441)
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
	at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
	at java.lang.Thread.run(Thread.java:748)

What could be the cause of this exception?



 Comments   
Comment by Ashish Tomer [ 20/Sep/18 ]

Even if I don't touch the ex at all I am not able to get the result in
the list. The issue is that I am using the async driver which is not
suitable to read the data from DB and use it for purposes, not including
the data streaming. So, switched to the sync driver and now it is working
fine.

Thanks and regards,

Ashish Tomer

Be better every day!

Comment by Jeffrey Yemin [ 20/Sep/18 ]

Hi ashish1269

It's hard to tell what's happening in each of the cases you described. But if we just take the provided code and stack trace, the most likely issue is that ex is null, and therefore a NullPointerException is thrown when you try to call a method on it (assume the dereferencing of ex in the callback is line 71). Try something like this instead:

List<Document> scrapingInformation = new LinkedList<>();
database.getCollection(scrapingInformationByClientidJobid)
 .find(and(eq("clientId", clientId), eq("jobId", jobId)))
 .into(scrapingInformation, (res, ex) ->
{ 
    if (ex != null) {
        ex.printStackTrace(); 
        System.out.println("Exception while iterating over the finditerable and adding to document list: " + ex.getMessage()); 
    } else {
        System.out.println("Total populated documents in list: " + scrapingInformation.size());{
    }
);

Also, note the the provided code will not work as you expect. Since you're using the async driver, the "Total populated documents in list" println will happen on the main thread, likely before the data is populated, and therefore will also print a size of zero. Unless you have good reason for it, I recommend that you stick with the synchronous driver.

I'm going to close this issue for now, as I don't think this is a bug in the driver.

Generated at Thu Feb 08 08:58:31 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.