0

I am getting

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

Class.exposewsdl.getCaseDetailsFromcsNum: line 47, column 1

global class exposewsdl{
    global class requestKT{
        webservice string inputName;

    }
       global class accList{
        webservice String sName;
        webservice String sPhone;
        webservice String sEmail;
    }
    global class conList{
       webservice String sName;
        webservice String sPhone;
        webservice String sEmail;
    }
    global class leadList{
        webservice String sName;
        webservice String sPhone;
        webservice String sEmail;
    }
    global class responseOfWs{
        List<accList> accList;
        List<leadList> leadList;
        List<conList> conList;
        webservice string errorFound;
    }
    webservice static responseOfWs getCaseDetailsFromcsNum(requestKT csNum){


            List<Lead> leadListt =New List<Lead>();
             List<contact> conListt= New List<contact>();
             List<account> accListt=New List<account>();

             String searchStr1 = '*'+csNum.inputName+'*';
             String searchQuery = 'FIND \'' +searchStr1  + '\' IN ALL FIELDS RETURNING  Account (Name,Phone,PersonEmail),Contact(Name,Phone,Email),Lead(Name,Phone,Email)';
            List<List <sObject>> searchList = search.query(searchQuery);
            accListt = ((List<Account>)searchList[0]);
            conListt  = ((List<contact>)searchList[1]);
            leadListt = ((List<Lead>)searchList[2]);
            responseOfWs res = new responseOfWs();
            for (Integer i = 0; i<accListt.size(); i++) {
            accList acRes = new accList();
                    acRes.sName=accListt[i].Name;
                    acRes.sEmail=accListt[i].PersonEmail;
                    acRes.sPhone=accListt[i].Phone;
                    system.debug('test'+acRes);
                    res.accList.add(acRes);   
            }   

            for (Integer i = 0; i < conListt.size(); i++) {
             conList coRes = new conList();
                    coRes.sName=conListt[i].Name;
                    coRes.sEmail=conListt[i].Email;
                    coRes.sPhone=conListt[i].Phone;
                    system.debug('test'+coRes);
                    res.conList.add(coRes);
            }
            for (Integer i = 0; i < leadListt.size(); i++) {
            leadList leRes = new leadList();
                    leRes.sName=leadListt[i].Name;
                    leRes.sEmail=leadListt[i].Email;
                    leRes.sPhone=leadListt[i].Phone;
                    system.debug('test'+leRes);
                    res.leadList.add(leRes);
            }
            return res;
        }


}

2 Answers 2

2

Inside this part you should declare the creator function to instantiate the lists of the class, otherwise when you are trying to add values there you get a null pointer since the responses lists haven't been instantiated.

global class responseOfWs{
        List<accList> accList;
        List<leadList> leadList;
        List<conList> conList;
        webservice string errorFound;
ResponseofWs(){
  accList = new List<accList> ();
  leadList = new List<LeadList> ();
  conList = new List <conList> ();
}
    }
1

You didn't really need to "copy" the lists, as you could have simply written:

webservice static responseOfWs getCaseDetailsFromcsNum(requestKT csNum){
    csNum.inputName = '*'+csNum.inputName+'*';
    List<SObject[]> results = [
        FIND :csNum.inputName IN ALL FIELDS RETURNING  
        Account (Name,Phone,PersonEmail),
        Contact(Name,Phone,Email),
        Lead(Name,Phone,Email)];
    responseOfWs res = new responseOfWs();
    res.accList = (Account[])results[0];
    res.conList = (Contact[])results[1];
    res.leadList = (Lead[])results[2];
    return res;
}

If you don't want the Id values to be returned, you can scrub those out:

for(SObject[] resultList: results) {
    for(SObject record: resultList) {
        record.Id = null;
    }
}

You can do this before building the 'res' variable.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .