JSP - TransformerFactory.newInstance().newTransformer(xsl) 返回 null?
java API 保证它永远不会返回 null - 但对我来说,它是!
相关代码:
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!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在向项目添加新的依赖项后,当我的一些测试出错时,我遇到了同样的问题。想知道当 API 说不可能时,它怎么会返回 null,我再次修改了它:
因此,我也查看了 TransformerFactory API,它说:
的,当我向项目添加新的依赖项时,问题就开始了:另一个处理 XML 文档/签名的项目。我删除了该依赖项,运行了测试(添加了依赖项后失败了)并检索了 TransformerFactory 的特定类。我重新添加了依赖项并再次运行测试:现在它是一个不同的类,因此,正如我所想,查找过程正在实例化错误的 TransformerFactory 实现,并在
我 添加的 jar 中找到它。不想通过添加另一个系统属性来修复它,所以我像这样更改了代码:
并且一切正常。
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:
So, I had a look at TransformerFactory API, too, and it says:
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:
and everything worked fine.
这个问题的答案似乎表明当 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.
输入流为空。
The input stream is empty.
在传递 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?