| Kobus Grobler on Thu, 20 Feb 2003 16:53:33 +0200 (SAST) |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| [Linux dev] Re: Java NullPointerException |
Should be something like this:
ResultSet rs = null;
try
{
CallableStatement cs = conn.prepareCall("{call GetUserByName(?)}");
cs.setString(1, name);
rs = cs.executeQuery();
while(rs.next())
{
// Do Stuff
}
}
catch(SQLException ex)
{
System.out.println("ERROR getting user");
System.out.println(ex.getMessage());
}
finally
{
if (rs != null) // cause what happens if rs is null (statement
failed)?
rs.close();
}
even better would be to split it up (where do you close the statement?),
but it's a bit more typing.
CallableStatement cs = null;
try
{
cs = conn.prepareCall("{call GetUserByName(?)}");
}
catch()
{
//log error and leave procedure here
return;
}
try
{
rs = cs.executeQuery();
}
catch()
{
//log error and leave procedure here
return;
}
finally
{
cs.close();
}
try
{
while(rs.next())
{
// Do Stuff
}
}
catch()
{
//log error and leave procedure here
return;
}
finally
{
rs.close();
cs.close();
}
etc...
>
On Thu, 2003-02-20 at 16:04, Jarrod Hermer wrote:
> Hi all,
>
> I have a strange bug which I can't seem to pin down. Using Java 1.4.1 I
> have a simple app that polls a MS SQL server for data using a stored
> procedure here is my code:
>
> ------------------------------------------------------------------------
> ----
>
> ResultSet rs = null;
>
> try
> {
> CallableStatement cs = conn.prepareCall("{call
> GetUserByName(?)}");
> cs.setString(1, name);
> rs = cs.executeQuery();
> }
> catch(SQLException ex)
> {
> System.out.println("ERROR getting user");
> System.out.println(ex.getMessage());
> }
>
> while(rs.next())
> {
> // Do Stuff
> }
> rs.close();
>
> ------------------------------------------------------------------------
> ----
>
> On my development machine it functions without any problems. However on
> my production machine which I am convinced is identical the program
> crashes with a Java.NullPointerException error.
>
> Any ideas?
>
> Regards,
> Jarrod
>
>