5

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]

7 Answers 7

6

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 : "";
}
Sign up to request clarification or add additional context in comments.

6 Comments

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!
I think that's the best solution, but I will change the getter. (I guess that's what you meant all along)
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.
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; }
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.
|
4

From a SQL angle try:

select ISNULL(column_name,'') from ...

1 Comment

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.
2

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.

Comments

2

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.

Comments

0

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

1 Comment

Yes, that would be best, but unfortunatly I cannot do that :(
0

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

2 Comments

Yes, but actually "NULL" can be legal value in DB ;>
Indeed, that is why you would wrap setText() & getText(), so you can interchange null with "" for setText and vice versa for getText.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.