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
Austin_SteveAustin_Steve 

System.NullPointerException error

I'm writing a trigger, and in an "If" statement I'm trying to test if the picklist option that was selected, starts off the "Cer" (there are about 15 options that start off with Cer). Instead of putting in each possible option that has "Cer" in an if or case statement, I would just like to test and see if the selection starts off with "Cer". I have tried using both these statements but ran into problems.

account.field.contains('Cer')  account.field.startswith('Cer')

That field on the account page is not mandatory (nor can it be). So the trigger works well if that field has a value, however if there is no value, I get an error that says:

Error:Apex trigger CalcMRRv2 caused an unexpected exception, contact your administrator: CalcMRRv2: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CalcMRRv2: line 56, column 11

I would like to be able to use If (acc.field == 'Cer*'){

or something similar, but the wild card didn't seem to work.  Any suggestions?

 

Thanks

 

Steve

 

Park Walker (TAGL)Park Walker (TAGL)

You might want to try something like:

 

if (account.field != null && account.field.startsWith('Cer')). I can't recall off hand if this works with picklist values, but it should point you in the right direction.

 

Park