[jdom-interest] NullPointerException when changing or adding content (setText() or addContent());

Per Norrman per.norrman at austers.se
Wed Aug 10 15:46:44 PDT 2005


Hard to say, but ...

Element#setText(String) should not throw a NullPointerException, so:

1) you don't really have the element you think you have, so you're
not reading the document you think you are when in the servlet container.

2) The stuff that you pass to setText is indirectly giving you the exception.


Always log the complete exception stack trace.

Separate nested method calls, i.e. use:
    Stuff stuff = object.getStuff();
    String text = stuff.getText();

    instead of:


Use a debugger.

Use assert statements and enable them.


Patrick Finch skrev:
> - The short, short, version:
> I have a servlet that passes an XML document as a String object to a 
> method contained in a jar file in the war file.
> That method (parseRequest()), can read XML template documents form disk, 
> and return them as a String.
> That method can create new documents from scratch, add stuff to them, 
> and return them as as a String.
> That method can, so long as I run the jar file (java -jar <jarfile>), 
> edit an XML template file and return the edited version as a String.
> When that method is used by a servlet, I receive a NullPointerException 
> when I try to alter the template file (not writing it back to disk, 
> altering it in memory).
> - The long and windy version.
> Arg, I've been having this problem for a couple of days.
> Anyway, I have a servlet, the servlet's soul job in life is to recieve a 
> URL encoded XML document via an http POST request, and then, and then 
> pass that document as a String to a method living in a jar file, packed 
> in the war file. That method, the one in the jar file, takes the String, 
> turns it back into an XML document, parses it, and then acts on what it 
> reads, returning an XML document that has been XMLOutputted to a String 
> object. The XML document it returns is read from a central location on 
> disk (not in the webroot, not anywhere special at all as far as I can 
> tell). This all works fine.
> What doesn't work fine is when I read an XML document from the disk (in 
> preperation of sending back to the servlet) and then try to edit a child 
> with setText, or addContent, or anything else. Whenever I do that, the 
> servlet returns an HTTP 500 saying that there was a NullPointerException 
> in the jar-file code at the setText() operation.
> Here's the kicker, I decided to test most of this jar code using a Test 
> class that reads an XML document from disk, converts it to a String, and 
> then sends that to the parseRequest method. That works fine, no 
> exceptions, it edits the template document from the disk, and returns 
> it. The error happens when the servlet runs the jar file, instead of me 
> running it locally.
> I can, however, create a new root node, a new document and add content, 
> and nothing blows up.
> - Plea
> If anyone has any idea what's going on (I'm probably just being dumb), I 
> would appreciate your advice.
> Thanks,
> Pat
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com

More information about the jdom-interest mailing list