function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rornelasrornelas 

question about null pointer error

Hi Everyone,

I have this code below. Can anybody tell me why this code throws the Null Pointer exception?
 
try

{

List<String> nameList;

Account a;

String s = a.Name;

nameList.add( s );

}

catch ( ListException le )

{

System.debug('List Exception');

}

catch ( NullPointerException npe )

{

System.debug('NullPointer Exception');

}

catch ( Exception e )

{

System.debug('Generic Exception');

}

 
Best Answer chosen by rornelas
Raj VakatiRaj Vakati
Hi ,

Please find the code here .


try

{

List<String> nameList= new List<String>();

Account a= new Account(Name ='Test');

String s = a.Name;

nameList.add( s );

}

catch ( ListException le )

{

System.debug('List Exception');

}

catch ( NullPointerException npe )

{

System.debug('NullPointer Exception');

}

catch ( Exception e )

{

System.debug('Generic Exception');

}

All Answers

Raj VakatiRaj Vakati
Hi ,

Please find the code here .


try

{

List<String> nameList= new List<String>();

Account a= new Account(Name ='Test');

String s = a.Name;

nameList.add( s );

}

catch ( ListException le )

{

System.debug('List Exception');

}

catch ( NullPointerException npe )

{

System.debug('NullPointer Exception');

}

catch ( Exception e )

{

System.debug('Generic Exception');

}
This was selected as the best answer
Anurag SaxenaAnurag Saxena
Please check the null value before adding the value in string 

like:

If(a.id !=null )
{
String s = a.Name
}

Let me know if it helps

Thanks