+ Start a Discussion
kumarcrm bingikumarcrm bingi 

tirgger issue attment to dereference null issue click save button i got the issue

i am writing tirgger got issue attment to dereference null issue click save button i got the issue

ChangingFirstlttrUpper Trigger 
=======================
trigger ChangingFirstlttrUpper on Project__c (before insert,before update) {
ChanignToCaps.Change(trigger.new);
    if(trigger.isBefore ){
         if(trigger.IsInsert ||trigger.IsUpdate ){
          ChanignToCaps.Change(trigger.new);
         ChanignToCaps.Change(trigger.new);        }
        
    }
}

Handler class
------------------------
global class ChanignToCaps{
   global static void Change( Project__c [] Pro){
      for(Project__c  P :Pro){
         if(P.PoPupfield__c !=null || P.PoPupfield__c  != ' '){
           P.PoPupfield__c = formatToUpper(P.PoPupfield__c );
   }
   
}

Helper Class
----------------------
public class FirstlttrUpper{
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;
}
}

Error 

ERROR!
ChangingFirstlttrUpper: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.FirstlttrUpper.formatToUpper: line 4, column 1 Class.ChanignToCaps.Change: line 10, column 1 Trigger.ChangingFirstlttrUpper: line 2, column 1
Best Answer chosen by kumarcrm bingi
Balaji Malemarpuram 10Balaji Malemarpuram 10
Hi ,

Please replace your statement if(P.PoPupfield__c !=null || P.PoPupfield__c  != ' ')
with if(P.PoPupfield__c !=null && P.PoPupfield__c  != ' ')

Thanks,
Balaji M 

All Answers

Balaji Malemarpuram 10Balaji Malemarpuram 10
Hi ,

Please replace your statement if(P.PoPupfield__c !=null || P.PoPupfield__c  != ' ')
with if(P.PoPupfield__c !=null && P.PoPupfield__c  != ' ')

Thanks,
Balaji M 
This was selected as the best answer
CharuDuttCharuDutt
Hii Kumarcrm
Try Below Code
ChangingFirstlttrUpper Trigger 
=======================
trigger ChangingFirstlttrUpper on Project__c (before insert,before update) {
ChanignToCaps.Change(trigger.new);
    if(trigger.isBefore ){
         if(trigger.IsInsert ||trigger.IsUpdate ){
          ChanignToCaps.Change(trigger.new);
         ChanignToCaps.Change(trigger.new);        }
        
    }
}

Handler class
------------------------
global class ChanignToCaps{
   global static void Change( Project__c [] Pro){
      for(Project__c  P :Pro){
         if(P.PoPupfield__c != null && P.PoPupfield__c != ''){
           P.PoPupfield__c = formatToUpper(P.PoPupfield__c );
   }
   
}

Helper Class
----------------------
public class FirstlttrUpper{
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;
}
}
Please Mark it As Best Answer if It Helps
Thank You!