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
ch ranjithch ranjith 

dubplicatewithoutgov: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.dubplicatewithoutgov: line 4, column 1

trigger dubplicatewithoutgov on student__c (before insert)
{
   list<string> stlist=new list<string>();
   for(student__C st:trigger.old)
   {
    stlist.add(st.name);
   }
   for(student__C st:trigger.new)
   {
   for(integer i=0;i<stlist.size();i++)
   {
    if(st.name==stlist[i])
    {
     st.adderror('cant not add dupicate record');
     }
   }
}  
}
Best Answer chosen by ch ranjith
Jim JamJim Jam
maybe change the line .. String objectAsString=String.valueOf(stlist[i]) .. to String objectAsString = stlist[i].name;

All Answers

Jim JamJim Jam
Trigger.old is not valid in a before insert trigger.

for ref: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables_considerations.htmhttps://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables_considerations.htm
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
trigger dubplicatewithoutgov on student__c (before insert)
{
   list<student__C> stlist=[Select studentname from student__C];
   for(student__C st:trigger.new)
   {
   for(integer i=0;i<stlist.size();i++)
   {
    if(st.name==stlist[i])
    {
     st.adderror('cant not add dupicate record');
     }
   }

}
This will work.mark it as solved if it is answered
AshlekhAshlekh
Hi,

You have written trigger on before insert so you will never get the value of Trigger.old because there are not old record, records are going to insert first time.

You can read about trigger from here https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm
trigger dubplicatewithoutgov on student__c (before insert)
{
   list<string> stlist=new list<string>();
   for(student__C st:trigger.old) // You will never get this and is null always.
   {
    stlist.add(st.name);
   }
   for(student__C st:trigger.new)
   {
   for(integer i=0;i<stlist.size();i++) the size of stlist is always 0 so the next line never will execute.
   {
    if(st.name==stlist[i])
    {
     st.adderror('cant not add dupicate record');
     }
   }
}  
}
trigger dubplicatewithoutgov on student__c (before insert)
{
   list<string> stlist=new list<string>();
if(Trigger.isafter && Trigger.isUpdate && Trigger.isdelete) //Becuase you will get the value of trigger.old //only after event of update and isdelete
   for(student__C st:trigger.old)
   {
    stlist.add(st.name);
   }
   for(student__C st:trigger.new) //Don't know why you are using this approach.
   {
   for(integer i=0;i<stlist.size();i++)
   {
    if(st.name==stlist[i])
    {
     st.adderror('cant not add dupicate record');
     }
   }
}  
}


ch ranjithch ranjith
I tried this but not getting error for duplicate records...
trigger dubplicatewithoutgov on student__c (before insert)
{
   list<student__C> stlist=[Select name from student__C];
  
   for(student__C st:trigger.new)
   {
   for(integer i=0;i<stlist.size();i++)
   {
    String objectAsString= String.valueOf(stlist[i]);
    if(st.name==objectAsString)
    {
     st.adderror('cant not add dupicate record');
     }
   }
}
}
Jim JamJim Jam
maybe change the line .. String objectAsString=String.valueOf(stlist[i]) .. to String objectAsString = stlist[i].name;
This was selected as the best answer
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Ranjith just try cmrc line change.if you still dint get the output.let us know