File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes How to handl Null Values from Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to handl Null Values from Database" Watch "How to handl Null Values from Database" New topic

How to handl Null Values from Database

Raj Sharma

Joined: Feb 06, 2001
Posts: 12
My query returns null values from the Database.
In my Bean Class I am having get Methods which return the values fetched from the Database.
The problem is that I am getting Null Pointer Exception, if the values of any column is null.
Is there a way around to handle null values.
Neeraj Mishra

Joined: May 02, 2001
Posts: 2
As far as i understand if u get the value as null because u didn't send any value to the database
The best way is either u pass a null ie "" string to the database
Or while u are quering dont throw the exception part in the query
simply keep it empty something like this
String a= resultset.get--()
keep empty
Keep empty
Try it
With Regards
Raja Shekhar
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
Hi Sharma

Just change u r query .....
select nvl(salary,0) from emp /
select nvl(manager,'') from emp..
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
You could change your query, as mentioned, or - cleaner, IMHO - change the setter methods in your bean to reflect the business logic that null values have no special meaning:

If you are dependent on externally produced data, you could alternatively implement this in your getter methods. In either case, you should default your fields in the constructor or using static initialisers.
On a higher level, you should carefully consider the semantics of your attributes and decide whether nulls make sense or not. A null value signifies the absence of information. If information in your bean can legitimately be absent, then null values are important and should NEVER be ditched in favour of "special" values (zero, the empty string...). You may perhaps discover that the right way to get rid of your NullPointerException is to detect and handle the null value, rather than get rid of it.
- Peter

[This message has been edited by Peter den Haan (edited May 19, 2001).]
Raja Shekhar
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
Hi Peter den Haan
"Yeah !! Righly Said " Mr. Peter.
We Should Never Ditch null Values in favour of 0/empty String
but Every thing on the Earth Has Got Some Limitations...
Is what i think....
Can u please explain me How To Handle NULLPOINTEREXCEPTION
not favouring in 0/empty string
It would be great help for me,,,,,


Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Are you sure you are getting these NullPointerExceptions because the database is actually returning nulls, or because you are using a ResultSet accessor which can't access the appropriate data?
I have had lots of NPEs when reading data from database, particularly from Access using the jdbcdbc bridge. For some field types (memo fields spring to mind) you can't always get the value using getString() or even getObject(), but have to read it using a stream, as if it was a blob. Similarly, some fields in other database can give a NPE if you try and retrieve the data as the wrong format (using getInt() on a long numeric field, for example).
I suggest that you make really sure that the NullPointerExceptions you are getting are really because of Null data, and not an artefact of the way you are accessing the results.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Raj Sharma

Joined: Feb 06, 2001
Posts: 12
Thanks Everybody for your reply.
Initially, I tried the nvl(....) approach, but later abandoned it because with nvl(...) I cannot use the same classes with another database.
In Java we have a Null check. I created this method.
public String parseString(String arg){
String aarg = arg;
if(aarg == null){
aarg = "";
return aarg;
and in every getMethod I am calling this like :
public String getProduct_class(){
return parseString(product_class);
In this case if the Database returns any null values, it will initialize it to an Empty String.
guus beltman

Joined: Sep 05, 2001
Posts: 1
The function parseString() should be private of course..
I agree. Here's the link:
subject: How to handl Null Values from Database
It's not a secret anymore!