Salesforce Test Class - System NullPointerException Attempt to de-reference a null object

0 votes

Salesforce Test Class - Facing - System.NullPointerException: Attempt to de-reference a null object Error while trying to Run Test Class

APEX class

public class NewAccountCreation {
public Account account{get;set;}
public void save(){
Account acc = new Account();
// User enter values in vf page and we are capturing and creating account
acc.name = account.Name;
//acc.address = account.adress;
Insert acc;
}
}

Test Class

@isTest
public class TestNewAccountCreation {
    @isTest static void TestNewAccountCreationMethod(){
        NewAccountCreation testAccount = new NewAccountCreation ();
        Account acc = new Account(Name='TestAcct');
        Insert acc;
        system.debug(''+acc);
        testAccount.save();
        System.assert([Select Id From Account].size()==1);
    }
}

Error: System.NullPointerException: Attempt to de-reference a null object

StackTrace: Class.NewAccountCreation.save: line 6, column 1 Class.TestNewAccountCreation.TestNewAccountCreationMethod: line 9, column 1


Mar 2 in SalesForce by surbhi
• 3,810 points
844 views

1 answer to this question.

0 votes
Because testAccount.account is never set, it is null. This variable must be filled, but you do not need to create and enter an Account in your test code. In fact, doing so will result in the failure of your statement.
answered Mar 2 by surbhi
• 3,810 points

Related Questions In SalesForce

0 votes
0 answers

Salesforce Test Class - System.NullPointerException: Attempt to de-reference a null object

Salesforce Test Class - Facing - System.NullPointerException: ...READ MORE

Mar 1 in SalesForce by surbhi
• 3,810 points
1,052 views
0 votes
0 answers

How to Write the Test class for Salesforce Apex Aura Enabled class?

stuck in here to write a test ...READ MORE

Mar 15 in SalesForce by surbhi
• 3,810 points
566 views
0 votes
0 answers

Salesforce API: How to identify a Case from an email reference code ("[Ref: ... :Ref]")?

1 I'm writing a Windows service that will ...READ MORE

Apr 5 in SalesForce by surbhi
• 3,810 points
222 views
0 votes
0 answers

How to take test.salesforce.com while deployment as salesforce login url for api connection logic app?

In logic app i'm using salesforce connector after deployment ...READ MORE

Mar 1 in SalesForce by surbhi
• 3,810 points
397 views
+2 votes
2 answers

Salesforce Interview questions

Here are some questions very important for ...READ MORE

answered Jan 11, 2019 in Career Counselling by Suresh
• 720 points
2,018 views
0 votes
1 answer

How to connect to salesforce from tableau?

Hi, follow these steps to connect to Salesforce: 1. ...READ MORE

answered Mar 25, 2019 in Tableau by Cherukuri
• 33,050 points
359 views
0 votes
1 answer

Power BI - Salesforce

Hi, Follow below steps: 1. Go to Data source. 2. ...READ MORE

answered Mar 25, 2019 in Power BI by Cherukuri
• 33,050 points
238 views
0 votes
1 answer

What is the best training for Salesforce ADM-201 Exam?

Hi @Vardhan, I took Edureka's Salesforce Training that covers all ...READ MORE

answered Jun 3, 2021 in Others by Jaya
• 140 points

edited Dec 22, 2021 by Soumya 262 views
0 votes
1 answer

How to take test.salesforce.com while deployment as salesforce login url for api connection logic app?

you are not passing the parameterValues, use ...READ MORE

answered Mar 2 in SalesForce by surbhi
• 3,810 points
304 views
0 votes
1 answer

Json response to be deserialized in Apex salesforce lightning

Because some fields in Apex are reserved ...READ MORE

answered Mar 2 in SalesForce by surbhi
• 3,810 points
1,744 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP