+ Start a Discussion
rajubalajirajubalaji 

while run test class getting these error "System.NullPointerException: Attempt to de-reference a null object"

Hi Everyone,

if anyone find error in my apex class and please guide me.Actually i was new to write a programs.thanks you very much for helping me.

Apex class:

@RestResource(urlMapping='/DeleteInfo/*')
global class DeleteInfo {
@HttpPost
    global static String doPost() {
        String responseJSON;
        if (RestContext.request.requestBody != null) {
            String requestStatus;
            String reqBody = RestContext.request.requestBody.toString();
            String ParentId = null;
            Request Request = new Request();
            String environmentCode = Utility.getEnvironmentCode(UserInfo.getUserName());
            
            try {
                Req req = (Req)JSON.deserialize(reqBody, DeleteInfo.Req.class);
                Request = req.Request;
                
                if (Request.Parent != null) {
                    if (Request.Parent.StarID != null && Request.Parent.StarID != '') {
                        String ParentStarID = Request.Parent.StarID + '__' + environmentCode;
                        List<Hub_Parent__c> ptList = [select Id from Hub_Parent__c where StarID__c = :ParentStarID Limit 1];
                        
                        if (ptList.size() > 0) {
                            Hub_Parent__c objParent = ptList[0];
                            ParentId = objParent.Id;
                            
                            ParentDataDelete.DataDelete(objParent.Id);
                            
                            //List<ParentPrescription__c> lstParentPrescription = [select Id, Status__c from ParentPrescription__c Where Parents__c = :ParentId order by RxExpirationDate__c desc NULLS LAST, LastModifiedDate Desc limit 1];
                            
                            /*if (objParent.Prescriptions__r.size() > 0) {
                                ParentPrescription__c objParentPrescription = objParent.Prescriptions__r[0];
                                if (Request.Prescription.Status != null && (Request.Prescription.Status == 1 || Request.Prescription.Status == 2)) {
                                    String prescriptionStatus = Request.Prescription.Status == 1 ? 'Active' : 'Inactive';
                                    if (prescriptionStatus != objParentPrescription.Status__c) {
                                        objParentPrescription.Status__c = prescriptionStatus;
                                        update objParentPrescription;
                                    }
                                    responseJSON = InboundTransactionResponse.GetSucessResponse(Request.Header.TransactionType, Request.Header.TimeStamp, Request.Header.FileID, Request.Prescription.StarID);
                                    requestStatus = 'Success';
                                }
                                else {
                                    responseJSON = InboundTransactionResponse.GetErrorResponse(Request.Header.TransactionType, Request.Header.TimeStamp, Request.Header.FileID, Request.Prescription.StarID, 'Status', '0056', 'Invalid Prescription Status');
                                    requestStatus = 'Failure';
                                }
                            }
                            else {
                                responseJSON = InboundTransactionResponse.GetErrorResponse(Request.Header.TransactionType, Request.Header.TimeStamp, Request.Header.FileID, Request.Prescription.StarID, 'Prescription', '0056', 'Prescription doesn\'t exists in system for the Parent');
                                requestStatus = 'Failure';
                            }*/
                            responseJSON = InboundTransactionResponse.GetSucessResponse(Request.Header.TransactionType, Request.Header.TimeStamp, Request.Header.FileID, Request.Parent.StarID);
                            requestStatus = 'Success';
                        }
                        else {
                            responseJSON = InboundTransactionResponse.GetErrorResponse(Request.Header.TransactionType, Request.Header.TimeStamp, Request.Header.FileID, Request.Parent.StarID, 'StarID', '0056', 'StarID doesn\'t exists in system');
                            requestStatus = 'Failure';
                        }
                    }
                    else {
                        responseJSON = InboundTransactionResponse.GetErrorResponse(Request.Header.TransactionType, Request.Header.TimeStamp, Request.Header.FileID, null, 'StarID', '001', 'StarID is a required field.');
                        requestStatus = 'Failure';
                    }
                }
                else {
                    responseJSON = InboundTransactionResponse.GetErrorResponse(Request.Header.TransactionType, Request.Header.TimeStamp, Request.Header.FileID, null, 'Parent', '001', 'Invalid request format.');
                    requestStatus = 'Failure';
                }
                BlueStarTransactionDetails.CreateInboundTransactionRequest(Request.Header.TransactionType, Request.Header.FileID, requestStatus, reqBody, responseJSON, null, null, null, environmentCode);
            }
            catch (Exception ex) {
                System.debug('Delete Exception: ' + ex.getMessage());
                String fileId;
                if (Request.Header != null && Request.Header.FileID != null) {
                    fileId = Request.Header.FileID;
                }
                else {
                    fileId = 'Unknown';
                }
                BlueStarTransactionDetails.CreateInboundTransactionRequest(Request.Header.TransactionType, fileId, 'Exception', reqBody, ex.getMessage(), null, null, null, environmentCode);
                if(Request != null && Request.Header != null){
                    responseJSON = InboundTransactionResponse.GetErrorResponse(Request.Header.TransactionType, Request.Header.TimeStamp, Request.Header.FileID, null, 'Error', '0056', ex.getMessage());
                }else{
                    responseJSON = ex.getMessage();
                }
            }
        }
        return responseJSON;
    }
    
    public class Parent {
        public String StarID {get;set;}
    }
    
    public class Header {
        public String TransactionType {get;set;}
        public String TimeStamp {get;set;}
        public String FileID {get;set;}
    }
    
    public class Request {
        public Parent Parent {get;set;}
        public Header Header {get;set;}
    }
    
    public class Req {
        public Request Request {get;set;}
    }
}


