JDK-6710755 : DOMSerializerImpl.prepareForSerialization throws NPE
  • Type: Bug
  • Component: xml
  • Sub-Component: org.w3c.dom
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2008-06-04
  • Updated: 2012-04-25
  • Resolved: 2009-06-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6 JDK 7
1.3.0 1.3Fixed 6-poolResolved 7Resolved
Related Reports
Relates :  
Description
Run the following on JDK 5:

---%<---
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.ls.DOMImplementationLS;
public class TestDOMSerializerImpl {
    public static void main(String[] args) throws Exception {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        Element el = doc.createElement("x");
        DOMImplementationLS ls = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0");
        System.out.println(ls.createLSSerializer().writeToString(el));
    }
}
---%<---

I get:

---%<---
java version "1.5.0_15"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_15-b04)
Java HotSpot(TM) Server VM (build 1.5.0_15-b04, mixed mode)

Exception in thread "main" org.w3c.dom.ls.LSException: java.lang.NullPointerException
        at com.sun.org.apache.xml.internal.serialize.DOMSerializerImpl.writeToString(DOMSerializerImpl.java:546)
        at TestDOMSerializerImpl.main(TestDOMSerializerImpl.java:10)
---%<---

The lack of a proper stack trace is recorded in bug #6710741, but using a source debugger shows that the culprit is prepareForSerialization:

  next = node.getNextSibling();

The bug appears to be fixed in JDK 6; perhaps the fix can be backported?

Comments
EVALUATION See also 6710741. Backported fix into jaxp 1.3. Will ask for jdk5 integration.
18-06-2009

WORK AROUND The following modification appears to make it work even on JDK 5: ---%<--- import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.ls.DOMImplementationLS; public class TestDOMSerializerImpl { public static void main(String[] args) throws Exception { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element el = doc.createElement("x"); doc.appendChild(el); DOMImplementationLS ls = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0"); System.out.println(ls.createLSSerializer().writeToString(doc)); } } ---%<--- Here the document is given a document element and the document, rather than this element, is serialized.
04-06-2008