-1

I have below generic class, whose methods are called from an Account before insert , before update trigger. Below class is getting a Null Pointer exception for any Account at commented line. Can someone please help me find the remedy?

Although the value I enter is not desired to be null.

Error: Review all error messages below to correct your data. Apex trigger Nameinitcaps caused an unexpected exception, contact your administrator: Nameinitcaps: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.AbbvieUtilitiesclass.initCaps: Apex class:

public class AbbvieUtilitiesclass
{
public static Map<String,String> M;
    public static String initCaps(String inputString)
    { 
    List<String> CapsName= inputString.split(' '); // This line gets Null pointer
     String RetName;
                for(Integer i=0;i<CapsName.size();i++)
                {
                    CapsName[i] = (CapsName[i].substring(0,1)).toUppercase() +(CapsName[i].substring(1,CapsName[i].length()).toLowercase());
                    if(i>0)
                    {
                        RetName=RetName+' '+CapsName[i];
                    }
                    else RetName=CapsName[i];    
                }



      return Retname;//return to trigger
    }

    public Static String translate(String str)
    {
    M= new Map<String,String>{'á'=>'a', 'é'=>'e' ,'í'=>'i', 'ó'=>'o', 'ú'=>'u', 'ü'=>'u', 'ñ'=>'n'};

   for (String key: M.keyset())//iterate over keyset
   {
   str.replace(key,M.get(key));//Get CORR Value
   }

   return str;//english



    }

        public static String McSpecialChars(String inputString)
    {       String RetSpName;
            List<String> FindName= inputString.split(' Mc');
                for(Integer i=0;i<FindName.size();i++)
                {  
                    FindName[i] = (FindName[i].substring(0,1)).toUppercase() +(FindName[i].substring(1,FindName[i].length()));
                    system.debug(FindName[i]+'1111');
                    if(i>0)
                    {
                        RetSpName=RetSpName+' Mc'+FindName[i];
                        system.debug(RetSpName+'Line63');   
                    }
                    else RetSpName=FindName[i];

                    system.debug(FindName[i]+'1111');
                }


                String SName =RetSpName;
            List<String> FindNamehyphenF= SName.split('-');
            for(Integer i=0;i<FindNamehyphenF.size();i++)
            {  
                FindNamehyphenF[i] = (FindNamehyphenF[i].substring(0,1)).toUppercase() +(FindNamehyphenF[i].substring(1,FindNamehyphenF[i].length()));
                system.debug(FindNamehyphenF[i]+'1111');
                if(i>0)
                {
                    SName=SName+'-'+FindNamehyphenF[i];
                    system.debug(SName+'@SaiLine63');   
                }
                else SName=FindNamehyphenF[i];
            }
            RetSpName=SName;

            String SpName =RetSpName;
            List<String> FiName= SpName.split('\'');
            for(Integer i=0;i<FiName.size();i++)
            {  
                FiName[i] = (FiName[i].substring(0,1)).toUppercase() +(FiName[i].substring(1,FiName[i].length()));
                system.debug(FiName[i]+'1111');
                if(i>0)
                {
                 SpName=SpName+'\''+FiName[i];
                 system.debug(SpName+'@Lastline');   
                }
                else SpName=FiName[i];
            }
            RetSpName=SpName;


                return RetSpName;
    }

}

2 Answers 2

3

I think that inputString is null and then you are trying to call the .split() function on it - either change the calling code to not pass in nulls or do a null check at the top of the function:

public static String initCaps(String inputString)
    { 
        if (null == inputString) {
            return '';
        }
        List<String> CapsName = inputString.split(' '); // This line gets Null pointer
2

The inputString doesn't contains any white space which can make that string to split and return a list however you can first check if it contains white space then split it

    if(inputString.contains(' ')){
       List<String> CapsName= inputString.split(' ');
}
2
  • Thanks Sagar. But problem is if there are no space in the string I need to make it initial caps. I am trying to achieve this by below. if(inputString.contains(' ')){ List<String> CapsName= inputString.split(' '); } else {String changeinput=String.Join(' ',inputString); List<String> CapsName= changeinput.split(' ');} But it says CapsName doesn't exist. Can you please help me here? Jan 30, 2015 at 8:25
  • Declaring CapsName before if condition as public variable should solve the problem Jan 30, 2015 at 9:23

You must log in to answer this question.

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