【发布时间】:2015-01-11 18:33:44
【问题描述】:
我在尝试调用从org.w3c.dom.Node.getTextContent() 返回的对象的方法java.lang.String.trim() 时得到java.lang.NullPointerException:。
当我使用时
List<String> welcomeFiles = WebXml.INSTANCE.getWelcomeFiles();
来自 ManagedBean,如下所示:
@ManagedBean
@RequestScoped
public class LogoffControl implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* log4j logger
*/
protected static final Logger LOGGER = LogFactory
.getLogger(LogoffControl.class);
public String logoff() {
List<String> welcomeFiles = WebXml.INSTANCE.getWelcomeFiles();
try {
Faces.redirect(welcomeFiles.get(0));
} catch (IOException e) {
LOGGER.error("Redirect Failed: " + e.getMessage());
}
Faces.invalidateSession();
return "";
}
}
我收到NullPointerException,说我的 web.xml 中可能有错字。我已经调试过了,我看到WebXML.java第414行中xpath执行的结果成功了,我把它放到第418行的循环中
welcomeFiles.add(welcomeFileList.item(i).getTextContent().trim());
在调试器中,welcomeFileList.item(i) 的计算结果为 <welcome-file>index.jsp</welcome-file>,但调用 welcomeFileList.item(i).getTextContent() 的结果为 null。我想不通...我错过了什么?
在日志中:
Jan 11, 2015 1:36:37 PM org.omnifaces.config.WebXml init
SEVERE: WebXml failed to initialize. Perhaps your web.xml contains a typo?
java.lang.NullPointerException: while trying to invoke the method java.lang.String.trim() of an object returned from org.w3c.dom.Node.getTextContent()
at org.omnifaces.config.WebXml.parseWelcomeFiles(WebXml.java:418
Oracle JSF 2.1.26、Omnifaces 1.8.1、servlet 2.5、sapjvm6.1 (Java6),在 SAPNetweaver 7.4 服务器上。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>tablemaint-web</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
....
</webapp>
【问题讨论】:
-
尴尬。我怀疑正在使用的 JAXP 解析器中存在错误。环境中使用了哪个 JAXP 解析器?你能用
getFirstChild().getNodeValue()而不是getTextContent()在本地试试吗? -
我可以确认 getFirstChild().getNodeValue() 确实有效。 JAXP 提供程序来自 SAP com.sap.engine.lib.jaxp.SAXParserFactoryImpl
-
我现在注意到 WebXML 类是由 omnifaces faces-config.xml 自动初始化的(我认为是通过 OmniPartialViewContext 配置),所以即使没有上面的特定代码,我也会得到这个 NullPointerException。如您所料,在此平台上,其他 getTextContent() 方法会引发相同的错误。我不太希望 SAP 修复这个错误,是否有机会解决这个问题?
-
我可以将
getTextContent()替换为getFirstChild().getNodeValue()。毕竟只是获取文本节点的节点值的简写。