Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Null Pointer Problem with XML Transformer

26 views
Skip to first unread message

Dick Adams

unread,
May 14, 2002, 4:04:18 PM5/14/02
to
I'm getting an aggravating null pointer exception in the following
function:

private synchronized String toXml(Document doc)
throws TransformerFactoryConfigurationError,
TransformerConfigurationException,
TransformerException
{
StringWriter writer = new StringWriter() ;
StreamResult result = new StreamResult(writer) ;
TransformerFactory factory = TransformerFactory.newInstance();
Transformer xformer = factory.newTransformer() ;
DOMSource source = new DOMSource(doc) ;
xformer.transform(source, result) ;
return writer.toString() ;
}

The offending line is the xformer.transform(source, result) call.
Here's the stack trace:

<May 14, 2002 3:00:10 PM CDT> <Error> <HTTP>
<[WebAppServletContext(10717459,DefaultWebApp,/DefaultWebApp)] Servlet
failed with Exception
java.lang.NullPointerException
at weblogic.apache.xml.utils.TreeWalker.dispatachChars(TreeWalker.java:223)
at weblogic.apache.xml.utils.TreeWalker.startNode(TreeWalker.java:353)
at weblogic.apache.xml.utils.TreeWalker.traverse(TreeWalker.java:131)
at weblogic.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:323)
at com.spcs.cams.xml.XmlServlet.toXml(XmlServlet.java:143)
at com.spcs.cams.xml.XmlServlet.sendReply(XmlServlet.java:106)
at com.spcs.cams.dirassist.DirAssistServlet.doPost(DirAssistServlet.java:164)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:265)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:200)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:2495)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2204)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
>

I've tried stepping into the transform function, but the debugger
(Eclipse) for some reason won't show me what's happening.

As far as I can tell the doc object is built correctly, obviously
something is wrong. Does anyone have a suggestion?

Laurence V

unread,
May 15, 2002, 7:42:42 AM5/15/02
to
On 14 May 2002 13:04:18 -0700, dada...@sprintspectrum.com (Dick
Adams) wrote:

>I'm getting an aggravating null pointer exception in the following
>function:
>
>private synchronized String toXml(Document doc)
> throws TransformerFactoryConfigurationError,
> TransformerConfigurationException,
> TransformerException
> {
> StringWriter writer = new StringWriter() ;
> StreamResult result = new StreamResult(writer) ;
> TransformerFactory factory = TransformerFactory.newInstance();
> Transformer xformer = factory.newTransformer() ;
> DOMSource source = new DOMSource(doc) ;
> xformer.transform(source, result) ;
> return writer.toString() ;
> }
>

>As far as I can tell the doc object is built correctly, obviously
>something is wrong. Does anyone have a suggestion?

Yes, use System.out.println() - or anything a bit more advanced - to
show the contents of your key variables as you approach your crashing
statement. Basically, include ASSERT statements that assert that your
program's state reflects what you think the state should be. As soon
as the state deviates from what you asserted it should be.. you've
located your bug.

L.

---
World-on-a-Disc, Interactive Map-Driven CD-ROMs Bursting with Photographs.
http://www.worldonadisc.com

Steve Chartley

unread,
May 31, 2002, 6:49:31 PM5/31/02
to
I've experienced a similar problem in a situation where
I was constructing the DOM programatically. The following
code will throw NullPointerException when you call:
transformer.transform(new DOMSource(doc), new StreamResult(outStream)).

String s = null;
Node c = doc.createCDATASection(s);
doc.appendChild(c);

It is likely (I haven't verified this) that the same problem
occurs with:
String s = null;
Node t = doc.createTextNode(s);
doc.appendChild(t);

Hope this helps and hope that everyone that has solutions
posts them because it has really saved my butt a few times.

Steve Chartley


dada...@sprintspectrum.com (Dick Adams) wrote in message news:<8bc5baf7.02051...@posting.google.com>...

0 new messages