cancel
Showing results for 
Search instead for 
Did you mean: 

HOw to handle the exception..

Former Member
0 Kudos

Dear All,

How can i trap the exception.

<b>java.lang.NullPointerException</b>

Please send me the code..

Regds,

Anup

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Anup,

Before accessing any property or methods of an object you can check that object for a null.

Like

if ( myObject != NULL ){
           here access its properties or methods..
         }
         else {
           first create a new object and use it..
            
         }

Hope this helps you.

Regards,

Narinder Hartala

Former Member
0 Kudos

Thanks Narinder,

One more thing..

how to place it in the try catch block..

please send

Former Member
0 Kudos

Hi Anup,

Broadly there are two types of Exceptions in JAVA, Checked and Unchecked exception. We can have a Try....Catch for a checked exception but we cant have that for an unchecked exception. NULLPOINTEREXCEPTION is an unchecked exception.

Check following links for more clarity.

http://www.javaworld.com/javaworld/jw-07-1998/jw-07-exceptions.html

http://www.javapractices.com/Topic129.cjp

http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html

http://www.devx.com/Java/Article/29253

If you got your answer plz close this thread.

Regards,

Narinder Hartala

Answers (0)