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
nitinkhunal.ax1786nitinkhunal.ax1786 

BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object

trigger SONo1 on Service__c (before insert) {
   
    AutoNumber__c au=new AutoNumber__c(name='Service__c', In_Grantee__c=1001, Out_Of_Grantee__c=2001, etc__c=3001);
   
    for(AutoNumber__c auto:[select id, name,In_Grantee__c,Out_Of_Grantee__c,etc__c from AutoNumber__c where name='Service__c'])
   
    au=auto;
   
    for(Service__c ser:Trigger.new)
    {
        if(ser.Status__c=='INGrantee')
        {
            ser.SONO__c=Integer.valueOf(au.In_Grantee__c++);
        }
        if(ser.Status__c=='OutofGrantee')
        {
            ser.SONO__c=Integer.valueOf(au.Out_Of_Grantee__c++);
        }
        if(ser.Status__c=='etc')
        {
            ser.SONO__c=Integer.valueOf(au.etc__c++);
        }
       
    }
    upsert au;
}

Please tell me how to resolve this error..

Vinit_KumarVinit_Kumar

On which line you are exactly getting the error ??

nitinkhunal.ax1786nitinkhunal.ax1786

 ser.SONO__c=Integer.valueOf(au.In_Grantee__c++);


 ser.SONO__c=Integer.valueOf(au.Out_Of_Grantee__c++);

ser.SONO__c=Integer.valueOf(au.etc__c++);

 

 

In these lines i'm getting error according to choosing picklist value

Vinit_KumarVinit_Kumar

Why have u added ++ after ur field names,you should be using __c with the field except the ++.

 

Change from 

 

Out_Of_Grantee__c++

 

to

 

Out_Of_Grantee__c

nitinkhunal.ax1786nitinkhunal.ax1786

I want to incrememt value of this field every time i insert a new record thats why i've used ++ in this.. This field value will increment if i don't use ++?

Vinit_KumarVinit_Kumar

Try below :-

 

 

change from

 

ser.SONO__c=Integer.valueOf(au.In_Grantee__c++);

 

to 

 

ser.SONO__c=Integer.valueOf(au.In_Grantee__c) + 1;

Suraj Tripathi 47Suraj Tripathi 47
Hi Nitinkhunal,
You are performing increment operation before converting to integer. Because your code "Integer.valueOf(au.In_Grantee__c++)" first increment the value then convert to integer.
So you have to perform the increment operation after converting to an integer.
Please refer to the below code.
 
for(Service__c ser:Trigger.new)
    {
        if(ser.Status__c=='INGrantee')
        {
            ser.SONO__c=Integer.valueOf(au.In_Grantee__c)+1;
        }
        if(ser.Status__c=='OutofGrantee')
        {
            ser.SONO__c=Integer.valueOf(au.Out_Of_Grantee__c)+1;
        }
        if(ser.Status__c=='etc')
        {
            ser.SONO__c=Integer.valueOf(au.etc__c)+1;
        }
       
    }

In case you find any other issue please mention. 

If you find your Solution then mark this as the best answer. 
Thanks and Regards
Suraj Tripathi.