Conversion using streams gives a NullPointerException

I’ve already created this question on stackoverflow.com:


java - Groupdocs conversion using streams - Stack Overflow

…but the gist of it is that I’m trying to create a couple of images (as OutputStream objects) from an InputStream which is coming from MongoDB, and then use those OutputStream objects to put them back into a different MongoDB collection. But when I try to execute this:

OutputStream outputStream = conversionHandler.convertToImage(inputStream, fileName, imageSaveOptions);

…for non-null conversionHandler and all of the method parameters, I get this not-helpful exception:

java.lang.NullPointerException at com.groupdocs.conversion.converter.a.(ConversionHelper.java:31) at com.groupdocs.conversion.handler.ConversionHandler.convertToImage(ConversionHandler.java:127)

I can certainly post more of the code – and there is a little more on the stackoverflow question – but I’m clearly missing something.

Any thoughts?

Hello,


We are sorry to hear that you have such issue. Yes, we definitely need your code example.
And full info about the project and use case.

Thank you.

Hello,


Thank you for the code example. We have checked your code and found out that you have set path for the saving converted file to null imageSaveOptions.setSavePath(null); - it’s wrong. You should set local path where the converted file will be saved.

Best regards.

Pavel…


The issue is that I’m not trying to save the output file at all, at least not locally. I’m trying to use the OutputStream generated by the ConversionHandler and save that to a MongoDB collection. No actual files would need to be created.

Hello,


We have checked such approach with our example project and all works well for us. Since that we definitely need your project example which we can run and reproduce the issue on our side.

Thank you

Before we get to that stage, I’ll tell you what I’ve done today with this. I was able to completely verify that the InputStream was good, by taking that, writing it to a File, and then using that File as the source for the conversion to create an OutputStream, and then store that in the database. It’s clunky, but it eliminates one file write procedure, which is good. I would ideally like to be able to eliminate both, though, so my question for you is: what settings to the ImageSaveOptions object did you use? Perhaps I could emulate those and achieve your results on my end.

Hello,


Glad to hear that you was able to found out that. As for the settings that we use - you can see them on this screenshot. Also you can find this code and investigate it on your side here.

Best regards.

Actually the difficulty I’m having is with converting an InputStream into an OutputStream, like this:


OutputStream wasStream = conversionHandler.convertToImage(inputStream, fileName, imageSaveOptions);

It’s this line that gives me the NPE, and is preventing me from doing an entirely file-less conversion. When you got it to work, what settings were you using for the ImageSaveOptions object?

Hello,


We have prepared an example project for you, find it attached. This example shows how to work with streams. In the example we get InputStream from local file but in case getting it from DB the logic will be same.

Best regards.

Pavel…


Thanks for your help here. I’m finding that com.groupdocs.conversion.maven.config.Config is not in the GroupDocs.Conversion.jar I’m using; where is this to be found?

Steve

Hello,


This is a simple config file for the the project which you should create in your project or you can take it from the example that I have shared and modify it as you need.

Best regards.

Sorry, that’s just me being stupid; I looked at the class with the main method only, not thinking that the class of interest would be elsewhere. I’ll try this out and get back to you.

Hi again,


No problems, yes sure, if you will have any questions feel free to contact us.

Best regards.
I was away from this for a while, but got back to it today.

Here is my solution:
(Thanks for your help, by the way.)

public class RenderImages {
private static final Log log = LogFactory.getLog(RenderImages.class);

public static void generateRenderedImages(Datastore datastore,
InputStream inputStream,
String fileName,
ConversionHandler conversionHandler,
SystemProperties systemProperties,
Logger logger) {

int thumbnailHeight = systemProperties.getAsInteger("RENDER_THUMBNAIL_HEIGHT");
int thumbnailWidth = systemProperties.getAsInteger("RENDER_THUMBNAIL_WIDTH");
String contentTypePreview = systemProperties.get("RENDER_CONTENT_TYPE_PREVIEW");
String contentTypeThumbnail = systemProperties.get("RENDER_CONTENT_TYPE_THUMBNAIL");

try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
IOUtils.copy(inputStream, outputStream);
byte[] imageBytes = outputStream.toByteArray();
inputStream.close();

ImageSaveOptions imageSaveOptions = new ImageSaveOptions();
imageSaveOptions.setSaveFileType(ImageSaveOptions.JPEG);
imageSaveOptions.setPage(1);

ByteArrayOutputStream previewStream = conversionHandler.convertToImage(new ByteArrayInputStream(imageBytes),
fileName + ".pdf",
imageSaveOptions);

saveToDatabase(datastore, previewStream, contentTypePreview, fileName);
previewStream.close();

imageSaveOptions.setHeight(thumbnailHeight);
imageSaveOptions.setWidth(thumbnailWidth);

ByteArrayOutputStream thumbnailStream = conversionHandler.convertToImage(
new ByteArrayInputStream(imageBytes),
fileName + ".pdf",
imageSaveOptions);

saveToDatabase(datastore, thumbnailStream, contentTypeThumbnail, fileName);
thumbnailStream.close();
} catch (Exception e) {
e.printStackTrace();
log.error("Image rendering failed); error = " + e.getMessage());
logger.writeLog("Image rendering failed); error = " + e.getMessage());

} finally {
// maybe need something here, not sure yet
}
}

private static void saveToDatabase(Datastore datastore,
ByteArrayOutputStream image, String contentType, String fileName) throws IOException {

RenderedImage renderedImage = datastore.find(RenderedImage.class)
.filter("attachmentId", fileName)
.filter("contentType", contentType)
.get();

if (renderedImage == null) {
renderedImage = new RenderedImage();
renderedImage.setAttachmentId(fileName);
renderedImage.setContentType(contentType);
renderedImage.setContent(image.toByteArray());
datastore.save(renderedImage);
}
}
}

Hello,

We glad to hear, that you have found the solution for your issue. If you will have more questions please feel free to contact us.

Best regards,
Evgen Efimov

http://groupdocs.com
Your Document Collaboration APIs
Follow us on LinkedIn, Twitter, Facebook and Google+