During my database application I sometimes suffer from
null strings within the database. Generally this really is fine, but as it pertains do exhibiting data inside a make up the Swing components - using
JTextField for instance - cannot handle null strings. (
(EDIT: I simply observed that
JTextField really accepts a
null string, however the question remains for those other cases when unpredicted
null values can result in problems.)
The null values don't have any special meaning, they are able to (must) be treated as empty strings.
What's the best practice to cope with this issue? Unfortunatly I am unable to alter the database.
- Checking every value if it's
- Adding an attempt-catch handler to each
- Presenting a static method which filters all
- Replace all
nullvalues to empty strings immediatly after reading through in the database?
- ... [your suggestions]
If you work with any ORM tool or in some way you map your DB fields to Java bean you are able to allways have:
public void setFoo(String str)
From the SQL position try:
select ISNULL(column_name,'') from ...
Use Beans Binding API to bind values out of your entity objects for your SWING Icons. Beanins Binding will transparently handle null values and won't replace the null by having an empty string.
I believe all of your solutions are reasonable, consider you labeled this "guidelinesInch, Let me help remind you from the null object design pattern. Wherever it appears well worth the effort, for whatever class require the protection, write special instantiation code for any "null" object of this class. The concept is "null" object is real, and may behave properly regardless of what you request it to complete. Your null "String" object could provide anything you want as it is value.
This pattern does mean you are able to eliminate plenty of null inspections, and also the code is much more robust. It will consume a little of CPU delivering messages to nulls and getting them do nothing at all, so it's less desirable when a lot of objects are required to become null.
If you're able to, give a default value - empty string - for any area in DB .
You can extend or wrap JTextField and overwrite the setText() approach to replace NULL by having an empty String.
As Ruben stated I'd extend the JTextField to overwrite the setText() method and replace NULL using the empty string.
However would also overwrite the getText() approach to overwrite empty string with NULL to ensure that when you're saving into the database you don't overwrite a null value inside using the empty string.