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
AmarjeetAmarjeet 

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

Apex Test Result Detail 
Time Started1/2/2018 12:15 AM
Classaccsearchcontrollertest
Method NameTestclear
Pass/FailFail
Error MessageSystem.NullPointerException: Attempt to de-reference a null object
Stack TraceClass.accsearchController.clear: line 25, column 1
Class.accsearchcontrollertest.Testclear: line 49, column 1


Apex Test Class:-

@isTest 
public class accsearchcontrollertest
{
    @testSetup static void createTestData()
      {

        Account acc= new Account();
        acc.Name='TestAccount';
        acc.Is_Acct_IP_or_RP__c='IP';
        acc.Buying_Groups__c='N/A (none)';
        insert acc;

        Opportunity op = new Opportunity ();
        op.Name = 'Test' ;
        op.Accountid= acc.Id;
        op.StageName='A = Market Participant';
        op.CloseDate= System.today() + 5;
        insert op;

        Quote qt = new Quote();
        qt.Name = 'Test123';
        qt.CBG_Employee_Responsible__c = UserInfo.getUserId();
        qt.CBG_Incoterms__c = 'CFR';
        qt.CBG_PaymentTerms__c = 'CO04 payable immediatly';
        qt.Format__c = 'List';
        qt.CBG_SAPQuoteNumber__c = '30010012';
        qt.OpportunityId=op.id; 
        insert qt;

      }
      
    static TestMethod void TestprocessLinkClick()
      {
      
        PageReference pref1 = Page.CBG_SearchQuote ;   
        Test.setCurrentPage(pref1);   
        accsearchcontroller Obj = new accsearchcontroller();
        Obj.searchstring = '30010012';  
        //Obj.clear();
        Obj.processLinkClick();
        
      }   
    static TestMethod void Testclear()//////////////////////////////////This one failing---->How to pass value to search string
      {
      
        
        accsearchcontroller Obj = new accsearchcontroller();
        Obj.searchstring = '30010012';  
        Obj.clear();  
             
     }  

   static TestMethod void Testsearch()
      {
      
        accsearchcontroller Obj = new accsearchcontroller();
        Obj.search();
        
      }  
    
}


Apex Class:-

public with sharing class accsearchController 
{

    public accsearchcontroller(ApexPages.StandardController controller) {

    }


public String l { get; set; }
public list <quote> acc {get;set;}
public string searchstring {get;set;}
public String quoteid{get; set;}
public Boolean search{get; set;}
public Boolean result{get; set;}
public accsearchcontroller( ) 
{
}
public void search(){
string searchquery='select QuoteNumber,Name,Email,Opportunity.Name,CBG_SAPQuoteNumber__c ,CBG_Phoenix_Offer_Number__c from quote where CBG_SAPQuoteNumber__c like \'%'+searchstring+'%\' OR CBG_SAPQuoteNumber__c like \'*'+searchstring+'*\' OR  CBG_Phoenix_Offer_Number__c like \'%'+searchstring+'%\' OR  CBG_Phoenix_Offer_Number__c  like \'*'+searchstring+'*\' Limit 20';
acc= Database.query(searchquery);
}
public void clear(){
acc.clear();

}
public PageReference processLinkClick() {
         
        search = false;
        result = false;
        
        Pagereference p1 = new PageReference('https://cs84.salesforce.com/'+quoteid);
        p1.setRedirect(true);
        return p1;
    }
}
Best Answer chosen by Amarjeet
jigarshahjigarshah
Srikant,

Your test method Testclear() is failing because, the acc variable that is attempted to be cleared in the clear() method, is already null. Hence, you will either need to initialize the acc variable in your constructor or first call the search() method to initialize the acc variable with a value and then call the clear() method.

Refer the sample test code to do so.
static TestMethod void Testclear(){

	List<Quote> quotes = new List<Quote>();

	//Create test Quote records to be searched and should be retrieved in the acc param
	for(Integer quoteCnt = 0; quoteCnt < 200; quoteCnt++){
       
        //Include additional Quote fields
		quotes.add(new Quote());
	}
	
	//Set the search string
	accsearchcontroller Obj = new accsearchcontroller();
	Obj.searchstring = '30010012';
	
	Test.startTest();
	
	//Call the search() before clear() to initialize the acc variable with a value
	Obj.search();
    Obj.clear(); 

	Test.stopTest();
	
	//Assert if the retireved search results are cleared out
	System.assertEquals(0, Obj.acc.size());
}

Moreover, the methodname clear, is already a standard Sobject method (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm) and a part of the reserved keywords. It may not be directly affecting your code here, but its always a good idea to refrain from using any reserved keywords as method names.

Please mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.

All Answers

jigarshahjigarshah
Srikant,

Your test method Testclear() is failing because, the acc variable that is attempted to be cleared in the clear() method, is already null. Hence, you will either need to initialize the acc variable in your constructor or first call the search() method to initialize the acc variable with a value and then call the clear() method.

Refer the sample test code to do so.
static TestMethod void Testclear(){

	List<Quote> quotes = new List<Quote>();

	//Create test Quote records to be searched and should be retrieved in the acc param
	for(Integer quoteCnt = 0; quoteCnt < 200; quoteCnt++){
       
        //Include additional Quote fields
		quotes.add(new Quote());
	}
	
	//Set the search string
	accsearchcontroller Obj = new accsearchcontroller();
	Obj.searchstring = '30010012';
	
	Test.startTest();
	
	//Call the search() before clear() to initialize the acc variable with a value
	Obj.search();
    Obj.clear(); 

	Test.stopTest();
	
	//Assert if the retireved search results are cleared out
	System.assertEquals(0, Obj.acc.size());
}

Moreover, the methodname clear, is already a standard Sobject method (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm) and a part of the reserved keywords. It may not be directly affecting your code here, but its always a good idea to refrain from using any reserved keywords as method names.

Please mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
This was selected as the best answer
AmarjeetAmarjeet
Thanks a lot jigarshah!!!  I got it.