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
V AnandV Anand 

System.NullPointerException: Argument 1 cannot be null

I have created a apex controller in that I calculated

 

 

 List<PManagement__c PM=[select Rep__c,Revenue___c,Quantity___c,Month_Starting__c,Month_Ending__c from PManagement__c where id=:scon.getId()];
    }
   
public pageReference UpdatePerformance(){
   
   if(!PM.IsEmpty()){
      PManagement__c PMD= PM[0];
      list<sales__c> BS=[select SRep__c,Total_Products__c,Total_Sale_Val__c from SMB_Sales__c   where sale_date__c >=:PMD.Month_Starting__c and sale_date__c <=:PMD.Month_Ending__c AND SRep__c=:PMD.Rep__c ];
if(SMBS.size()!=0){
for(Sales__c BS1:BS){ TotalRev += BS1.Total_sale_Val__c; // I think problem is here TotalRev is incremented with null value.. TotalTar += BS1.Total_Products__c; } PMD.Revenue__c=TotalRev; PMD.Quantity__c=TotalTar; Update PMD; return null; }

 Now I am writing test case for above controller

ApexPages.StandardController stdcon=new ApexPages.StandardController(PM); 
     RevTarCon Con= new RevTarCon(stdcon);
       Con.UpdatePerformance(); // This generates error 
    }

 I Got System.NullPointerException: Argument 1 cannot be null , error in test class

How I can I solve this problem.. please help me ?

 

for(Sales__c BS1:BS){          
TotalRev += BS1.Total_sale_Val__c;
// I think problem is here TotalRev is incremented with null value.. TotalTar += BS1.Total_Products__c;
)
Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

TotalTar and TotalRev must both be initialized to zero in the constructor as already suggested and ...

 

TotalRev += (BS1.total_sale_val__c != null ? BS1.Total_sale_Val__c : 0);


All Answers

kiranmutturukiranmutturu

can you please post the test class code 

V AnandV Anand
@isTest
Class TestRevTarCon{
  Static testMethod void Method1(){
  Date mydate= Date.Today();
  pageReference PR=page.Calculate;
  Test.SetCurrentPage(PR);
   
Sales_R__c SR= new Sales_R__c(name='yyy',Surname__c='b'); 
insert SR;
  
PManagement__c PM=new PManagement__c(name='sample',Rep__c=SR.id,Month_Starting__c=mydate,Month_Ending__c=mydate);
insert PM;  
 
List<Sales__c> SM= new List<sales__c>();
SM.add(new SM_Sales__c(Rep__c=SR.id,NO_OF_PRODUCTS__c='1',Sale_Value__c=100,sale_date__c=mydate)); SM.add(new SM_Sales__c(Rep__c=SR.id,NO_OF_PRODUCTS__c='2',Sale_Value__c=50,sale_date__c=mydate)); insert SM; ApexPages.StandardController stdcon=new ApexPages.StandardController(PM); RevTarCon Con= new RevTarCon(stdcon); System.assert(con!=null); Con.UpdatePerformance(); // This line causing an error.
PManagement__c DUP=[select Revenue__c,Quantity__c,Performance_Calculated__c from PManagement__c where id=:PM.id]; System.assert(DUP.Revenue__c==200); System.assert(DUP.Quantity__c==3); } }

 This is my test class

SubhamSubham

Hi,

 

Try initializing in constructor.

TotalTar = 0; before you increment

 

Regards,

Subham

 

 

crop1645crop1645

TotalTar and TotalRev must both be initialized to zero in the constructor as already suggested and ...

 

TotalRev += (BS1.total_sale_val__c != null ? BS1.Total_sale_Val__c : 0);


This was selected as the best answer
V AnandV Anand

Thank you for your response....

 

I followed your suggestions there is no error but..... in test case assertion failed due to not updating of Revenue__c,Quantity__c.

System.assert(DUP.Revenue__c==200);
   System.assert(DUP.Quantity__c==3);


I have noticed that in debug log above valules are initalized with zeros. How to get success with assertions,.. my test case is avaible in previous post of this problem....

18:28:56.308 (308388000)|USER_DEBUG|[30]|DEBUG|REVENUE..........................0
18:28:56.308 (308454000)|USER_DEBUG|[31]|DEBUG|QUANTITY.........................0

 

crop1645crop1645

Your testmethod is inserting SM_Sales__c SObjects but your controller method is reading SMB_Sales__c objects 

 

As an aside, these issues are readily resolvable by using system.debug statements in your controller so you can see variable values