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
SF Beginner 2019SF Beginner 2019 

Attempt to deReference a Null Object when billing country is null please help

global class ChangingCapsLock{
    global static void Change( Account [] acc){
        for(Account a :acc){
            
            if(a.BillingPostalCode !=null )
                a.BillingPostalCode = allUpper(a.BillingPostalCode);
            
            if(a.BillingCountry !=null )
                a.BillingCountry= formatToUpper(a.BillingCountry);
            a.BillingCountry= formatToUpperLower(a.BillingCountry);
            
            if(a.BillingCountry ==null )
           String.isEmpty(a.BillingCountry);
            
            if(a.ShippingPostalCode !=null )
                a.ShippingPostalCode = allUpper(a.ShippingPostalCode);
            
            if(a.ShippingCountry !=null )
                a.ShippingCountry= formatToUpper(a.ShippingCountry);
            a.ShippingCountry= formatToUpperLower(a.ShippingCountry);
            
        }
    }
    public static String formatToUpper (String Str) {
        String result = '';
        for (String iter : Str.split('[ ]+')) {
            if (iter != null  && iter != '') {
                if (iter.length() > 1) {
                    result += iter.substring(0,1).toUpperCase() + iter.substring(1,iter.length()) + ' ';
                }
                else
                    result += iter.substring(0,1).toUpperCase() + ' ';
            }
        }
        return result;
    }
    public static String allUpper (String Str) {
        String result = '';
        for (String iter : Str.split('[ ]+')) {
            if (iter != null && iter != '') {
                if (iter.length() > 1) {
                    result += iter.substring(0,1).toUpperCase() + iter.substring(1,iter.length()).toUpperCase() + ' ';
                }
                else
                    result += iter.substring(0,1).toUpperCase() + ' ';
            }
        }
        return result;
    }
    public static String formatToUpperLower (String Str) {
        String result = '';
        for (String iter : Str.split('[ ]+')) {
            if (iter != null && iter != '') {
                if (iter.length() > 1) {
                    result += iter.substring(0,1).toUpperCase() + iter.substring(1,iter.length()).toLowerCase() + ' ';
                }
                else
                    result += iter.substring(0,1).toLowercase() + ' ';
            }
        }
        return result;
    }
}
 
trigger AccountChangerFirstLetter on Account (before insert, before update) {
 ChangingCapsLock.Change(trigger.new);
      if(trigger.isBefore )
         if(trigger.IsInsert ||trigger.IsUpdate )
           ChangingCapsLock.Change(trigger.new);    
}

 
Nayana KNayana K
You gave invoked ChangingCapsLock.Change twice in trigger which is unnecessary.

Just add the if condition in a block. Like, replace
if(a.BillingCountry !=null )
                a.BillingCountry= formatToUpper(a.BillingCountry);
            a.BillingCountry= formatToUpperLower(a.BillingCountry);

to

if(a.BillingCountry !=null ){
                a.BillingCountry= formatToUpper(a.BillingCountry);
            a.BillingCountry= formatToUpperLower(a.BillingCountry);
}

 
SF Beginner 2019SF Beginner 2019
the issue is that he cannot format a blank billing country, and its ending as null
SF Beginner 2019SF Beginner 2019
my main objective in here is that if the user enter billing country at iran, it will convert to Iran, then of the user enters IRAN, it will convert to Iran wherein proper casing
Nayana KNayana K
global class ChangingCapsLock{
    global static void Change( Account [] acc){
        for(Account a :acc){
            
            if(a.BillingPostalCode !=null )
                a.BillingPostalCode = a.BillingPostalCode.toUpperCase();
            
            if(a.BillingCountry !=null )
                a.BillingCountry= a.BillingCountry.capitalize();
            
            if(a.ShippingPostalCode !=null )
                a.ShippingPostalCode = a..ShippingPostalCode.toUpperCase();
            
            if(a.ShippingCountry !=null )
                a.ShippingCountry= a.ShippingCountry.capitalize();
            
        }
    }
}

When a field is null, if you set it to empty ("), then it will stay as null in the backend. It is the default behaviour. So, When it is null, leave it like that