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
Siddhesh ChavanSiddhesh Chavan 

System.NullPointerException: Argument cannot be null in my test class

Stack Trace MassTransfferOwnershipController.doSearch: line 306, column 1
                   MassCaseTransferTestClass.MassCaseTransferTestClass: line 52, column 1

My Controllers method

 public PageReference doSearch()
    { 
       
        theResultList = new List<wrapperClass>();
        
        //system.debug('IN THE LOOP'+toUserFunc);
        
        if(selectedCaseFiler <> 'Select from list...' && selectedCaseFiler1 == 'Select from list...'&&  selectedCaseFiler2 == 'Select from list...')
        {
            CaseFiler = String.escapeSingleQuotes(selectedCaseFiler);
            CaseFilter = String.escapeSingleQuotes(selectedFilterValue);
            theQ = 'SELECT id,OwnerId,CaseNumber,Origin,Priority,Status,Account.Name,New_Owner__c  FROM Case WHERE OwnerId = :myId AND '+CaseFiler+' = :CaseFilter';
            
        }
        else if(selectedCaseFiler <> 'Select from list...' && selectedCaseFiler1 <> 'Select from list...' &&  selectedCaseFiler2 == 'Select from list...')
        {
              CaseFilerFirst = String.escapeSingleQuotes(selectedCaseFiler);
              CaseFilterFirst = String.escapeSingleQuotes(selectedFilterValue);
              CaseFilerFirst1 = String.escapeSingleQuotes(selectedCaseFiler1);
              CaseFilterFirst1 = String.escapeSingleQuotes(selectedFilterValue1);
             theQ = 'SELECT id,OwnerId,CaseNumber,Origin,Priority,Status,Account.Name,New_Owner__c FROM Case WHERE OwnerId = :myId AND '+CaseFilerFirst+' = :CaseFilterFirst AND '+CaseFilerFirst1+' = :CaseFilterFirst1 ';
            system.debug('IF LOOP 2 '+theQ);
        }
        else if(selectedCaseFiler <> 'Select from list...' && selectedCaseFiler1 <> 'Select from list...' &&  selectedCaseFiler2 <> 'Select from list...')
        {
            CaseFilerSecond = String.escapeSingleQuotes(selectedCaseFiler );
            CaseFilterSecond = String.escapeSingleQuotes(selectedFilterValue);
            CaseFilerSecond1 = String.escapeSingleQuotes(selectedCaseFiler1 );
            CaseFilterSecond1 = String.escapeSingleQuotes(selectedFilterValue1);
            CaseFilerSecond2 = String.escapeSingleQuotes(selectedCaseFiler2 );
            CaseFilterSecond2 = String.escapeSingleQuotes(selectedFilterValue2);
              
            theQ = 'SELECT id,OwnerId,CaseNumber,Origin,Priority,Status,Account.Name,New_Owner__c FROM Case WHERE OwnerId = :myId AND '+ CaseFilerSecond +' = :CaseFilterSecond AND '+ CaseFilerSecond1+' = :CaseFilterSecond1 AND '+CaseFilerSecond2+' = :CaseFilterSecond2 ';           
           system.debug('IF LOOP 3 '+theQ);
        }
        else
        {
        
            theQ = 'SELECT id,OwnerId,CaseNumber,Origin,Priority,Status,Account.Name,New_Owner__c FROM Case WHERE OwnerId = '+myId;
        }
        /*
        else
        {
            theQ = 'SELECT id,OwnerId FROM '+selectedObject+' WHERE OwnerId = :myId AND Stage = \''+selectedOpportunityFiler+'\'';
        }
        */
        
        system.debug('THE QUERY IS:-'+theQ);
        for(Case s : Database.Query(theQ))
        {
            system.debug('THE S:-'+s);
            wrapperClass wc = new wrapperClass(s);
            system.debug('THE WC:-'+wc);
            theResultList.add(wc);
        }
        system.debug('THE LIST IS:-'+theResultList);
        if(theResultList.isEmpty())
        {
             ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'NO RECORDS FOUND.'));
             showResult = FALSE;
        }
        showResult = TRUE;
        
        
        return null;
    }

My Test Class

@isTest(SeeAllData=true)
private class MassCaseTransferTestClass
{
    static testMethod void MassCaseTransferTestClass()
    {
        Test.setCurrentPage(Page.MassTransfferOwnershipPage);

       List <User> user= [select id from User where Profile.Name = 'System Administrator'];
        
        String userId;   
        for(User usr: user)
        {
            userId = usr.id;
         }  
        
        
        Case c = new Case(OwnerId=userId,Origin='Phone',Subject='Test Case 1');
        insert c;
        
       
        
        Transffer_Ownership__c testObj = new Transffer_Ownership__c(User__c=userId);
        insert testObj; 
        
       
        ApexPages.Standardcontroller sc = New ApexPages.StandardController(testObj);
        MassTransfferOwnershipController mct = new MassTransfferOwnershipController(sc);    
        
        ApexPages.Standardcontroller sc1 = New ApexPages.StandardController(c);
        MassTransfferOwnershipController mct1 = new MassTransfferOwnershipController(sc1);    
        
        
        
        mct1.selectedCaseFiler = 'Origin';
        mct1.selectedCaseFiler1 = 'Origin';
        mct1.selectedCaseFiler2 = 'Origin';
       
        
        mct.getObjectsList(); //Covered
        mct.getblockChoices(); //Covered
        mct.getCaseFilersList(); //Covered
        mct1.getFiltersList(); //Covered
        
        mct.userPopulate();
        mct.touserPopulate();
        
        
        mct1.getOpportunityFilersList();
       
        mct1.getFiltersList1();
        mct1.getFiltersList2();
        mct1.doSearch();
        mct1.doTransfer(); 
        mct1.doTransfer1(); 
        mct1.finalMethod(); 
        
    }
    
