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
Sailaja N 4Sailaja N 4 

Test class error: System.NullPointerException: Argument cannot be null.

Hi everypone,

Please help me on fixing this error in my test class

@isTest
private class ItemizedReceiptTest {
    static testMethod void testMethodPostive(){
        Product2 prod = new Product2(
            Name = 'Test',
            IsActive = true
        );
       
        insert prod;
       
        Account acc = new Account(Name ='Test');
        insert acc;
       
       
        //Create your product
        Opportunity testOpp = new Opportunity();
        testOpp.Name='Test Opportunity' ;
        testOpp.AccountId=acc.id;
        testOpp.StageName='Order Placed';
        testOpp.CloseDate= Date.newInstance(2019,03,28);
        insert testOpp;
       
        Id pricebookId = Test.getStandardPricebookId();
       
       
        //Create your pricebook entry
        PricebookEntry pbEntry = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = prod.Id,
            UnitPrice = 100.00,
            IsActive = true
           
        );
        insert pbEntry;
        //create your opportunity line item.  This assumes you already have an opportunity created, called opp
        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId = testOpp.Id,
            Quantity = 5,
            PricebookEntryId = pbEntry.Id,
            TotalPrice = pbEntry.UnitPrice
        );
        insert oli;

        PageReference pageRef = Page.ItemizedReceipt;
        Test.setCurrentPage(pageRef);
        system.debug('Products'+prod);
           system.debug('acc'+acc);
         system.debug('testOpp'+testOpp);
         system.debug('pbEntry'+pbEntry);
         system.debug('oli'+oli);
        ApexPages.StandardController sc = new ApexPages.StandardController(testOpp);
        ItemizedReceipt obj = new ItemizedReceipt(sc);
        obj.total();

 }
   
   
}


class:


public with sharing class ItemizedReceipt {
    public List<OpportunityLineItem> oppLst          { set; get; } //List of Products in a Opportunity
    public Double                    totalPrice_cal  { set; get; } //Variable to calculate Total Price of all the products
    public Double                    totalGST_cal    { set; get; } //Variable to calculate GST of all the products
    public Double                    totalPST_cal    { set; get; } //Variable to calculate PST of all the products
    public Double                    totalAmt_cal    { set; get; } //Variable to calculate Total Amount of all the products
    public DateTime                  getCloseDate    { set; get; } //Variable to get the Created Date
    public String                    dayOfWeek       { set; get; } //Variable to store the Day from the Created Date
    public String                    accName         { set; get; } //Variable to store the Customer Name
    public  ItemizedReceipt(ApexPages.StandardController controller) {
        if ( controller.getId() != null ) {
            oppLst = [SELECT Product2.Name, Opportunity.Id, Opportunity.CloseDate, TotalPrice, GST__c, PST__c,
                             Total_w_Tax__c, Opportunity.Account.Name
                      FROM   OpportunityLineItem
                      WHERE  Opportunity.Id=:controller.getId()
                     ];
            getCloseDate = oppLst[0].Opportunity.CloseDate;
            dayOfWeek = getCloseDate.format('EEEE');
            accName = (string)oppLst[0].Opportunity.Account.Name;
            totalPrice_cal = 0.0;
            totalGST_cal = 0.0;
            totalPST_cal = 0.0;
            totalAmt_cal = 0.0;
            total();
        }
    }  
    //Method calculates all the totals
    public  void total() {
        for ( integer i = 0; i < oppLst.size(); i++ ) {
            totalPrice_cal += oppLst[i].TotalPrice;
            totalGST_cal   += oppLst[i].GST__c;
            totalPST_cal   += oppLst[i].PST__c;
            totalAmt_cal   += oppLst[i].Total_w_Tax__c;
        }
    }
}

Thank You
Madhukar_HeptarcMadhukar_Heptarc
Dear Sailaja,

I have understand your question. 
 
Test Class :
========
@isTest
public class CommunityTestCls_ItemizedReceipt {

