Java Discussion: "Null values assistance sought"

by Earthweb Staff

We reach into our Discussions mailbag for a thread that may be of interest. Feel free to participate. Ask a question or offer some advice. Join your fellow Java developers in the Gamelan discussion forum. Registration is simple and free.

Welcome. Click here to access the new home of Gamelan's discussions on Java.

From the Gamelan Forum...

Null values assistance sought

Big Swifty - 10:09am Jan 14, 2002 EDT

I have written a Java application front-end connecting to a SOL Server 7.0 abase on our NT4 Server. Everything works great except my application errors whenever a field in the record set is null. Is there a way I can overcome this and have the records shown even if some fields are null? I have ASP scripts on our web site which pull the same records from the same data base without any problem. Any suggestions? Many thanks Chris

Norm R - Jan 14, 02 (1.)

You don't show the java source code that has the problem with null values. That would help to find a solution for you. Anytime a method can return a null value, you must test for it and write code to handle it. For example:

  variable = method(); // get a value from method 
  if(variable == null) { 
    ... handle null 
  }else { 
     ... use variable 

Big Swifty - 05:45am Jan 15, 2002 EDT (2.) (2.)

Hi and thanks for the reply. The error occurs in the method where I assign to a variable each of the returned colums in the recordset, as:

String myField1 = database.getValueAt(0, 1)).toString(); 
String myField2 = database.getValueAt(0, 2)).toString(); 
I take it the toString() method is causing the problem...as the field in question contains a Null value. I accept your suggestion for a test...I suppose I need to iterate over the resultset and test each and every field as to its "nullness" and replace those found with an empty string ("")...something like that do you reckon? Thanks Chris

Norm R - 09:48am Jan 15, 2002 EDT (3.)

From your code, I assume that the getValueAt() method is returning null, in which case the call to toString() will fail with an error. So you need to test if the returned value is null and set the appropriate value for a null field: "" for a string, 0 for numberic , etc.

We hope you'll participate in the give and take of information with your peers that has been a hallmark of our site since the early days of Java technology.

If you've got a problem, here's the perfect place to ask others for help solving it. If you've got advice, here's the perfect place to help build a community of like-minded Java specialists. Enjoy!

The Dalang

This article was originally published on Friday Jan 18th 2002
Mobile Site | Full Site