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

test method not able to execute if statements(null pointer exception)

Hi All,


I am not able to execute if statement.It is giving me null pointer exception.below is the code

public class SingleSmsController {
    private ApexPages.StandardController con;
    private PageReference smsPR=null;
    public String msg {get; set;}
    private String url;
    private String mobileNo;
    private String status;
    private String msgStatus;
    private SMS__c smsObj;
    private SMSHttpRequestResponse shrr;
    public SingleSmsController(ApexPages.StandardController controller) {
        con = controller;
        shrr = new SMSHttpRequestResponse();
        smsObj = new SMS__c();
        smsObj = (SMS__c) con.getRecord();
            smsObj.To_Lead__c = ApexPages.currentPage().getParameters().get('CF00NO0000000GAGe_lkId');
        else if((ApexPages.currentPage().getParameters().get('sms')).equals('contact'))
            smsObj.To_Contact__c = ApexPages.currentPage().getParameters().get('CF00NO0000000GAGa_lkId');
    public PageReference sendAction() {
        if(smsObj.To_Lead__c != null) 
            mobileNo = [select Id,MobilePhone from Lead where id=:smsObj.To_Lead__c][0].MobilePhone;
        else if(smsObj.To_Contact__c != null) 
            mobileNo = [select Id,MobilePhone from Contact where id=:smsObj.To_Contact__c][0].MobilePhone;
        //System.debug('### MobileNo: '+ mobileNo);
        if(mobileNo.startsWith('60') || mobileNo.startsWith('+60')) {
            '&mobileno='+mobileNo+'&senderid=NilaiUni.&languagetype=1&message=RM0.00 '+ 
            EncodingUtil.urlEncode(msg, 'UTF-8') +'';
        else {
            EncodingUtil.urlEncode(msg, 'UTF-8') +'';
        //System.debug('### URL: '+ url);
        status = shrr.getContent(url);
        System.debug('### res.getBody() '+status);
            msgStatus = 'Failed-Insufficient balance';
        else if(status.equals('-300'))
            msgStatus = 'Failed-Invalid mobile number';
        else if(status.equals('-100'))
            msgStatus = 'Failed-Invalid credentials';
        else if(status.contains('-'))
            msgStatus = 'Failed';
            msgStatus = 'Success-Sent';
        //System.debug('### Status Number: '+ status);
        //System.debug('### Status String: '+ msgStatus);
       catch(Exception e) {
           msgStatus = 'Failed';
           //msgStatus = 'Failed-'+ e.getTypeName();
          // System.debug('### Exception: '+e.getMessage());
       smsObj.Message__c = msg;
       smsObj.Message_Status__c = msgStatus;
       smsObj.Mobile_No__c = mobileNo;
       smsObj.MessageID__c = status;
       smsObj.RecordTypeId = '012O000000008OB';
       System.debug('### SMS ID: '+ smsObj.Id);      
       smsPR = new PageReference('/'+ smsObj.Id);
       return smsPR;
     public static testMethod void smsTest(){
        SMS__c smsOb = new SMS__c(Message__c ='i am interested',To_Lead__c ='00QO0000000ydUa');
        insert smsOb;
        Lead l = new Lead();
        Contact co = new Contact();
        ApexPages.StandardController apexpag = new ApexPages.StandardController(smsOb);
        SingleSmsController smsCont = new SingleSmsController(apexpag);
        System.debug('nasir:'+ smsCont );
        SMSHttpRequestResponse shrr = new SMSHttpRequestResponse();

 the line below is giving me null pointer exception.Please help me to make this code 75 % covetage.


            smsObj.To_Lead__c = ApexPages.currentPage().getParameters().get('CF00NO0000000GAGe_lkId');



I think you need to set a URL parameter in your test method.  You can use the getParameter().put() method to do that.