Test Class for above Apex class:

@isTest
public class DeleteInfo_Test {
    static testMethod void TestDeleteInfo(){
        
        Test.startTest();
        HubTestController.createCustomSettings();
        //User usr = [Select Id from User where UserName = 'bluestar@welldocinc.com.sfdev4' Limit 1];
        List<User> usr = [Select Id,UserName from User where Name = 'bluestar bluestar' Limit 1];
        system.runAs(usr[0]){
            HubTestController.createEnviornmentSettingsWithUserName(usr[0].UserName);
            Hub_parent__c parent = new Hub_parent__c();
            parent.GivenName__c = 'Mangesh';
            parent.SampleCode__c = 'SCA1556329';
            parent.FamilyName__c = 'K';
            parent.Status__c = 'SRF Incomplete';
            parent.StarID__c ='StarID__bst';
            parent.FamilyName__c = 'K';
            parent.DOB__c = System.today();
            parent.Gender__c = 'M';
            
            insert parent;  
            RestRequest req = new RestRequest();
            RestResponse res = new RestResponse();
            
            String str ='{"Request":{"Header":{"TransactionType":"DeleteInfo","TimeStamp":"03-15-2016 12:09:15:252","FileID":"DeleteInfo.03152016120915252"},"parent":{"StarID":"StarID","Status":"Pending"}}}';
            req.requestURI = '/DeleteInfo/*';  
            req.httpMethod = 'Post';
            RestContext.request = req;
            RestContext.request.requestBody = Blob.valueOf(str);
            RestContext.response = res;
            String results = DeleteInfo.doPost();
            
            str = '{"Request":{"Header":{"TransactionType":"DeleteInfo","TimeStamp":"03-15-2016 12:09:15:252","FileID":"DeleteInfo.03152016120915252"},"parent":{"StarID":"StarID","AccountCode":"WD","SampleCode":"SampleCode"}}}';
            req.requestURI = '/DeleteInfo/*';  
            req.httpMethod = 'Post';
            RestContext.request = req;
            RestContext.request.requestBody = Blob.valueOf(str);
            RestContext.response = res;
            DeleteInfo.doPost();
            
            str = '{"Request":{"Header":{"TransactionType":"DeleteInfo","TimeStamp":"03-15-2016 12:09:15:252","FileID":"DeleteInfo.03152016120915252"},"parent":{"StarID":"StarID","AccountCode":"WD","SampleCode":"SampleCode"}}}';
            req.requestURI = '/DeleteInfo/*';  
            req.httpMethod = 'Post';
            RestContext.request = req;
            RestContext.request.requestBody = Blob.valueOf(str);
            RestContext.response = res;
            DeleteInfo.doPost();
            
            Str = '{"Request":{"Header":{"TransactionType":"DeleteInfo","TimeStamp":"03-15-2016 12:09:15:252","FileID"!=""},"parent":{"StarID"!="","AccountCode"!="","SampleCode"!=""}}}';
            req.requestURI = '/DeleteInfo/*';  
            req.httpMethod = 'Post';
            RestContext.request = req;
            req.requestBody = Blob.valueOf(str);
            RestContext.response = res;
            DeleteInfo.doPost();

            Test.stopTest();
        }
    }
}


Here i was facing issue was code was almost covered but while run the test class i was showing error.

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


if anyone please help me.


Thanks INadvance,
RajuBalaji
WEN JIEWEN JIE
Hi Raju,

Actually, you could add some breakpoints in your origional apex class and test class ,then debug the test class, I think you will find the problem.

NullPointerException exception means you program wants an apple, but there is no apple in contexts.

Thanks.
rajubalajirajubalaji
Hi WEN JIE,

Thanks for very quick reply.Please can u give me some guidelines where i want to add the steps.if possible.

it reaaly helpful for me.
Thanks Inadvance,
RajuBalaji
WEN JIEWEN JIE
Hi Raju,

You could add following code in your apex and test call, then run then and check which part have probelm.
System.debug('======log======' + ptList);

https://www.tutorialspoint.com/apex/apex_debugging.htm