[prev in list] [next in list] [prev in thread] [next in thread] 

List:       groovy-user
Subject:    [groovy-user] MarkupBuilder and null values
From:       "Dustin Whitney" <dustin.whitney () gmail ! com>
Date:       2007-01-30 5:04:22
Message-ID: 23def8000701292104i684e0084l86365eee4256aca8 () mail ! gmail ! com
[Download RAW message or body]

I am trying to select some info out of a table and build an XML document.
When I get a null value from the table and try to put it into an element, I
get a null pointer exception:

For example:

sql.eachRow("select * from service_provider LIMIT 0, 10"){ result ->
    def writer = new StringWriter()
    def xml = new MarkupBuilder(writer)
    xml.service(){
        serviceName(result.SERVICE_NAME)
}

if result.SERVICE_NAME is NULL then I get a NullPointerException.  I can
check each field to see if it's null, like:

if(result.SERVICE_NAME){
     serviceName(result.SERVICE_NAME)
}else{
    serviceName()
}

But, when I've got a table with 40 columns, this method sucks.  I guess I
could write a method to return either the data or an empty string in the
case of null, but IMHO, it seems like the right thing to do is for the
MarkupBuilder to simply create an empty element when there is a NULL.  Am I
wrong - Is there a better way to do what I'm doing, or what?

-Dustin

[Attachment #3 (text/html)]

I am trying to select some info out of a table and build an XML document.&nbsp; When \
I get a null value from the table and try to put it into an element, I get a null \
pointer exception:<br><br>For example: <br><br>sql.eachRow(&quot;select * from \
service_provider LIMIT 0, 10&quot;){ result -&gt;  <br>&nbsp;&nbsp;&nbsp; def writer \
= new StringWriter()<br>&nbsp;&nbsp;&nbsp; def xml = new \
MarkupBuilder(writer)<br>&nbsp;&nbsp;&nbsp; xml.service(){<br>&nbsp;&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp; serviceName(result.SERVICE_NAME)<br>}<br><br>if \
result.SERVICE_NAME is NULL then I get a NullPointerException.&nbsp; I can check each \
field to see if it&#39;s null, like: \
<br><br>if(result.SERVICE_NAME){<br>&nbsp;&nbsp;&nbsp;&nbsp; \
serviceName(result.SERVICE_NAME)<br>}else{<br>&nbsp;&nbsp;&nbsp; \
serviceName()<br>}<br><br>But, when I&#39;ve got a table with 40 columns, this method \
sucks.&nbsp; I guess I could write a method to return either the data or an empty \
string in the case of null, but IMHO, it seems like the right thing to do is for the \
MarkupBuilder to simply create an empty element when there is a NULL.&nbsp; Am I \
wrong - Is there a better way to do what I&#39;m doing, or what? <br><br>-Dustin<br>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic