• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ResultSet NullPointerException problem

 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
I'm having difficulties with accessing a MySQL database resultset in a JSP.
The error I'm getting is this:
The following JDBC exception has occurred : java.lang.NullPointerException
The reason I get it is because some columns in the database can be null and then the "ResultSet resultset.getString(4).trim();" code produces this error.
I've been over the java.sql class docs to see if I cound find a boolean to indicate whether or not a column was null but I couldn't find anything.
Ideas?
Thanks,
Terry
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Terence Doyle:
HI,
I'm having difficulties with accessing a MySQL database resultset in a JSP.
The error I'm getting is this:
The following JDBC exception has occurred : java.lang.NullPointerException
The reason I get it is because some columns in the database can be null and then the "ResultSet resultset.getString(4).trim();" code produces this error.
I've been over the java.sql class docs to see if I cound find a boolean to indicate whether or not a column was null but I couldn't find anything.
Ideas?
Thanks,
Terry


Try using :
String s = resultset.getString(4);
if(s!=null){
s = s.trim();
}

Shilpa.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sure you're not missing
ResultSet rs;
rs.next();
if you don't issue this first time u want to access the result set you will get the null pointer exception
------------------
KaReEm
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
Thanks guys.
The null test was the trick!
I didn't realise the trim() method threw an exception id the String passed in was null.
Bye,
Terry
reply
    Bookmark Topic Watch Topic
  • New Topic