Re: null pointer

Martin Kurz <[email protected]> Sat, 28 Feb 2009 00:08:45 +0100
Newsgroups gmane.comp.jakarta.ecs.user
Message-ID <[email protected]>
Hi Thufir,

> Would there a be a better approach to this class?
> 
> It's just supposed to output a result set.  however, since there has to 
> be an update mechanism, I figured that might as well be incorporated as 
> well.
> 
> One thing I notice is that there's no real need to extend table, so I 
> think I'm taking maybe a bad approach.

Depends on your preferences. If you're in need of an ecs table 
constructed from a resultset, then i don't see any real problems with 
this approach. Maybe, i would prefer some kind of factory instead, but 
that's my personal opinion.

> Also, I'm getting a null pointer error further down, commented in CAPS.  
> At first the data is good, then a null value.  Oh, maybe the start of a 
> nonexistent column?  Not sure.

Depends on your database: If some columns allow null values, you can of 
course get NULL in a resultset. When you know the type of a cols value 
is int (col 1), you can directly get the int from the resultset by using 
  rs.getInt(j), with rs.wasNull() you can check if the last cols value 
in database was null (you're in need of this check, because when ie 
using getInt(), you don't know if the resulting int 0 is 0 because 
that's the value in database or in database there was null.

---snip---
for (int j = 1; j <= colCount; j++) {
   TD td = new TD();
   td.setPrettyPrint(isPretty);
   if (j == 1) {
       int id = Integer.parseInt(element);
       Form form = this.getDeleteButtonForm(resultSet.getInt(j));
       td.addElement(form);
   } else {
       String element =  resultSet.getString(j);
       if ( resultSet.wasNull() ) {
         td.addElement("-");
       } else {
         Form form = new ElementForm(element, 1,2);
         //form.addElement(element);
         td.addElement(form);
       }
   }
   tr.addElement(td);
   addElement(tr);
}
---snip---

grretings,

Martin