JSP - TransformerFactory.newInstance().newTransformer(xsl) 返回 null?

发布于 2024-09-30 18:06:18 字数 3537 浏览 5 评论 0原文

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,所以它一定是转换器变成了 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) 中的问题。

The java API guarantees that it will never return a null - but for me, it is!

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

Relevant code:

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);

Since xml, xsl and result were made something using new, they -must- contain not null, so it must be transformer that's becoming null. Why is this happening and how can I fix it?

Throws this:

org.apache.jasper.JasperException: Exception in 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)

EDIT: It is DEFINITELY the case that the transformer is null somehow. Watch:

org.apache.jasper.JasperException: Exception in JSP: /dca/ass2/a.jsp:49

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

And it's a null pointer exception again. If it was because xml was null then it would have thrown an NPE at 47.

EDIT2: If I change newTransformer(xsl) to newTransformer() it works, so it's a problem in newTransformer(xsl).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无悔心 2024-10-07 18:06:18

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

“获取 TransformerFactory 的新实例。此静态方法创建一个新的工厂实例此方法使用以下顺序
查找过程以确定 TransformerFactory 实现
要加载的类:使用 javax.xml.transform.TransformerFactory 系统
财产。 ... [查找顺序] ... 一旦应用程序获得了
引用一个 TransformerFactory 它可以使用工厂来配置
并获取变压器实例。”

因此,我也查看了 TransformerFactory API,它说:

“确定要创建哪个 Factory 实现的系统属性名为“javax.xml.transform.TransformerFactory”。这
属性命名 TransformerFactory 抽象的具体子类
班级。如果未定义该属性,则平台默认值为
使用过。”

的,当我向项目添加新的依赖项时,问题就开始了:另一个处理 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

并且一切正常。

I ran into the same problem, when some of my tests went wrong, after having added a new dependency to the project. Wondering how could this return null when its API says it's impossible, I revised it again:

"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."

So, I had a look at TransformerFactory API, too, and it says:

"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."

As I mentioned, the problem started when I added a new dependency to the project: another project that processes XML documents/signatures. I deleted the dependency, ran the test (that failed with the dependency added) and retrieved the specific class of the TransformerFactory. I added the dependency back and ran the test again: it was a different class now. So, as I supposed, the lookup procedure was instantiating a wrong TransformerFactory implementation, finding it in the added jar.

I didn't want to fix it adding another system property, so I changed the code like that:

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

and everything worked fine.

自演自醉 2024-10-07 18:06:18

这个问题的答案似乎表明当 XSL 无效时会发生这种情况,这可以解释我当前遇到的情况。

在我们的具体案例中,使用的 XML 编辑器默默地在文件的第一个位置插入了一个不可见的字符。打印文件的前几个字节表明了这一点。使用更强大的编辑器,我们从文件中删除了有问题的字节,一切都很好。

This question's answer seems to indicate this will happen when the XSL is invalid, which would explain what I'm currently running into.

In our specific case, an XML editor that was used silently inserted an invisible character in the first position of the file. Printing the first few bytes of the file showed this. Using a more powerful editor, we removed the offending bytes from the file and all was well.

眼泪都笑了 2024-10-07 18:06:18

输入流为空。

The input stream is empty.

枕花眠 2024-10-07 18:06:18

在传递 xml 之前检查它是否为 null/空。我想你会发现它是空的。

例如 new StreamSource(new URL(mondialURL).openStream()) 返回 null 或空对象?

Check whether xml is null/empty before passing it it. I think you'll find it's empty.

e.g. is new StreamSource(new URL(mondialURL).openStream()) returning null or and empty object?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文