Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for null objects

Status
Not open for further replies.

luckydexte

Programmer
Apr 26, 2001
84
US
Hello everyone,

Do you have to use a try/catch block to check for a null object? I know this is a pretty simple question but it has been bothering me for a while. An example:

I have a class called RoadCond that is a bean. So the following code throws a Null Pointer Exception if I get a nothing returned from the database.

RoadCond rc = rc.getUniqueQuery() // using Hibernate
if (!rc.equals(null) {
....

This throws the null pointer exception. Hibernate will correctly give me a RoadCond object but there is no values of course in the getters and setters. Obviously I can throw it into a try/catch block but I would think there has to be an easier way to check for a null object. Am I correct?

Thanks for any help,

Brandon
 
I tried that as well but it still throws a null pointer exception. Maybe there is no way around using a try/catch block but if someone knows of one please let me know.

Thanks for the help.

Brandon
 
RoadCond rc = rc.getUniqueQuery(); //
How can you use rc.getUniqueQuery() in the same line where you are initiallizing and assigning value to the object.
Basically rc is not initialized. Hence null pointer.

What you need is something like:
RoadCond rc = new RoadCond();
Object o = rc.getUniqueQuery();


BTW, null pointer exception is not something you should catch. Its a runtime exception.
 
Sure the exception is thrown from the if-statement? I suspect it is thrown from within the getUniqueQuery method, and if that's the case you will have to catch in.
 
nirajj said:
BTW, null pointer exception is not something you should catch. Its a runtime exception.

There's a lot to say here. It's true that you should do checks in your code to avoid null pointers, but that doesn't mean you cannot catch the exception.

Cheers,
Dian
 
Null pointers can be caused by wrong code (using an object before creating or initializing it), or in execution time by null DB records (just an example, many other causes).

The first ones got to be corrected in the code, but the others must be catched in runtime, if you want to control your app execution flow.
 
Have to agree with nirajj.

NullObject would be usually be more appropriate than checking for null.

C
 
Sorry everyone, had to go out of town on what could be called a family emergency.

The code does declare rc as a RoadCond object - I just shorthanded the code and should not have done that. So the code does go as follows:

RoadCond rc = new RoadCond();
rc.getUniqueQuery(value); // Assume 1234 for an example

The unique query method makes a database call and returns a RoadCond object. But there was no data returned because that record does not exist in the database.

So lets suppose there is a getter for RoadCond called get roadNum. If a record returns rc.getRoadNum() works fine. But if no record is returned then I get a NullPointerException which has to be handled. Make sense? So my code looks like the following:

int roadNum;
RoadCond rc = new RoadCond();
rc.getUniqueQuery(value); // Assume 1234 for an example

try {
roadNum = rc.getRoadNum();
}
catch (NullPointerException e) {
// do something
}

I would like to avoid the try/catch if possible.

Thanks again,

Brandon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top