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
Ram Shiva KumarRam Shiva Kumar 

Before Update Trigger on Contact object

Hi i have written the trigger  in such away that when i update the  contact record , another Contact  record  hasto be inserted .But when i try to update the existing record it throwing the error like  " execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.wed: line 39, column 1" 

and my code is as below,

trigger wed on Contact(Before insert, Before Update)
{

Public Account a;

Public List<Contact> ab;

if(Trigger.isinsert==TRUE)
{
For(Contact c:Trigger.New)

{

ab=new List<Contact>();

 a= [select Phone from Account where ID=:c.AccountID];
 
 c.Phone=a.Phone;
 
 ab.add(c);
 
}

}

if (Trigger.isUpdate==TRUE)
{

For(Contact ct:Trigger.New)
{

String stv=ct.FirstName;

Contact cts=new Contact();

cts.FirstName='ram';
cts.LastName=ct.LastName;

ab.add(cts);

}
insert ab;

}

}

So please suggest me any one, where iam doing the error.




 
Best Answer chosen by Ram Shiva Kumar
Vasani ParthVasani Parth
Ram,

Do this,
 
trigger wed on Contact(Before insert, Before Update) {

		 Public Account a;

		 Public List < Contact > ab;

		 if (Trigger.isinsert == TRUE) {
		  For(Contact c: Trigger.New)

		  {

		   ab = new List < Contact > ();

		   a = [select Phone from Account where ID = : c.AccountID];

		   c.Phone = a.Phone;

		   ab.add(c);

		  }

		 }

		 if (Trigger.isUpdate == TRUE) {

		  For(Contact ct: Trigger.New) {

		   String stv = ct.FirstName;

		   Contact cts = new Contact();

		   cts.FirstName = 'ram';
		   cts.LastName = ct.LastName;

		   ab.add(cts);

		  }

		  if (ab != null) {
		   try {
		    insert ab;
		   } catch (DMLException e) {
		    system.debug(e.getMessage());
		   }
		  }

		 }

		}

Please mark this as the best answer if this helps

All Answers

lalitaroralalitarora
Hi Ram,

I can see that "Public List<Contact> ab" is not initiated before assigning the values to it.

Do reply if that solved your problem.
Vasani ParthVasani Parth
Ram,

Do this,
 
trigger wed on Contact(Before insert, Before Update) {

		 Public Account a;

		 Public List < Contact > ab;

		 if (Trigger.isinsert == TRUE) {
		  For(Contact c: Trigger.New)

		  {

		   ab = new List < Contact > ();

		   a = [select Phone from Account where ID = : c.AccountID];

		   c.Phone = a.Phone;

		   ab.add(c);

		  }

		 }

		 if (Trigger.isUpdate == TRUE) {

		  For(Contact ct: Trigger.New) {

		   String stv = ct.FirstName;

		   Contact cts = new Contact();

		   cts.FirstName = 'ram';
		   cts.LastName = ct.LastName;

		   ab.add(cts);

		  }

		  if (ab != null) {
		   try {
		    insert ab;
		   } catch (DMLException e) {
		    system.debug(e.getMessage());
		   }
		  }

		 }

		}

Please mark this as the best answer if this helps
This was selected as the best answer
Vasani ParthVasani Parth
This error is caused by a line of code that is trying to use an object that has not been instantiated, or an object's attribute that has not been initialized.
sailee handesailee hande
Hi ram ram 11,

Never write SOQL query inside for loop as it will cause SQOL 101 error if you insert bulk data. and As you have written ab=new List<Contact>(); inside for loop it is going to create new contact list everytime.

Thanks,
Sailee
Ram Shiva KumarRam Shiva Kumar
Hi All,

@vasani parth:Your code worked for me with samll correction of creating instance for the List<Contact>. Thanks alot.

@lalitarora: yoursuggest is exact and it worked for me. Thanks.


@Sailee hande: thanks for the suggestin. Couldyou please tell , how can i use the SOQL queryin the abocve code  to avoid the  soql 101 error.