You need to sign in to do that
Don't have an account?
SFDC16
How to solve System NullPointerException: Attempt to de-reference a null object
hello,
I am getting an error for below code
System.NullPointerException: Attempt to de-reference a null object
Account[] acc1=[Select id,name from account];
Case ca=new Case();
ca.Status='New';
ca.Origin='Phone';
ca.Account.name=acc1[0].name; // --------> error line
ca.Subject='hello';
insert ca;
system.debug('Account Name'+ca);
I am getting an error for below code
System.NullPointerException: Attempt to de-reference a null object
Account[] acc1=[Select id,name from account];
Case ca=new Case();
ca.Status='New';
ca.Origin='Phone';
ca.Account.name=acc1[0].name; // --------> error line
ca.Subject='hello';
insert ca;
system.debug('Account Name'+ca);
ca.AccountId=acc1[0].Id;
All Answers
ca.AccountId=acc1[0].Id;
Try by changing to
List<Account> acc1=[Select id,name from account];