jQuery in Action, 2nd edition*
The moose likes Spring and the fly likes Null pointer Exception in  in Spring. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Null pointer Exception in  in Spring." Watch "Null pointer Exception in  in Spring." New topic
Author

Null pointer Exception in in Spring.

Shibani Joseph
Greenhorn

Joined: Mar 13, 2013
Posts: 7
Hi All,

It seems like NullPointerException is the most common Exception encountered while doing Spring.

I normally solve it by setting the if(x != null){ } condition

Can Any One please help me with an Expert Advice in dealing with Null Pointer Exceptions.

Thanks.


Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

NPEs happening a lot happen in all applications. It isn't Spring specific. NPEs is my bane in Java development. I get it a lot, and that was even before I ever used Spring.

But I will say that you just can't get around a NPE happening if an object is set to null and you try to call a method etc.

Practices are

1) check for null before calling the object
2) never leave an object at null
3) Make sure it is set.
4) You can do exception handling with AOP, but that won't stop NPEs or other exceptions, just a nice centralized location for Exception Handling.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Shibani Joseph
Greenhorn

Joined: Mar 13, 2013
Posts: 7
Thanks a Lot Mark.This will surely help.

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
Shibani Joseph wrote:Hi All,

It seems like NullPointerException is the most common Exception encountered while doing Spring.

I normally solve it by setting the if(x != null){ } condition

Can Any One please help me with an Expert Advice in dealing with Null Pointer Exceptions.

Thanks.




Check out this link: Exception Handling. It is nice one.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
nikhilg gupta
Greenhorn

Joined: Feb 22, 2011
Posts: 4
You should take care of certain things like.

Calling the instance method of a null object.
Accessing or modifying the field of a null object.
Taking the length of null as if it were an array.
Accessing or modifying the slots of null as if it were an array.
Throwing null as if it were a Throwable value.
Applications should throw instances of this class to indicate other illegal uses of the null object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null pointer Exception in in Spring.