I have a trigger which is calling several non-void methods of apex classes.But I always get 'null' as a return value from these methods. Can someone explain why is this happening? I tried using the static key-word too, but it is showing the same result, 'null'. Following is the code snippet I am using to call class methods from trigger:

String[] operations=cd.CustBusinessPro__Operations__c.split(',');
          System.debug('splitted operations>>>>'+operations);
          for(String str:operations)
              if(str == ' PAN_Check')
             { system.debug('inside  pan');
               PAN_Check pan = new PAN_Check();
              result = Pan.PanCheck(app.CustBusinessPro__PAN__c,app.CustBusinessPro__Last_Name__c);
              system.debug('>>>>>>>>returned value'+result);

             else if(str == 'De_Dup')
                system.debug('inside de_dup');
                De_Dup d = new De_Dup();
             else if(str == ' CIBIL Verification')
              system.debug('inside cibil');
              CIBILVerification cibil=new CIBILVerification();
             else if(str == ' Eligibility Verification')
             system.debug('inside eligibilty');
             EligibilityVerification elig=new EligibilityVerification();
             else if(str == ' EMI Verification')
                system.debug('inside EMI');
                EMI_Calculation emi = new  EMI_Calculation();
             else if(str == ' NegativeAreaCheck')
               system.debug('inside NAC');
               NegativeAreaCheck NAC = new NegativeAreaCheck();

          System.debug('Criteria not satisfied');

And this is the pan_check class:

public with sharing class PAN_Check{

public CustBusinessPro__Applicant__c applicant{get;set;}

String panCard{get;set;}
String LastName{get;set;}
String PanFormat{get;set;}

public PAN_Check(){

    applicant = new CustBusinessPro__Applicant__c();
    PanFormat = '[A-Z]{5}[0-9]{4}[A-Z]{1}'; //Definition of format of PAN consisting of 5 letters in the start followed by 4 numbers and again a letter. 


public  Boolean PanCheck(String panCardNo,String lname){

    Boolean flag;
    LastName = lname;

    panCard = panCardNo;
    System.debug('----------------------' + panCard);

    //Checking the PAN number against the defined format. 
    pattern MyPattern = pattern.compile(PanFormat);
    matcher MyMatcher = MyPattern.matcher(panCardNo);

    //for splitting the characters of PAN and the last name of the applicant. 
    string[] chars = panCardNo.split('');
    string[] chars1 = lname.split('');

    //checking if the 5th character of the PAN is equal to the 1st character of last name. 

        if(chars[4] == chars1[0]){

            //if its equal PAN check passed or else failed. 
            System.debug(panCard + '---------------PAN Check passed!!'); 



        System.debug(panCard + '---------------PAN Check failed!!');


    insert new Account(Name='MyPANCheck');
    return flag;

  • is it even getting into the if statement of ` if(MyMatcher.matches()){ ` Does it show the debug statement within the if statements?
    – Prady
    Commented Apr 11, 2016 at 13:09
  • Yes the debug statements are generated Commented Apr 11, 2016 at 13:14
  • 2
    Your declaration Boolean flag; leaves that value null if the field is not set in the if/else logic later in the method. So if you want the method to return false if it isn't set to a value in the if/else logic, initialise it where you declare it `Boolean flag = false;'.
    – Keith C
    Commented Apr 11, 2016 at 13:20
  • Yes as Prady pointed out, Its not getting inside the if/else part. But it was working fine when executed alone! Commented Apr 11, 2016 at 13:21
  • 1
    What do you mean by executed alone? Here is what could be happening ` if(MyMatcher.matches()){ ` is returning true and it goes into the if statement and thenchecks if(chars[4] == chars1[0]){ . If this statement returns false then the flag is null. You are not setting the value of Flag if this statement is false
    – Prady
    Commented Apr 11, 2016 at 13:28

1 Answer 1


The issue looks to me with you not setting the value of flag in the else statement for if(chars[4] == chars1[0]){

If the code execution does not satisfy the this if statement, then it exits out without setting any value for flag. You need to set the value of Flag in the else statement.

You must log in to answer this question.

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