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, 2022 in SalesForce by surbhi
• 3,820 points
2,885 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.

I hope this helps!

Enroll for the salesforce course online here to learn more!

answered Mar 2, 2022 by surbhi
• 3,820 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, 2022 in SalesForce by surbhi
• 3,820 points
2,083 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, 2022 in SalesForce by surbhi
• 3,820 points
2,059 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, 2022 in SalesForce by surbhi
• 3,820 points
801 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, 2022 in SalesForce by surbhi
• 3,820 points
1,064 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
3,400 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,030 points
1,043 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,030 points
758 views
0 votes
2 answers

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

Hi @Vardhan, I took Edureka's Salesforce Online Training, ...READ MORE

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

edited Dec 22, 2021 by Soumya 729 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, 2022 in SalesForce by surbhi
• 3,820 points
987 views
0 votes
1 answer

Json response to be deserialized in Apex salesforce lightning

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

answered Mar 2, 2022 in SalesForce by surbhi
• 3,820 points
3,563 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