    static testMethod void MassCaseTransferTestClass_2()
    {
    
      List<User> user = [select id from User where Profile.Name = 'System Administrator'];
      
      String userId ;
      for(User usr: user)
        {
            userId  = usr.id;
        }
        
      Case c2 = new Case(OwnerId=userId ,Priority='High',Subject='Test Case 2');
      insert c2;
      
      Transffer_Ownership__c testObj = new Transffer_Ownership__c(User__c=userId);
      insert testObj;
        
        
       
        ApexPages.Standardcontroller sc = New ApexPages.StandardController(testObj);
        MassTransfferOwnershipController mct = new MassTransfferOwnershipController(sc);    
      
      ApexPages.Standardcontroller sc2 = New ApexPages.StandardController(c2);
      MassTransfferOwnershipController mct2 = new MassTransfferOwnershipController(sc2);   

      
 
      mct2.selectedCaseFiler = 'Priority';
      mct2.selectedCaseFiler1 = 'Priority';
      mct2.selectedCaseFiler2 = 'Priority'; 
      
      
       mct.getObjectsList(); //Covered
        mct.getblockChoices(); //Covered
        mct.getCaseFilersList(); //Covered
        mct2.getFiltersList(); //Covered
        
        mct.userPopulate();
        mct.touserPopulate();
        
        
        mct2.getOpportunityFilersList();
       
        
        mct2.getFiltersList1();
        mct2.getFiltersList2();
        mct2.doSearch();
        mct2.doTransfer();
   
       
    }
    
    static testMethod void MassCaseTransferTestClass_3()
    {
    
      List<User> user = [select id from User where Profile.Name = 'System Administrator'];
      
      String userId ;
      
       for(User usr: user)
        {
            userId = usr.id;
        }
      
          
      Case c3 = new Case(OwnerId=userId ,Status='Open',Subject='Test Case 2');
      insert c3;
      
      Transffer_Ownership__c testObj = new Transffer_Ownership__c(User__c=userId);
      insert testObj;
        
        
       
        ApexPages.Standardcontroller sc = New ApexPages.StandardController(testObj);
        MassTransfferOwnershipController mct = new MassTransfferOwnershipController(sc);    
      
      ApexPages.Standardcontroller sc3 = New ApexPages.StandardController(c3);
      MassTransfferOwnershipController mct3 = new MassTransfferOwnershipController(sc3);   

      
 
      mct3.selectedCaseFiler = 'Status';
      mct3.selectedCaseFiler1 = 'Status';
      mct3.selectedCaseFiler2 = 'Status'; 
      
      
       mct.getObjectsList(); //Covered
        mct.getblockChoices(); //Covered
        mct.getCaseFilersList(); //Covered
        mct3.getFiltersList(); //Covered
        
        mct.userPopulate();
        mct.touserPopulate();
        
        
        mct3.getOpportunityFilersList();
       
        
        mct3.getFiltersList1();
        mct3.getFiltersList2();
        mct3.doSearch();
        mct3.doTransfer();
   
    }

}
Carolina Ruiz MedinaCarolina Ruiz Medina
Hi Siddhesh Chavan,

Could you please let us know which line is the one 306 please?  ( not quite sure exactly in which pointo of the method doSearch is failing)

however looking at the method,
could be that the variable theQ is null when you try to do Database.Query(null) and therefore you could get System.NullPointerException: Argument 1 cannot be null, if variable theQ hasn't got a value ( because never enter in the if-else cases and was not initialised on the constructor ) then this could be the error. 
Another thing that cross my mind is that could be the case that when you create your wrapperClass, in the constructor you are requesting a field that the case doesnt have? therefore if the field is null and you are tryting to do something with it is giving you null pointer? 
Maybe also some of the logic on the controller is making some of the values that you are using in the method null?

Hope it helps.

Cheers,
Carolina .
Cyrus TalladenCyrus Talladen
In your code the stacktrace you have copy-pasted doesn't show which method call has a null pointer argument.  It could be that you have not properly called an object in your class that was expecting one or more arguments. or that an instantiation did not have a field explicitly specified. 

Cyrus Talladen
CRM Engineer
www.levementum.com