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
NasirNasir 

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();
        if((ApexPages.currentPage().getParameters().get('sms')).equals('lead'))
            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() {
    try{
        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')) {
            url='http://www.onewaysms.com.my/api2.aspx?apiusername=xxAPIU6QF39AOLY&apipassword=APIU6QF39AOLY8UXUB'+
            '&mobileno='+mobileNo+'&senderid=NilaiUni.&languagetype=1&message=RM0.00 '+ 
            EncodingUtil.urlEncode(msg, 'UTF-8') +'';
        }
        else {
            url='http://www.onewaysms.com.my/api2.aspx?apiusername=xxAPIU6QF39AOLY&apipassword=APIU6QF39AOLY8UXUB'+
            '&mobileno='+mobileNo+'&senderid=NilaiUni.&languagetype=1&message='+ 
            EncodingUtil.urlEncode(msg, 'UTF-8') +'';
        }
        //System.debug('### URL: '+ url);
        
        status = shrr.getContent(url);
        System.debug('### res.getBody() '+status);
        
        if(status.equals('-600'))
            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';
        else
            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';
       insert(smsObj);
       
       System.debug('### SMS ID: '+ smsObj.Id);      
       smsPR = new PageReference('/'+ smsObj.Id);
       smsPR.setRedirect(true);
       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();
        smsCont.sendAction();
        
     }
     
}

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

 

if((ApexPages.currentPage().getParameters().get('sms')).equals('lead'))
            smsObj.To_Lead__c = ApexPages.currentPage().getParameters().get('CF00NO0000000GAGe_lkId');

 

dmchengdmcheng

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