    static testMethod void testMethodPostive(){
        Product2 prod = new Product2(
            Name = 'Test',
            IsActive = true
        );
       
        insert prod;
       
        Account acc = new Account(Name ='Test');
        insert acc;
       
       
        //Create your product
        Opportunity testOpp = new Opportunity();
        testOpp.Name='Test Opportunity' ;
        testOpp.AccountId=acc.id;
        testOpp.StageName='Order Placed';
        testOpp.CloseDate= Date.newInstance(2019,03,28);
        insert testOpp;

        system.debug('testOpp'+testOpp);
        
        Id pricebookId = Test.getStandardPricebookId();
       
       
        //Create your pricebook entry
        PricebookEntry pbEntry = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = prod.Id,
            UnitPrice = 100.00,
            IsActive = true
           
        );
        insert pbEntry;
        //create your opportunity line item.  This assumes you already have an opportunity created, called opp
        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId = testOpp.Id,
            Quantity = 5,
            PricebookEntryId = pbEntry.Id,
            TotalPrice = pbEntry.UnitPrice,
            GST__c = pbEntry.UnitPrice,  // You need to insert the GST field 
            PST__c = pbEntry.UnitPrice,  // You need to insert the GST field 
            Total_w_Tax__c = pbEntry.UnitPrice // You need to insert the GST field 
        );
        insert oli;

        PageReference pageRef = Page.ItemizedReceipt;
        Test.setCurrentPage(pageRef);
        system.debug('Products'+prod);
        system.debug('acc'+acc);
        system.debug('testOpp'+testOpp);
        system.debug('pbEntry'+pbEntry);
        system.debug('oli'+oli);
        ApexPages.StandardController sc = new ApexPages.StandardController(testOpp);
        system.debug('SC :'+sc + '/'+'testOpp :'+testOpp );
        ItemizedReceipt obj = new ItemizedReceipt(sc);
        obj.total();

 }
}
 
==============================================================
Apex Class :
=========
public with sharing class ItemizedReceipt {
    public List<OpportunityLineItem> oppLst          { set; get; } //List of Products in a Opportunity
    public Double                    totalPrice_cal  { set; get; } //Variable to calculate Total Price of all the products
    public Double                    totalGST_cal    { set; get; } //Variable to calculate GST of all the products
    public Double                    totalPST_cal    { set; get; } //Variable to calculate PST of all the products
    public Double                    totalAmt_cal    { set; get; } //Variable to calculate Total Amount of all the products
    public DateTime                  getCloseDate    { set; get; } //Variable to get the Created Date
    public String                    dayOfWeek       { set; get; } //Variable to store the Day from the Created Date
    public String                    accName         { set; get; } //Variable to store the Customer Name
    public  ItemizedReceipt(ApexPages.StandardController controller) {
        if ( controller.getId() != null ) {
            oppLst = [SELECT Product2.Name, Opportunity.Id, Opportunity.CloseDate, TotalPrice, GST__c, PST__c,
                             Total_w_Tax__c, Opportunity.Account.Name
                      FROM   OpportunityLineItem
                      WHERE  Opportunity.Id=:controller.getId()
                     ];
            system.debug('oppLst -'+oppLst + '/' +controller );
            getCloseDate = oppLst[0].Opportunity.CloseDate;
            dayOfWeek = getCloseDate.format('EEEE');
            accName = (string)oppLst[0].Opportunity.Account.Name;
            totalPrice_cal = 0.0;
            totalGST_cal = 0.0;
            totalPST_cal = 0.0;
            totalAmt_cal = 0.0;
            total();
        }
    }  
    //Method calculates all the totals
    public  void total() {
        system.debug('oppLst : '+oppLst[0].GST__c +'/' +oppLst);
        for ( integer i = 0; i < oppLst.size(); i++ ) {
            totalPrice_cal += oppLst[i].TotalPrice;
            totalGST_cal   += oppLst[i].GST__c;   // You are not inserted GST field data from test class that will return null that is the reason it is given argument null. 
            totalPST_cal   += oppLst[i].PST__c;  // You are not inserted PST field data from test class that will return null that is the reason it is given argument null.
            totalAmt_cal   += oppLst[i].Total_w_Tax__c; // You are not inserted Total_w_Tax__c field data from test class that will return null that is the reason it is given argument null.
        }
    }
}

You are not inserted GST/PST/Total_W_Tax field data from test class that will return null that is the reason it is given argument null.
I have excecuted test class and apex class in my org. it run successfully and also added comments in the code have a look at that.

Please let us know if it helps you!

Thanks & regards
Madhukar_Heptarc
Sailaja N 4Sailaja N 4
Thank You for the reply
But GST ,PST values are calculated in workflow,
Total_W_Tax field is a Formula field.
if i am commenting GST ,PST values class is running succesfully
Britto Fernandez 2Britto Fernandez 2
Dear Sailaja,

It seems like you dont have to update GST and PST, since these fields will be updated from workflow. 

What is condition to invoke workflow ?

Is this resolved ?