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
m@x~W0W~m@x~W0W~ 

What's wrong with the below code???

System.NullPointerException: Attempt to de-reference a null object: Trigger.updateCount: line 13, column 1

 

 

trigger updateCount on Student__c (after insert, before delete)
{
//////////////////////////////////////
// When insert/update any student’s in class then update MyCount Accordingly.
// Created 20131009 max
// Modified 20131009
//////////////////////////////////////
if(Trigger.isAfter && Trigger.isInsert)
{
for (Student__c stent : Trigger.new)
{
Id cid = stent.Class__c;
Integer clist = [Select Count() FROM Student__c where Class__c =: cid ];
//stent.Class__r.My_Count__c = clist;
}
}
else if(Trigger.isAfter && Trigger.isDelete)
{
for (Student__c stent : Trigger.new)
{
Id cid = stent.Class__c;
Integer clist = [Select Count() FROM Student__c where Class__c =: cid ];
//stent.Class__r.My_Count__c = clist;
}
}

}

Rahul_sgRahul_sg
can you put an If statement to check if stent.Class__c is not null
m@x~W0W~m@x~W0W~
why does the below code refer to null??
stent.Class__r.My_Count__c
instead of referring to My Count field present in Class__c object.
Satish_SFDCSatish_SFDC
Check this line:
Id cid = stent.Class__c;

See if the Class__c field value is not empty.

Regards,
Satish Kumar
m@x~W0W~m@x~W0W~

Satish It returns the ID of class and its not null.
The problem is with this code "stent.Class__r.My_Count__c".

stent is a child object(Student__c) reference, using it I'm trying to access field present in parent object(i.e class__c)

 

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger updateCount caused an unexpected exception, contact your administrator: updateCount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateCount: line 16, column 1

 

Help will be appreciated

AuyonAuyon

Max,

I am new to salesforce but I believe there are several issues here. I am considering you would like to maintain the count of the students of a particular class in a class record.

a. Trigger.New is available only in insert and update triggers. And is updateable in before triggers only.
b. You are updating a field in class__c object in the trigger of Student__c, you would need to do a explicit update here. (atleast that what I read, may there is a cool way other than that I am not sure).
c. Each time in the loop you are doing a query to the get a count of the student records for a given class, which we can avoid as it might be unneccessary if say all the records belong to the same class.
d. In the second if you are checking isAfter and isDelete where as you have registered the trigger as before delete. I think that one must be a typo.

Now, If Class is a master of Student (typically it would be) I would rather add a roll-up field for this. But if I had to do it through a trigger I would do something like this.
(* Note just a sample it can be written in much efficient way)

 

trigger updateCount on Student__c (after insert, before delete)
{
//////////////////////////////////////
// When insert/update any student’s in class then update MyCount Accordingly.
// Created 20131009 max
// Modified 20131009
//////////////////////////////////////
	Set<Id> sClassIDs = new Set<Id>();
	Map<Id, Decimal> mClassWithCounts = new Map<Id, Decimal>();
	
	List<Class__c> lClassListToUpdate = [select id, My_Count__c from Class__c];
	
	for (Class__c classItem : lClassListToUpdate) {			
			mClassWithCounts.put(classItem.id,classItem.my_count__c );			
	}
	
	if(Trigger.isAfter && Trigger.isInsert)
	{
		for (Student__c stent : Trigger.new)
		{	
			Decimal dCurrentCount ;			
			if(mClassWithCounts.containsKey(stent.Class__c)) 
				dCurrentCount = mClassWithCounts.get(stent.Class__c)+1;
			else 
				dCurrentCount = 1;
			mClassWithCounts.put(stent.Class__c,dCurrentCount);
		}				
	}
	if(Trigger.isBefore && Trigger.isDelete)
	{
		for (Student__c stent : Trigger.old)
		{	
			Decimal dCurrentCount ;			
			if(mClassWithCounts.containsKey(stent.Class__c)) {
				dCurrentCount = mClassWithCounts.get(stent.Class__c)-1;
				
			} else {
				dCurrentCount = 0;			
			}
			
			mClassWithCounts.put(stent.Class__c,dCurrentCount);
		}				
	}
	
	if (mClassWithCounts.size() > 0) {
	
			
		for (Class__c classItem : lClassListToUpdate) {
			system.debug(mClassWithCounts.get(classItem.id)); 
			classItem.my_count__c =  mClassWithCounts.get(classItem.id);
		
		}
		
		update lClassListToUpdate;
	}

}