ContentService throws a NullPointerException when finding for ContentType.ATTACHMENT

Hello,

I am trying to use the ContentService class to retrieve results, but following code throws a NullPointerException:

contentService.find()
                .withTitle("Non existing title")
                .withType(ContentType.ATTACHMENT)
                .fetch();

Searching for an existing title, like the “welcome.png” attachment from the example space, has the same behaviour. However, searching for ContentType.PAGE works as expected.

Tested with library version 7.19.4.

Hello AnsgarNell,

I think you have the same problem as mentioned here Cannot use ContentService.find().withSpace() and ContentType.ATTACHMENT

So for you maybe the com.atlassian.confluence.api.service.content.AttachmentService (AttachmentService.AttachmentFinder (Atlassian Confluence 7.19.0 API)) is the answer.

Regards,
Christian

Thank you Christian, you are absolutely right. I missed this part in the AttachmentService documentation:

Restrict the attachments returned to those with the given parent. Unless the attachment is being looked up by ID via the withId method, this method is compulsory.

I suppose nobody at Atlassian thought about doing a check before calling fetch() which would give a message like stating that exact condition instead a completely useless NullPointerException.

1 Like