Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to handl Null Values from Database

 
Raj Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks,
Raj
 
Neeraj Mishra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
try
{
statement----
resultset--
while(resultset.next)
{
String a= resultset.get--()
}
catch(SqlExc)
{
keep empty
}
catch(exc)
{
Keep empty
}
Try it
With Regards
Neeraj
 
Raja Shekhar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sharma

Just change u r query .....
sample:
select nvl(salary,0) from emp /
select nvl(manager,'') from emp..
GOT IT RITE..
Raj
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,,,,,

Regards
Raj




 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raj Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Raj
 
guus beltman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The function parseString() should be private of course..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic