点击保存按钮触发空指针异常及Account对象ChangingFirstlttrUpper触发器报错求助
Alright, let's break down why you're hitting that Attempt to de-reference a null object error and fix it properly.
Root Causes
Let's trace the error stack to pin down the issues:
- Null passed to formatting method: The error occurs at
FirstlttrUpper.formatToUpper: line 4when callingStr.split('[ ]+'). This means theStrparameter being passed here is null. - Broken conditional logic: In
ChanignToCaps.Change, your checkif(P.PoPupfield__c !=null || P.PoPupfield__c != ' ')is logically flawed. This condition will always evaluate totrue:- If
P.PoPupfield__cis null,P.PoPupfield__c != ' 'returns true - If it's a blank space,
P.PoPupfield__c !=nullreturns true
So even when the field is null, you're still callingformatToUpper(null)which triggers the null pointer exception.
- If
- Type mismatch: Your trigger is on the
Accountobject, butChanignToCaps.Changeexpects aProject__c[]array. This should cause a compile error immediately—you're passing a list of Accounts to a method built for Projects, which won't work. - Redundant code: The trigger calls
ChanignToCaps.Change(trigger.new)twice, which is unnecessary and could lead to unexpected behavior.
Fixes & Corrected Code
Let's address each issue one by one:
1. Fix the Conditional Logic
Replace the broken || with && and use isBlank() to handle full whitespace strings (more robust than checking for a single space).
2. Align Object Types
Since the trigger is on Account, update the helper method to accept Account[] instead of Project__c[] (adjust the field name to match your actual Account field—here I'll use Name as an example; swap it for your custom field like PoPupfield__c if needed).
3. Add Null Protection in formatToUpper
Even with fixed conditions, adding a guard clause in the formatting method ensures we never try to split a null string.
4. Clean Up Redundant Trigger Calls
Remove the duplicate ChanignToCaps.Change call in the trigger.
Corrected Trigger
trigger ChangingFirstLetterUpper on Account (before insert, before update) { ChangingToCaps.Change(trigger.new); }
Corrected ChangingToCaps Helper Class (fixed spelling + type alignment)
global class ChangingToCaps{ global static void Change(Account[] accList){ for(Account acc : accList){ // Only process if the field is not null and not blank (including full whitespace) if(acc.Name != null && !acc.Name.isBlank()){ acc.Name = FirstLetterUpper.formatToUpper(acc.Name); } } } }
Corrected FirstLetterUpper Class (fixed spelling + null guard)
public class FirstLetterUpper{ public static String formatToUpper(String str) { // Guard clause: return empty string if input is null/blank if(str == null || str.isBlank()){ return ''; } String result = ''; for(String iter : str.split('[ ]+')){ if(iter != null && !iter.isBlank()){ if(iter.length() > 1){ result += iter.substring(0,1).toUpperCase() + iter.substring(1) + ' '; } else { result += iter.toUpperCase() + ' '; } } } // Trim trailing space before returning return result.trim(); } }
Key Notes
- I fixed the spelling of class names (
Chanign→Changing,lttr→Letter) to make the code more maintainable—you can adjust these back if you need to match existing naming conventions, but clarity helps. - Swap
acc.Namein the helper class with your actual Account field (likePoPupfield__c) if that's the field you need to format. - The
isBlank()method (available in Salesforce API 24.0+) checks for both null and empty/whitespace-only strings, which is more reliable than manual checks.
内容的提问来源于stack exchange,提问作者bingi kumarcrm

