File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null pointer Exception in in Spring.

 
Shibani Joseph
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shibani Joseph
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a Lot Mark.This will surely help.

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nikhilg gupta
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic