You need to sign in to do that
Don't have an account?
Help: System.NullPointerException: Attempt to de-reference a null object:
Hello, I'm hoping someone can help me out!
So this code is pretty basic in idea. A custom object is created or edited. It has numerous lookups to the user table (each representing a different type of relationship, for example: Field Rep).
The user table as number fields that this code is meant to decrement or increment.
The issue is this; when i made an adjustment to the user table, if i set a static value--its fine, example:
u.Sales_Area_Field_Rep__c=10;
But if I do something like the following, I get the de-reference error
u.Sales_Area_Field_Rep__c++;
u.Sales_Area_Field_Rep__c+=1;
u.Sales_Area_Field_Rep__c=u.Sales_Area_Field_Rep__c+1;
Here is a big snippet of the code. Any help would be REALLY appreciated.
trigger UpdateSalesAreaToUserCount on Sales_Area__c (before insert, before update) { String prefix = User.sObjectType.getDescribe().keyPrefix; list <User> usersToUpdate= new List <User>{}; if(Trigger.IsUpdate){ for (Sales_Area__c co : Trigger.new) { Sales_Area__c coOld = Trigger.oldMap.get(co.Id); //Begin Field Rep if(coOld.Field_Rep__c != co.Field_Rep__c){ if(!String.isBlank(coOld.Field_Rep__c)) { User u = new User(Id = coOld.Field_Rep__c, Sales_Area_Field_Rep__c=coOld.Field_Rep__r.Sales_Area_Field_Rep__c); u.Sales_Area_Field_Rep__c++; usersToUpdate.add(u); } if(!String.isBlank(co.Field_Rep__c)) { User u = new User(Id = co.Field_Rep__c, Sales_Area_Field_Rep__c=co.Field_Rep__r.Sales_Area_Field_Rep__c); u.Sales_Area_Field_Rep__c=10; usersToUpdate.add(u); }
And just to reiterate what I said in my first post - before testing, please make sure there is a value in the the Sales_Area_Field_Rep__c field on the user. Or add in null checks like the below:
All Answers
Actually looking back at your answer, I'm confused on two things. You say...
"If you want to avoid querying, just make sure that the field on all user records is not blank (will require a data migration, and preferably a default value of 0 on new users"
But we want to increment/decrement, so we need to know the value.
So I created a user, i assigned it the same ID as the 'newmap' (this also works for the old map mind you). And I was able to assign Sales_Area_Field_Rep a value of 10. Clearly I was able to 'know' the id of the the user id from my map.As to the rest..I'm also confused.
This code words (assignign a static value)
So then what is wrong with this?
I'm assigning the Sales Area at create from the map (with new or old map mind you). Can you use references in a map? It's not throwing any errors when I try to do that?
Whats strange is this causes no error:
But this does
I think something like this would do the trick: If you wanted to increment the Sales_Area_Field_Rep__c on the user that is newly assigned to the Sales_Area__c, you would want the soql query statement to say WHERE Id in : Trigger.new, but I used Trigger.old since it looked like you were taking the value of the old Field Rep.
Tried the above. No errors but--it's not doing anything anymore (specifically to the old values). Even if I assign a explicit value like u.Sales_Area_Field_Rep__c = 10 it now does nothing.
What the code looks like. Basically what you posted. Im....wondering if with how you have the for loop if its breaking the check 'if changed' condition?
Where are your defining "cosChanged" from line 18?
I assume something like...
?
And just to reiterate what I said in my first post - before testing, please make sure there is a value in the the Sales_Area_Field_Rep__c field on the user. Or add in null checks like the below: