2

我有 spring mvc 项目,我上传 xml 文件并尝试通过 DOM 将其写入我的 xml,但遇到此异常。当我尝试在通常的项目中执行相同的过程时 - 一切正常。不明白为什么会这样。。

    type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)


root cause 

java.lang.NullPointerException
    org.apache.xml.utils.TreeWalker.dispatachChars(TreeWalker.java:303)
    org.apache.xml.utils.TreeWalker.startNode(TreeWalker.java:447)
    org.apache.xml.utils.TreeWalker.traverse(TreeWalker.java:202)
    org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:343)
    com.mycompany.xmlloader.xmlHandler.DOMParser.saveToCatalog(DOMParser.java:147)
    com.mycompany.xmlloader.service.XMLServiceImpl.updateCatalog(XMLServiceImpl.java:48)
    com.mycompany.xmlloader.controller.XMLController.uploadToCatalog(XMLController.java:47)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

和代码片段:

TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        StreamResult result = new StreamResult(catalogFile.toURI().getPath());
        Source source = new DOMSource(doc);
        trans.transform(source, result);
4

1 回答 1

4

您的问题的可能原因是您的Text节点中的Document数据集为null. (有关该问题的讨论,请参见此处此处。)

您可以Document使用以下代码检查您的诊断是否正确:

public static void checkDoc(Node n) {
  if (n instanceof Text) {
    if (((Text) n).getData() == null) {
      System.err.println("null data!!!!");
    }
  }

  NodeList l = n.getChildNodes();
  for (int i = 0; i < l.getLength(); ++i) {
    checkDoc(l.item(i));
  }
}

致电checkDoc您的Document

checkDoc(doc);

如果您看到任何输出,那么您现在知道 NPE 的直接原因。那么问题的解决方案是:不要使用空数据构建Document包含节点。Text

于 2013-06-09T12:24:07.987 回答