title: Using Saxon Extension in XSLT to connect to SQL server NullPointerException
body: <p>Scenario:  Transforming XML to XML, but some fields I need to do a lookup in an existing database table to populate in the output XML.  I'm using SQL Server 2014. I've downloaded the Microsoft JDBC driver version 4.1. The ODBC driver has been setup under Datasources.  I'm able to connect to the database locally using my Windows Authentication.  When I compile the stylesheet however, a "nullPointerexception" occurs.  I'm not sure what I'm doing wrong or if there are additional configuration settings that need to be set on my PC.</p>
  
  <p>Operating System: Windows 7
  Saxon Version: 9.5.1.8
  TCP/IP has been enabled in the Configuration Tool for SQL Server
  IDE used to create stylesheet is Stylus Studio.  </p>
  
  <p>Below is the code snippet from the stylehsheet:  </p>
  
  <pre><code>&lt;?xml version='1.0' ?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  extension-element-prefixes="saxon sql"
  
  xmlns:sql="java:/net.sf.saxon.sql.SQLElementFactory" 
  xmlns:java="http://saxon.sf.net/java-type" 
  xmlns:saxon="http://saxon.sf.net/"&gt;
  
  &lt;xsl:variable name="count" select="0"/&gt;
  &lt;xsl:variable name="date" select="current-date"/&gt;
  &lt;xsl:variable name="time" select="current-time"/&gt;
  
  &lt;!-- insert your database details here, or supply them in parameters --&gt;
  &lt;xsl:param name="jdbc.driver" as="xs:string" select="string('com.microsoft.sqlserver.jdbc.SQLServerDriver')"/&gt;
  &lt;xsl:param name="jdbc.database" as="xs:string" select="string('jdbc:sqlserver://localhost:1433;integratedSecurity=true;databaseName=master')"/&gt;  
  
  
  &lt;xsl:template match="/"&gt;
  
  &lt;xsl:choose&gt;
      &lt;xsl:when test="element-available('sql:connect')"&gt;
          &lt;xsl:variable name="sql.conn" as="java:java.sql.Connection"&gt;
              &lt;sql:connect driver="{$jdbc.driver}" database="{$jdbc.database}"&gt;
              &lt;xsl:fallback&gt;
                  &lt;xsl:message terminate="yes"&gt; SQL extensions are not installed&lt;/xsl:message&gt;
              &lt;/xsl:fallback&gt;
              &lt;/sql:connect&gt;
              &lt;/xsl:variable&gt;
              &lt;sql:close connection="$sql.conn"/&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
          &lt;xsl:message terminate="yes"&gt;sql:connect element is not available&lt;/xsl:message&gt;
      &lt;/xsl:otherwise&gt; 
  &lt;/xsl:choose&gt;
  .......
  
  
  
   Result is a "java.lang.NullPointerException"
  java.lang.NullPointerException
  at net.sf.saxon.expr.Expression.computeDependencies(Expression.java:979)
  at net.sf.saxon.expr.Expression.computeStaticProperties(Expression.java:927)
  at net.sf.saxon.expr.Expression.getDependencies(Expression.java:351)
  at net.sf.saxon.expr.instruct.TraceExpression.getDependencies(TraceExpression.java:352)
  at net.sf.saxon.expr.Expression.computeDependencies(Expression.java:979)
  at net.sf.saxon.expr.Expression.computeStaticProperties(Expression.java:927)
  at net.sf.saxon.expr.Expression.getDependencies(Expression.java:351)
  at net.sf.saxon.expr.Expression.computeDependencies(Expression.java:979)
  at net.sf.saxon.expr.Expression.computeStaticProperties(Expression.java:927)
  at net.sf.saxon.expr.Expression.getCardinality(Expression.java:312)
  at net.sf.saxon.style.SourceBinding.getInferredType(SourceBinding.java:484)
  at net.sf.saxon.style.XSLLocalVariable.getRequiredType(XSLLocalVariable.java:61)
  at net.sf.saxon.style.StyleElement.compileSequenceConstructor(StyleElement.java:1607)
  at net.sf.saxon.style.XSLChoose.compile(XSLChoose.java:121)
  at net.sf.saxon.style.StyleElement.compileSequenceConstructor(StyleElement.java:1634)
  at net.sf.saxon.style.XSLTemplate.compileDeclaration(XSLTemplate.java:325)
  at net.sf.saxon.style.PrincipalStylesheetModule.compileStylesheet(PrincipalStylesheetModule.java:773)
  at net.sf.saxon.PreparedStylesheet.setStylesheetDocument(PreparedStylesheet.java:393)
  at net.sf.saxon.PreparedStylesheet.prepare(PreparedStylesheet.java:236)
  at net.sf.saxon.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:142)
  at net.sf.saxon.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:97)
  at com.exln.stylus.CSaxon8Driver.doProcessing(CSaxon8Driver.java:224)
  at com.exln.stylus.CProcessorDriver.process(CProcessorDriver.java:116)
  java.lang.RuntimeException
  </code></pre>