views:

2187

answers:

7

In my database application I sometimes have to deal with null strings in the database. In most cases this is fine, but when it comes do displaying data in a form the Swing components - using JTextField for example - cannot handle null strings. (.setText(null) fails)

(EDIT: I just noticed that JTextField actually accepts a null string, but the question remains for all other cases where unexpected null values can lead to problems.)

The null values have no special meaning, they can (must) be treated as empty strings.

What is the best practice to deal with this problem? Unfortunatly I cannot change the database.

  • Checking every value if it is null before calling setText()?
  • Adding a try-catch handler to every setText() call?
  • Introducing a static method which filters all null strings?
  • Replace all null values to empty strings immediatly after reading from the database?
  • ... [your suggestions]
A: 

If you can, add a default value - empty string - for a field in DB .

Tomo
Yes, that would be best, but unfortunatly I cannot do that :(
DR
+3  A: 

If you are using any ORM tool or somehow you map your DB fields to Java bean you can allways have:

public void setFoo(String str) {
  this.foo = str != null ? str : "";
}
Dev er dev
You do have to be careful when writing back the data to the DB, since you will be inserting an empty String instead of the NULL value!
Ruben
I think that's the best solution, but I will change the getter. (I guess that's what you meant all along)
DR
I think Marko ment the setter since that is used by most ORM tools, e.g. hibernate, to put the values from the database into the instances.When using the getter you still need to be carefull for side effects in the database when using an ORM tool.
Ruben
FWIW, I'd flip the inequality to an equality test. Just another best practice (or opinion, depending on how you look at it).public void setFoo(String str) { this.foo = str == null ? "" : str;}
jacobko
On Oracle, i don't believe this is an issue. It may depend on settings, but our 10g implementation interprets empty strings as nulls. Other DBMS's and perhaps other configurations may vary.
firebird84
Then how would you check whether the value is empty in the DB or NULL, from your Entity object?
Adeel Ansari
+2  A: 

From a SQL angle try:

select ISNULL(column_name,'') from ...
Ron Tuffin
Someone just marked this answer down :(. While I accept that the question asks about Java. This solution can be used to handle ALL unexpected null values from a database. and as the question states "unfortunately I can't change the database" it implies that non java answers are also acceptable.
Ron Tuffin
A: 

You could extend or wrap JTextField and overwrite the setText() method to replace NULL with an empty String.

Ruben
Yes, but actually "NULL" can be legal value in DB ;>
Dev er dev
Ruben
A: 

As Ruben said I would extend the JTextField to overwrite the setText() method and replace NULL with the empty string.

However I would also overwrite the getText() method to overwrite empty string with NULL so that when you are saving back into the database you do not overwrite a null value in there with the empty string.

+2  A: 

Use Beans Binding API to bind values from your entity objects to your SWING Widgets. Beanins Binding will transparently handle null values and will not replace the null with an empty string.

James Schek
+2  A: 

I think all your answers are reasonable, but since you tagged this "best practices", I'd like to remind you of the null object design pattern. Wherever it seems worth the effort, for whatever class need the protection, write special instantiation code for a "null" object of that class. The idea is this "null" object is real, and can behave appropriately no matter what you ask it to do. Your null "String" object could provide whatever you want as it's value.

This pattern also means you can get rid of lots of null checks, and the code is more robust. It does use up a bit of CPU sending messages to nulls and having them do nothing, so it is less desirable when a large percentage of objects are expected to be null.

dongilmore