Handling NullPointerException

0 views
Skip to first unread message

javatechnicals

unread,
Feb 6, 2010, 5:22:10 AM2/6/10
to javatechnicals
Please suggest best practices for handling NullPointerException?

Rejin Chandran

unread,
Feb 6, 2010, 12:24:23 PM2/6/10
to javatec...@googlegroups.com
not null checking.

Baiju Chandran

unread,
Feb 9, 2010, 8:00:37 AM2/9/10
to javatec...@googlegroups.com
best practice is to check each Object for Null before using

eg string obj
obj != null && obj.anyofit member
List
list == null || (list !=null && list.isEmpty)
etc

On Sat, Feb 6, 2010 at 3:52 PM, javatechnicals <javatec...@googlegroups.com> wrote:

Rejin Chandran

unread,
Feb 9, 2010, 8:22:00 AM2/9/10
to javatec...@googlegroups.com

Object obj[] = new Object[3];

if ( obj != null ){
 System.out.println("The value is not null");
}else {
 System.out.println("The value is null");
}

 

in the case of arrays how you will solve????

Baiju Chandran

unread,
Feb 10, 2010, 7:52:30 AM2/10/10
to javatec...@googlegroups.com
 

  In the case of array of objects handling of null pointer exception is a tedious task, we have to check each and every individual elements before its usage



 public static boolean isNullOrEmpty(Object[] array) {
        if (array == null || (array != null && array.length == 0)) {
            return true;
        } else {
            return false;

Effective Java

unread,
Feb 10, 2010, 11:03:42 AM2/10/10
to javatechnicals
I like last solution written by baiju.

I found same solution in Spring framework's source code.
They are created a class named Assert and written a static method for
checking the
Null
Thank you Baiju annan and spring source

On Feb 10, 5:52 pm, Baiju Chandran <baijuchandr...@gmail.com> wrote:
>   In the case of array of objects handling of null pointer exception is a
> tedious task, we have to check each and every individual elements before its
> usage
>
>  public static boolean isNullOrEmpty(Object[] array) {
>         if (array == null || (array != null && array.length == 0)) {
>             return true;
>         } else {
>             return false;
>         }
>     }
>
> On Tue, Feb 9, 2010 at 6:52 PM, Rejin Chandran <reji...@gmail.com> wrote:
> > Object obj[] = new Object[3];
>
> > if ( obj != null ){
> >  System.out.println("The value is not null");
> > }else {
> >  System.out.println("The value is null");
> > }
>
> > in the case of arrays how you will solve????

Reply all
Reply to author
Forward
0 new messages