Project

Profile

Help

Bug #5937

closed

Extension function gives NullPointerException in 12.1 when returning empty sequence

Added by Martin Honnen over 1 year ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Category:
s9api API
Sprint/Milestone:
Start date:
2023-03-23
Due date:
% Done:

100%

Estimated time:
Legacy ID:
Applies to branch:
12, trunk
Fix Committed on Branch:
12, trunk
Fixed in Maintenance Release:
Platforms:
Java

Description

I am struggling to get an extension function to run with 12.1 HE and Java 8; the sample code is

package org.example;

import net.sf.saxon.s9api.*;

import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;

public class Main {
    public static void main(String[] args) throws SaxonApiException {
        Processor processor = new Processor(false);

        processor.registerExtensionFunction(new ExtensionFunction() {
            @Override
            public QName getName() {
                return new QName("http://example.math.co.uk/demo", "sqrtSimple");
            }

            @Override
            public SequenceType[] getArgumentTypes() {
                return new SequenceType[]
                        {
                                SequenceType.makeSequenceType(ItemType.DOUBLE, OccurrenceIndicator.ZERO_OR_ONE)
                        };
            }

            @Override
            public SequenceType getResultType() {
                return SequenceType.makeSequenceType(ItemType.DOUBLE, OccurrenceIndicator.ZERO_OR_ONE);
            }

            @Override
            public XdmValue call(XdmValue[] xdmValues) throws SaxonApiException {
                return xdmValues[0].isEmpty() ? XdmEmptySequence.getInstance() : new XdmAtomicValue(Math.sqrt(((XdmAtomicValue) xdmValues[0]).getDoubleValue()));
            }
        });

        String xslt = "<xsl:transform version='3.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" +
                "    xmlns:math='http://example.math.co.uk/demo'> " +
                " <xsl:template name='xsl:initial-template'> " +
                "   <out sqrt2='{math:sqrtSimple(2.0e0)}' " +
                "        sqrtEmpty='{math:sqrtSimple(())}'/> " +
                " </xsl:template>" +
                " </xsl:transform>";

        Xslt30Transformer xslt30Transformer = processor.newXsltCompiler().compile(new StreamSource(new StringReader(xslt))).load30();

        xslt30Transformer.callTemplate(null, processor.newSerializer(System.out));

    }

}

Running that gives the error

Exception in thread "main" java.lang.NullPointerException
	at net.sf.saxon.trans.Err.depict(Err.java:170)
	at net.sf.saxon.expr.parser.RoleDiagnostic.composeErrorMessage(RoleDiagnostic.java:261)
	at net.sf.saxon.functions.IntegratedFunctionCall.evaluateItem(IntegratedFunctionCall.java:350)
	at net.sf.saxon.expr.elab.FallbackElaborator.lambda$elaborateForItem$2(FallbackElaborator.java:57)
	at net.sf.saxon.expr.AtomicSequenceConverter$AtomicSequenceConverterElaborator.lambda$elaborateForItem$1(AtomicSequenceConverter.java:527)
	at net.sf.saxon.expr.elab.PullElaborator.lambda$elaborateForUnicodeString$3(PullElaborator.java:76)
	at net.sf.saxon.expr.elab.Elaborator.lambda$elaborateForString$0(Elaborator.java:184)
	at net.sf.saxon.expr.instruct.FixedAttribute$FixedAttributeElaborator.lambda$elaborateForPush$1(FixedAttribute.java:359)
	at net.sf.saxon.expr.instruct.Block$BlockElaborator.lambda$elaborateForPush$1(Block.java:856)
	at net.sf.saxon.expr.instruct.FixedElement$FixedElementElaborator.lambda$elaborateForPush$0(FixedElement.java:653)
	at net.sf.saxon.expr.instruct.NamedTemplate.expand(NamedTemplate.java:274)
	at net.sf.saxon.trans.XsltController.callTemplate(XsltController.java:872)
	at net.sf.saxon.s9api.Xslt30Transformer.callTemplate(Xslt30Transformer.java:503)
	at org.example.Main.main(Main.java:47)

I think what fails is the sqrtEmpty='{math:sqrtSimple(())}'. Although I don't have a project with that code with Saxon 11 I think that code worked fine with Saxon 11.

Do I have to change the code for Saxon 12 or is the NullPointerException a bug?

Please register to edit this issue

Also available in: Atom PDF