File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Null checking required for objects created by me before? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Null checking required for objects created by me before?" Watch "Null checking required for objects created by me before?" New topic
Author

Null checking required for objects created by me before?

Karan Jain
Ranch Hand

Joined: May 30, 2007
Posts: 82
Hi,
I am initializing many objects in the init and doPost method of my servlet and storing it in a hashmap. If object instantiation fails i am not proceeding further and logging appropriate error message and show the user "Some internal error occured" and stop the processing.
These objects will be used down the line in my java classes.

1) Please suggest if this approach is fine to show user this message?
2)I am only inserting it in hashmap and making sure it is not-null while inserting. So Do i need to do null checking for these objects after retrieving it from the hashmap?

Any suggestions/ideas are welcome.

Thanks and regards,
Karan
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30919
    
158

1) Please suggest if this approach is fine to show user this message?

Sure. If it is a fatal error. It doesn't sound like there is anything more user friendly you can show. IF you have a help desk or webmaster e-mail, you may want to include this in the error message.

2)I am only inserting it in hashmap and making sure it is not-null while inserting. So Do i need to do null checking for these objects after retrieving it from the hashmap?

Would you do anything if it was null? If not, does it matter if you crash with a null pointer or some other error. Even though it can't be null, someone may change the code in the future.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Karan Jain
Ranch Hand

Joined: May 30, 2007
Posts: 82
Thanks for your reply...

Would you do anything if it was null? If not, does it matter if you crash with a null pointer or some other error. Even though it can't be null, someone may change the code in the future.


I will not able to proceed if any of those objects are null. So i will end up in the same message as "Internal Error".
i do understand your point about someone changing the code. But it will really create a mess in my code .
I think i might end up checking nulls. Any suggestion for making it bit cleaner.

Thanks and regards,
Karan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null checking required for objects created by me before?