Bug 65530 - Adding an opaque URI hyperlink (e.g., 'mailto', 'tel') to an image triggers a NullPointerException.
Summary: Adding an opaque URI hyperlink (e.g., 'mailto', 'tel') to an image triggers a...
Alias: None
Product: POI
Classification: Unclassified
Component: XSLF (show other bugs)
Version: unspecified
Hardware: Macintosh All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2021-08-31 10:27 UTC by Henry Iguaro
Modified: 2021-10-31 09:22 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description Henry Iguaro 2021-08-31 10:27:52 UTC
Adding an opaque URI hyperlink (e.g., 'mailto', 'tel') to an image triggers a NullPointerException.

var uri = toUri("mailto:foo@bar");
System.out.println("Is URI opaque? " + uri.isOpaque());
var nvPicPr = ((CTPicture) picture.getXmlObject()).getNvPicPr();
var cNvPr = nvPicPr.getCNvPr() != null ? nvPicPr.getCNvPr() : nvPicPr.addNewCNvPr();
var hyperlink = cNvPr.addNewHlinkClick();
// Here is where the NullPointerException is thrown
var rel = slide.getPackagePart()
    .addRelationship(uri, TargetMode.EXTERNAL, XSLFRelation.HYPERLINK.getRelation());

We tracked it down to file PackagingURIHelper.java, method `isRelationshipPartURI`:

public static boolean isRelationshipPartURI(URI partUri) {
    if (partUri == null)
        throw new IllegalArgumentException("partUri");

// !! Here, calling partUri.getPath() when the URI is opaque returns null !!
    return partUri.getPath().matches(
            ".*" + RELATIONSHIP_PART_SEGMENT_NAME + ".*"
                    + RELATIONSHIP_PART_EXTENSION_NAME + "$");

Part of the stack trace:

	at org.apache.poi.openxml4j.opc.PackagingURIHelper.isRelationshipPartURI(PackagingURIHelper.java:169)
	at org.apache.poi.openxml4j.opc.PackagePart.addRelationship(PackagePart.java:337)
	at org.apache.poi.openxml4j.opc.PackagePart.addRelationship(PackagePart.java:293)
Comment 1 Nick Burch 2021-08-31 10:30:11 UTC
Would you be able to generate a file like this using Microsoft Office, unzip it, and post the XML parts for the image and the relationships?
Comment 2 Henry Iguaro 2021-08-31 12:56:50 UTC
Hi Nick, thank you for looking into this!

I'm afraid we won't be able to provide such a file as we are trying to generate the presentation dynamically from code, and because of the exception, the file is never created.

Is there anything else we could try in order to help?
Comment 3 PJ Fanning 2021-08-31 13:09:17 UTC
var rel = slide.getPackagePart()
    .addRelationship(uri, TargetMode.EXTERNAL, XSLFRelation.HYPERLINK.getRelation());

the uri in this call is not for adding mailto addresses - this is for accessing files with the pptx zip file.

Using the CTPicture class like that is not a directly supported part of POI. Those classes are generated from the Microsoft XML schemas. Many people can work out how to do custom code using them but it is basically the wild west. People generally start by producing a pptx in Powerpoint and then unzipping it to look at the XML they need to produce.

XLSFHyperlink is the supported POI class. I am not sure if it can be used to associate a hyperlink to a picture.
Comment 4 PJ Fanning 2021-08-31 13:11:54 UTC
there is an answer on https://stackoverflow.com/questions/48141061/apache-poi-putting-hyperlink-in-images that shows how this might be done using POI for Excel but you might be able to gain some insights that help you solve this for Powerpoint
Comment 5 PJ Fanning 2021-08-31 13:22:31 UTC
LinkVideoToPptx in poi-examples may also be useful for ideas
Comment 6 PJ Fanning 2021-10-31 09:22:25 UTC
Closing as this appears to be a user misunderstanding - please feel free to reopen if you have new information