I am trying to refactor some code. Right now I have a trigger calling lots of methods. And I would like to to have it call on master method. Right now I am getting a null pointer exception. And I don't understand why. I feel like I did all the right things to make I am not referencing an empty collection. I am not even getting the issue seems to be in the healthCheckController method.

Any help would be great.

Error: "Relationship_Owner_Trigger_Handler: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Class.RelationshipOwner_Health_Checks.healthCheckController: line 20, column 1 Trigger.Relationship_Owner_Trigger_Handler: line 18, column 1"

The Trigger

trigger Relationship_Owner_Trigger_Handler on relationship_owner__c (before insert, before update, before delete, after insert, after update, after delete, after undelete) {

if (Trigger.isBefore) {
if (Trigger.isInsert) {
// Call class logic here!
if (Trigger.isUpdate) {

RelationshipOwner_Health_Checks.healthCheckController(Trigger.oldMap, Trigger.new);//*****Line 18 Is here*****

And the Class

public with sharing class RelationshipOwner_Health_Checks {

  //method calls other methods to run scoring
    public static void healthCheckController(Map<Id, relationship_owner__c> oldMap, List<relationship_owner__c> newList){
    //methods for total unsubscribes

   List<relationship_owner__c> unsubscribeUpdate;
    for(relationship_owner__c owner : newList){
        relationship_owner__c oldOwner = oldMap.get(owner.Id);

        if (owner.Total_Unsubscribes_DLRS__c != oldOwner.Total_Unsubscribes_DLRS__c
                && owner.Total_Unsubscribes_DLRS__c != null) {
            System.debug('Total unsubsribes found and passed to method = ' + unsubscribeUpdate.size());                        
    }//**** Line 20 is here***************
    //final score
    //final grade

public static void updateOwnerUnsubscribeRate(List<relationship_owner__c> relationshipOwnersList){
    for(relationship_owner__c owner : relationshipOwnersList){
        owner.Unsubscribe_Rate__c = owner.Total_Unsubscribes_DLRS__c / owner.Total_Influencers_DLRS__c;
  • 1
    Hey Brooks, what line is the NPE thrown on? – David Reed Dec 11 '18 at 16:33
  • Even though including your error helps, it is difficult to match a line number to a code sample on this site. Please track down which line is indicated and edit your post to make it clear and easy for us to identify as well. Thanks! – Adrian Larson Dec 11 '18 at 16:45
List<relationship_owner__c> unsubscribeUpdate;

unsubscribeUpdate is uninitialized. Every method call against that object will throw a NullPointerException.

As an aside, this code looks like it potentially could be replaced with a formula field referencing your DLRS lookups. I don't know that I see enough of the flow represented here to be sure of that.

  • 1
    I added the error in above. The formula exceeded the byte limits. It was referencing lookups that were referencing lookups. So I added it to Process Builder. But PB always has bulk issues. That was why I decided to refactor. – Brooks Johnson Dec 11 '18 at 16:45
  • An embarrassing thing to miss for sure. I didn't think an error like that could get past the compiler? – Brooks Johnson Dec 11 '18 at 16:57
  • There is relatively little static analysis-type error checking in the compiler. It's legal to declare, pass, and use uninitialized objects and primitives (both of which have the value null). – David Reed Dec 11 '18 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.