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
Sarada RVS 2Sarada RVS 2 

Critical Issue : System.NullPointerException: Attempt to de-reference a null object

Hi,

I am unable to deploy my code due to null pointer exception.
Could anyone help me please.
Below is the my controller and test class.

Controller wit the method iam testing

public PageReference saveTOLead()
    {
        showrefreshblck = true;
        shouldRedirect = true;
     
        Lead acc = new Lead(id=a.id);
        
        
        redirectUrl = controller.view().getUrl();
        
        getRatings(acc.Id,allCompDetails[selectedrow].companyId);
        
        
        acc.Company_Registration_Number__c = allCompDetails[selectedrow].RegistrationNumber;
        acc.Company =  allCompDetails[selectedrow].Company;
        acc.is_saved_prev__c = true;
        acc.Company_Id__c = allCompDetails[selectedrow].companyId;
        
        List<String> addrArray = allCompDetails[selectedrow].Address.split(',');
        Map<Integer,String> accMap = new Map<Integer,String>();
        Integer n= addrArray.size();
        for(Integer l=0;l<addrArray.size();l++)
        {
            accMap.put(l, addrArray[l]);
        }
        
        acc.Street = accMap.get(0);  
        
        acc.City =  accMap.get(1);
        acc.State = accMap.get(2); 
        
        acc.PostalCode = allCompDetails[selectedrow].postalCode;
        acc.Company_Status__c =    allCompDetails[selectedrow].status;
        acc.CompanyType__c =allCompDetails[selectedrow].type;
        
     
        
        showPgblck = false;
       return null;
      
    }
    
Test class witht the piece which testes the method

 public static testMethod void testLeadRatings() {
        Test.setMock(HttpCalloutMock.class, new MockHttpResGenFetchLeadDetailsRatings());
        Lead l = new Lead();
        l.FirstName = 'FirstName1';
        l.LastName = 'LastName1';
        l.MobilePhone = '074434567890';
        l.Company = 'Traves1';
       
        insert l;

        test.startTest();

        PageReference pageRef = Page.Lead_DisplayCompany; //FC_VF
        Test.setCurrentPage(pageRef);                    
        pageRef.getParameters().put('id',l.id);
        ApexPages.StandardController sc = new ApexPages.StandardController(l);

        LeadFetchCompanyDetails fc = new LeadFetchCompanyDetails(sc);
        LeadFetchCompanyDetails.CompanyDetails cd = new LeadFetchCompanyDetails.CompanyDetails();
        cd.companyid = 'GB003/0/07375960';
        cd.RegistrationNumber='07375960';
        cd.Address = 'Test.test,test';                
        cd.Company = 'Traves';
        cd.postalCode ='HA02RX';
        cd.Status ='Active';
        cd.Type= 'Ltd';

 
        fc.allCompDetails.add(cd);
        // fc.selectedrow = 0;

        system.assert(fc!=null);
        fc.saveTOLead();
        
        LeadFetchRatingDetails.CompanyDetails lfrd = new LeadFetchRatingDetails.CompanyDetails ();
        LeadFetchRatingDetails.contactWrapper cw = new LeadFetchRatingDetails.contactWrapper();
        test.stopTest();
    }
    
   
Glyn Anderson 3Glyn Anderson 3
Sarada,  You haven't posted all of the relevant code.  For example, in "saveTOLead()", on the line "Lead acc = new Lead(id=a.id);", the variable "a" is not defined.  You have left out the code where "a" is defined.  Also, the problem might be in the class, "LeadFetchCompanyDetails", which you did not provide.  An easy thing to check is to look for code that accesses the id parameter using "getParameter()".  If anyone is looking for is as "Id" or "ID", it will not match "id", and you'll get a null value.  Assuming that's not the problem, you'll need to post the entire controller class, the LeadFetchCompanyDetails class and any other relevant code.  UNLESS, you know the class and line number where the NullPointerException is thrown.  That would be good to know.
Sarada RVS 2Sarada RVS 2
Thank you for your reply Glyn , iam unable to post my complete code in this.could you please share me your email.
Glyn Anderson 3Glyn Anderson 3
Sorry, I would trust you with it, but I don't think I should post my email address in a public forum.

Does the null pointer exception only occur when running the test code?  If so, you could run the test manually (from the Apex Test Execution page) and you should be able to get the class name and line number where the exception was thrown - as well as the whole call stack.  Could you do that and post the results?