gpt4 book ai didi

java - JSP - TransformerFactory.newInstance().newTransformer(xsl) 返回空值?

转载 作者:数据小太阳 更新时间:2023-10-29 02:55:08 26 4
gpt4 key购买 nike

Java API 保证它永远不会返回 null - 但对我来说,它是!

http://download.oracle.com/javase/6/docs/api/javax/xml/transform/TransformerFactory.html#newTransformer(javax.xml.transform.Source )

相关代码:

xml = new StreamSource(new URL(mondialURL).openStream());
xsl = new StreamSource(new File("/REMOVED/countriesnofilter.xsl"));
result = new StreamResult(new PrintWriter(out));

transformer = TransformerFactory.newInstance().newTransformer(xsl);
transformer.transform(xml, result);

由于 xml、xsl 和结果是使用 new 生成的,因此它们 - 必须 - 不包含 null,因此它必须是变空的转换器。为什么会发生这种情况,我该如何解决?

抛出这个:

org.apache.jasper.JasperException:JSP 中的异常:/dca/ass2/a.jsp:46

43:  result = new StreamResult(new PrintWriter(out));
44:
45: transformer = TransformerFactory.newInstance().newTransformer(xsl);
46: transformer.transform(xml, result);
47:
48: %>
49: <INPUT TYPE="SUBMIT" VALUE="Get one country">


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
sun.reflect.GeneratedMethodAccessor50.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
root cause

java.lang.NullPointerException
org.apache.jsp.dca.ass2.a_jsp._jspService(a_jsp.java:104)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
sun.reflect.GeneratedMethodAccessor50.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)

编辑:肯定是变压器以某种方式为空的情况。观看:

org.apache.jasper.JasperException:JSP 中的异常:/dca/ass2/a.jsp:49

46:     
47: xml.getInputStream();
48: xsl.getInputStream();
49: transformer.reset();
50:
51: //transformer.transform(xml, result);
52:

又是一个空指针异常。如果是因为 xml 为空,那么它会在 47 处抛出 NPE。

EDIT2:如果我将 newTransformer(xsl) 更改为 newTransformer() 它会起作用,所以这是 newTransformer(xsl) 中的问题。

最佳答案

我遇到了同样的问题,当我的一些测试出错时,在向项目添加了新的依赖项之后。想知道当它的 API 说这是不可能的时候,它怎么会返回 null,我再次修改了它:

"Obtain a new instance of a TransformerFactory. This static method creates a new factory instance This method uses the following ordered lookup procedure to determine the TransformerFactory implementation class to load: Use the javax.xml.transform.TransformerFactory system property. ... [lookup order] ... Once an application has obtained a reference to a TransformerFactory it can use the factory to configure and obtain transformer instances."

所以,我也查看了 TransformerFactory API,它说:

"The system property that determines which Factory implementation to create is named "javax.xml.transform.TransformerFactory". This property names a concrete subclass of the TransformerFactory abstract class. If the property is not defined, a platform default is be used."

正如我提到的,当我向项目添加一个新的依赖项时,问题就开始了:另一个处理 XML 文档/签名的项目。我删除了依赖项,运行了测试(由于添加了依赖项而失败)并检索了 TransformerFactory 的特定类。我添加了依赖项并再次运行测试:现在它是一个不同的类。因此,正如我所料,查找过程正在实例化一个错误的 TransformerFactory 实现,在添加的 jar 中找到它。

我不想通过添加另一个系统属性来修复它,所以我这样更改了代码:

private static final String TRANSFORMER_FACTORY_CLASS = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";

TransformerFactory factory = TransformerFactory.newInstance(TRANSFORMER_FACTORY_CLASS, null);
//null for default ClassLoader

一切正常。

关于java - JSP - TransformerFactory.newInstance().newTransformer(xsl) 返回空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4096295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com