0

I have the below code in my controller Class in a Verify () function -

public class SA_reCAPTCHA {

    private Reference__c Reference;
    public List<Reference__c> RefID = new List <Reference__c>();
    public List<WrapperReference> ListReferences {get;set;}
    public WrapperReference wrapper {get;set;}
    public Boolean confirmed { get; public set; }

    public SA_reCAPTCHA(ApexPages.StandardController controller) 
    {
      this.Reference = (Reference__c)controller.getRecord();
      this.Reference.Candidate__c = apexpages.currentpage().getparameters().get('CanID');
      ListReferences = new list <WrapperReference>();
      wrapper = new WrapperReference();
      ListReferences.add(wrapper);

      system.debug ('Current Reference Record -'+ this.Reference);
    }

    public SA_reCAPTCHA() {
        this.verified = false;
       // this.confirmed = false;
        }

    public PageReference addReference() // called from vf
    {
       WrapperReference wrapper = new WrapperReference();
       ListReferences.add(wrapper); 
       return null;
    }

    public class WrapperReference {
        public Reference__c Ref {get; set;} 
        public WrapperReference() {
            Ref = new Reference__c(); 
        }
    }

    // this method is called on click of Submit button
    public PageReference verify() {

            Set <String> EmailSet = new set<string>();
            Set <String> MobileSet = new set<string>();


            for (WrapperReference wrapper : ListReferences) 
            { 
              if(!Pattern.matches('[0-9]{10}', wrapper.ref.Mobile__c)) 
              {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Invalid Mobile No. Please enter 10 digit phone no.'));
                return null;
              }
              if(wrapper.ref.Office_Phone__c != null && (!Pattern.matches('[0-9]{10}', wrapper.ref.Office_Phone__c))) 
              {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Invalid office Phone No. Please enter 10 digit phone no.'));
                return null;
              }
             EmailSet.add(wrapper.ref.Email__c);
             MobileSet.add(wrapper.ref.Mobile__c);
            }

            system.debug ('ListReferences size '+ListReferences.size());
            system.debug ('EmailSet size '+EmailSet.size());
            system.debug ('MobileSet size '+MobileSet.size());

            if (ListReferences.size() != EmailSet.size() || ListReferences.size() != MobileSet.size())
            {
               ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Email Address and Mobile No. should be unique for each reference')); 
               return null;
            } 

            List<Reference__c> listRef = new List<Reference__c>();
            for (WrapperReference wrapper : ListReferences) 
            { 
             Wrapper.ref.Candidate__c = apexpages.currentpage().getparameters().get('CanID');
             listRef.add(wrapper.Ref);
            } 

            if (listRef.size() > 0)
            {
             insert listRef;
             system.debug ('Inserted References - '+ listRef);
             PageReference pg = new PageReference('/apex/SA_Candidate_Ref_ThankYou');
             pg.setRedirect(false);
             return pg;
            }  

            return null;


            } // end method

    }

When I run the associated test class, it fails with below message. If I remove the above code, the test class succeeds. How can I test cover the pattern matcher?

Error Message System.NullPointerException: Script-thrown exception Stack Trace Class.System.Pattern.matcher: line 30, column 1 Class.System.Pattern.matches: line 20, column 1 Class.SA_reCAPTCHA.verify: line 112, column 1 Class.SA_reCAPTCHA_Test.References: line 56, column 1

Below is the related snippet from my Test Class -

Reference__c r1 = new Reference__c();
r1.Candidate__c = con.id;
r1.First_Name__c = 'test ref first';
r1.last_Name__c = 'test ref last';
r1.Mobile__c = '3134345671';
insert r1; 

    Reference__c ref = new Reference__c();
    ref.id = r1.id;
    ref.Candidate__c = r1.Candidate__c;
    ref.First_Name__c = r1.First_Name__c;
    ref.last_Name__c = r1.Last_Name__c;
    ref.Mobile__c = r1.Mobile__c;

    SA_reCAPTCHA RefCntrl = new SA_reCAPTCHA(new ApexPages.StandardController(ref));
    RefCntrl.Verify();
  • can you highlight lines with line number in your code? – Oleksandr Berehovskyi Nov 8 '18 at 5:18
  • 1
    Your error, strictly speaking, isn't here. It may happen here, but we don't have enough details to tell. How is ListReferences set? Did you call the methods necessary? Is the Wrapper properly initialized? There's no way we can tell just from the snippet here. – sfdcfox Nov 8 '18 at 5:23
  • I updated the code – Maneesh Nov 8 '18 at 5:35
2

Your wrapper adds its own Reference__c object which will have all null fields:

public class WrapperReference {
    public Reference__c Ref {get; set;} 
    public WrapperReference() {
        Ref = new Reference__c(); 
    }
}

Wrappers are more often written like this:

public class WrapperReference {
    public Reference__c Ref {get; set;} 
    public WrapperReference(Reference__c Ref) {
        this.Ref = Ref;
    }
}

so that an object can be passed in such as the (Reference__c)controller.getRecord() from the standard controller that does have its fields initialised:

public SA_reCAPTCHA(ApexPages.StandardController controller) 
{
  this.Reference = (Reference__c) controller.getRecord();
  ...
  wrapper = new WrapperReference(this.Reference);
  ...
}
  • Thanks Keith. that worked! I should have caught that myself – Maneesh Nov 8 '18 at 13:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.