United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6710755 DOMSerializerImpl.prepareForSerialization throws NPE
JDK-6710755 : DOMSerializerImpl.prepareForSerialization throws NPE

Details
Type:
Bug
Submit Date:
2008-06-04
Status:
Closed
Updated Date:
2012-04-25
Project Name:
JDK
Resolved Date:
2009-06-18
Component:
xml
OS:
linux
Sub-Component:
org.w3c.dom
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:
1.3.0 (1.3)

Related Reports
Backport:
Backport:
Backport:
Relates:

Sub Tasks

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
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.
                                     
2008-06-04
EVALUATION

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



Hardware and Software, Engineered to Work Together