0

i wrote unit test code and got 52% code coverage and struck with following error:

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

Please correct me where i wrong i am new tp apex and rest api.

@isTest
public class LeadCreateTest {

    static testmethod void teste(){

        Lead ll1= new Lead();
        ll1.FirstName ='Test';
        ll1.LastName='LeadLastName';
        ll1.CMS_ID__c='12345';
        ll1.Company='ABC';
        insert ll1;
        List<Addresses__c> addressToBeInsertedList = new List<Addresses__c>();
        fromJSON.cls_Addresses ll2 = new fromJSON.cls_Addresses();
        // ll2.Lead__c     = ll1.id;
        ll2.CMS_Id   = ll1.CMS_ID__c;
        ll2.city  = 'pune';
        Addresses__c address = new Addresses__c();
        address.CMS_ADDRESS_ID__c =   ll2.CMS_Id;
        address.Lead__c = ll1.Id;
       addressToBeInsertedList.add(address);
        insert address;
        RestRequest request = new RestRequest();
        request.requestUri ='/services/apexrest/something';
        request.httpMethod = 'POST';
        String myJSON = JSON.serializePretty(address);
        request.requestBody = Blob.valueof(myJSON);
        RestContext.request = request;
        String accObj = CreateLead.doPost();

    }

}

Here is doPost mehthod code:

@RestResource(urlMapping='/something/*')
global with sharing class CreateLead 
{  
    @HttpPost
    global static string doPost() 
    {
        String errorMsg = '';
        List<Addresses__c> addressToBeInsertedList = new List<Addresses__c>();
        RestRequest req = RestContext.request;
        Blob body = req.requestBody;
        String requestString = body.toString();     
        fromJSON request = fromJSON.parse(requestString);   

        Lead lead = new Lead();
        lead.FirstName = request.FirstName;
        lead.LastName = request.LastName;
        lead.State = 'New';
        lead.CMS_ID__c = request.CMS_ID;
        lead.Company = request.Company;
        lead.Email =  request.Email;    
        lead.Phone =  request.Phone;    
        lead.Status = request.Status;   
        lead.Company = request.Company; 
        lead.Salutation =  request.Salutation;
        lead.isStaff__c = String.isBlank(request.isStaff)?false:Boolean.valueOf(request.isStaff);   
        lead.Joined_Date__c = String.isBlank(request.Joined_Date)?null: Date.valueOf(request.Joined_Date);  
        lead.SAP_Code__c =  request.SAP_Code;   
        lead.isSuperuser__c =String.isBlank(request.isSuperuser)?false:Boolean.valueOf(request.isSuperuser);    
        try
        {
            insert lead;
        }Catch(Exception e)
        {
            errorMsg = e.getMessage();
        }

        if(request.Addresses.size() > 0  )
        {
            for(fromJSON.cls_Addresses add : request.Addresses)
            {
                Addresses__c address = new Addresses__c(); // custom object in saleforce
                address.CMS_ADDRESS_ID__c =  add.CMS_Id;    
                address.Street_1__c =  add.Street_1;    
                address.Street_2__c =  add.Street_2;    
                address.State__c =  add.State;  
                address.Country__c =  add.Country;  
                address.Postal_Code__c =  add.Postal_Code;
                address.City__c =  add.City ;
                address.Type__c =  add.Type;
                address.Lead__c = lead.Id;
                addressToBeInsertedList.add(address);
            }
        }
        try
        {
            insert addressToBeInsertedList;
        }Catch(Exception e)
        {
            ErrorMsg = e.getMessage();
        }
        Response resp = new Response();
        if(errorMsg == '')
        {
            resp.Status = 'Success';
            resp.Message = 'Lead and Address Inserted Successfully.';
        }
        else
        {
            resp.Status = 'Error';
            resp.Message = errorMsg;
        }
        return JSON.serialize(resp);
    }  
    public class Response
    {
        Public String Status;
        Public String Message;
    }
}
  • You need to pay attention to the class name and line number reported as part of the NullPointerException. That will point you to the exact line where the problem occurs which makes figuring out the problem a whole lot easier. – Keith C May 17 at 9:00
  • i got exception on last line ie.String accObj = CreateLead.doPost(); – Satish May 17 at 9:02
  • @Satish as keith said whats the line number where the exception is ? you also need to post code of doPost method, just the error message and some test class code wont be of much use – RedDevil May 17 at 9:04
  • for(fromJSON.cls_Addresses add : request.Addresses) {: from this line i got exception. – Satish May 17 at 9:10
  • The JSON that you are passing from test class is not in correct format. You will have to correct the JSON format. – Vijay Ganji May 17 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.