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?

Actions #1

Updated by Martin Honnen over 1 year ago

I have now tested the code in a project using Saxon HE 11.5 and there it doesn't give an exception but outputs the expected <?xml version="1.0" encoding="UTF-8"?><out xmlns:math="http://example.math.co.uk/demo" sqrt2="1.4142135623730951" sqrtEmpty=""/>.

Actions #2

Updated by Michael Kay over 1 year ago

  • Tracker changed from Support to Bug
  • Subject changed from Extension function gives NullPointerException in 1`2.1: is some code change necessary from 11 to 12 extension functions? to Extension function gives NullPointerException in 12.1: is some code change necessary from 11 to 12 extension functions?

Problem reproduced. With a function declared as returning a ZERO_OR_ONE result, an empty sequence result isn't being correctly handled.

Actions #3

Updated by Michael Kay over 1 year ago

  • Subject changed from Extension function gives NullPointerException in 12.1: is some code change necessary from 11 to 12 extension functions? to Extension function gives NullPointerException in 12.1 when returning empty sequence
  • Status changed from New to Resolved
  • Applies to branch trunk added
  • Fix Committed on Branch 12, trunk added
Actions #4

Updated by Michael Kay over 1 year ago

Note, the simplest workaround until you get the bug fix may be to declare the return type as xs:double*.

Actions #5

Updated by O'Neil Delpratt over 1 year ago

  • Status changed from Resolved to Closed
  • % Done changed from 0 to 100
  • Fixed in Maintenance Release 12.2 added

Bug fix applied in the Saxon 12.2 maintenance release

Please register to edit this issue

Also available in: Atom PDF