This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes validation on null string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "validation on null string" Watch "validation on null string" New topic

validation on null string

Andy Zhu
Ranch Hand

Joined: May 26, 2004
Posts: 145
This is referred to validation on the data access level: update and create. What should I do with a null string for a field? (surely it is best to be filtered at client and business logic, but sun's testing can directly call on this level.)

For example: I think of options for create (this is not required in client and thus in business logic):
1. disallow null field -- exception
2. replace \0 padding with spaces
3. simply don't verify.

The option 3 is error prone, since if the last field for create is null, it will result in an incomplete data format.

Any idea?

--------<br />Andy Zhu<br />scjp 1.4<br />scjd 1.4<br />SAS Certified Programmer 9.0
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Hi, Andy. I may be of help in your question. If you have a null string, it is best to convert it to "" and pad it with whitespace for update and create.

Anton Golovin ( SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
Andy Zhu
Ranch Hand

Joined: May 26, 2004
Posts: 145
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
checking for a null values depends on the business logic of specific method. Sometimes it is acceptable and sometimes it is not. For example: a primary key field in a record cannot be null, and an optional field might be ok to be null. If you don't allow null values in your methods, you must throw a checked exception and mention that in the method documentation. You can use IllegalArgumentException or NullPointerException. Personally, when I validate passing parameters, I use IllegalArgumnetException.

SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Andy Zhu
Ranch Hand

Joined: May 26, 2004
Posts: 145
Thanks, Hanna. You made some good points. But even the definition of primary key in this assignment is vague. But the illegal arg ex is a way to handle it.

I agree. Here's the link:
subject: validation on null string
Similar Threads
NX: Validation in update, create methods
Who should verify the sanity of data?
Strategy to place validation
Regular Expression
System.arraycopy() or clone().