[JAVA-2096] NullPointerException when calling GridFSInputFile.save Created: 21/Jan/16  Updated: 13/Sep/16  Resolved: 13/Sep/16

Status: Closed
Project: Java Driver
Component/s: GridFS
Affects Version/s: 2.14.1
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Francisco Manuel Benitez Chico Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Production



 Description   

Hello,

If I get an instance of GridFSInputFile from the method createFile() of GridFS, MongoDB Java Driver assigns null to the attribute _in.

So, when I want to call the "save" method from GridFSInputFile, I always get a NullPointerException (line 295 - GridFSInputFile) because this value is null, and I cannot set it anywhere (the only place is the constructor and with by the method createFile() is always null)

Does it make sense to have an method createFile without parameters if I always get a NullPointer when I try to save?

Many thanks.

Regards,
Paco.



 Comments   
Comment by Jeffrey Yemin [ 13/Sep/16 ]

With the addition of GridFSBuckets and its associated classes in the 3.1 driver, GridFS and its associated classes are effectively deprecated, and the bar for fixing issues there is pretty high. Given that, I'm going to close this minor bug as Won't Fix.

Comment by Francisco Manuel Benitez Chico [ 21/Jan/16 ]

Many thanks Jeff!

No problem, I can use another overloaded method

It was just to inform your team about that. Have a nice day!

Regards,
Paco.

Comment by Jeffrey Yemin [ 21/Jan/16 ]

Hi Francisco,

It's nice to meet you too. I hope you enjoyed the courses, especially mine.

What you've found is a bug, but based on the API all that the driver could do better is to throw a more informative exception. In order to use GridFSInputFile.save, you must use one of the overloaded GridFS.createFile methods that take either an InputStream or a File. For the other createFile overloads, it's expected that instead of calling GridFSInputFile.save you call GridFSInputFile.getOutputStream and write the file contents to that output stream.

Regards,
Jeff

Comment by Francisco Manuel Benitez Chico [ 21/Jan/16 ]

Hello Jeff!

Nice to meet you here by Jira! I finished three MongoDB online courses and one of them you were the teacher

It is not common to do that, but it would be possible and throws a NullPointerException:

final MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017), new ServerAddress("localhost", 27018))) ;
 
final DB db = mongoClient.getDB("example") ;
final GridFS gridFs = new GridFS(db) ;
		
final GridFSInputFile inputFile = gridFs.createFile();
inputFile.setId("LONG_XML_11");
inputFile.save();

And here is the exception stack trace:

Exception in thread "main" java.lang.NullPointerException
	at com.mongodb.gridfs.GridFSInputFile._readStream2Buffer(GridFSInputFile.java:295)
	at com.mongodb.gridfs.GridFSInputFile.saveChunks(GridFSInputFile.java:215)
	at com.mongodb.gridfs.GridFSInputFile.save(GridFSInputFile.java:167)
	at com.mongodb.gridfs.GridFSInputFile.save(GridFSInputFile.java:150)
        ...

Thanks!

Cheers,
Paco.

Comment by Jeffrey Yemin [ 21/Jan/16 ]

Can you share a code snippet that reproduces the exception? As you have probably noticed, there are a number of overloads of the createFile method, and I'd also like to see what else you're doing with the GridFSInputFile before you call save.

Comment by Jeffrey Yemin [ 21/Jan/16 ]

I updated the description to reflect the correction. Thanks.

Comment by Francisco Manuel Benitez Chico [ 21/Jan/16 ]

I had a mistake in the first paragraph:

If I get an instance of GridFSInputFile from the method createFile() of *GridFS*, MongoDB Java Driver assigns null to the attribute _in.

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