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
akallioWileyakallioWiley 

Help with null pointer exception

The method below fails and throws a null pointer exception on the highlighted and only on the highlighted line, which is strange because the highlighted is nearly exactly the same as a line above it that is in a different if/else branch. The other thing that has me scratching my head is that the system.assert(also highlighted) shows me that the variable called 'source'  is not null and has the expected value. So, can anyone spot why I might be getting this error?

 

Thanks!

  public static void saveLogic(Map<Id,List<courseCodeWrapper>> consCourseCodeMap, String triggerType, String source) {
        
        if(triggerType == 'Insert/Update') {            
            List<Contact_Course_Code__c> conCourseCodes = new List<Contact_Course_Code__c>();
                        
            for(Contact con : [Select Id,Name, Course_Code_Count__c, (Select Id, Contact__c, Course_Code__c, CourseRoleCount__c, Current__c, Primary__c, EvalCount__c from Contact_Course_Code__r) from Contact where Id IN: consCourseCodeMap.keySet()]) {
                
                if(con.Course_Code_Count__c == 0) {
	                    for(courseCodeWrapper cc : consCourseCodeMap.get(con.Id)) {
	                        
	                        //Integer currentCount = cc.currentCheck ? 1 : 0;
	                		//Integer primaryCount = cc.PrimaryCheck ? 1 : 0;
	                        
	                        Contact_Course_Code__c newCCC = new Contact_Course_Code__c(
	                    		Contact__c = con.Id,                            
	                        	Course_Code__c = cc.courseCodeId,
	                            CourseRoleCount__c = source == 'Course' ? 1 : 0,
	                            EvalCount__c = source == 'Eval' ? 1 : 0
	                            //Current__c = currentCount,                                    
	                            //Primary__c = primaryCount                            
	                    	);
	                        conCourseCodes.add(newCCC);
	                    }                	
                    insert conCourseCodes;
                } else if(con.Course_Code_Count__c > 0) {
                    for(courseCodeWrapper cdWrap : consCourseCodeMap.get(con.Id) ) {
                        
                        //Integer currentCounter = cdWrap.currentCheck ? 1 : 0;
                        //Integer primaryCounter = cdWrap.primaryCheck ? 1 : 0;
                        
                        for(Contact_Course_Code__c conCD : con.Contact_Course_Code__r) {
                            if(conCD.Course_Code__c == cdWrap.CourseCodeId) {                                
                                conCD.CourseRoleCount__c += source == 'Course' ? 1 : 0;
                                conCD.EvalCount__c += source == 'Eval' ? 1 : 0;
                                //conCD.Current__c += currentCounter;
                                //conCD.Primary__c += primaryCounter;
                                conCourseCodes.add(conCD);
                            }
                        }
                    }
                    update conCourseCodes;
                }
            }
        }
            
        if(triggerType == 'Delete') {
            List<Contact_Course_Code__c> conCourseCodesUpdate = new List<Contact_Course_Code__c>();
            List<Contact_Course_Code__c> conCourseCodesDel = new List<Contact_Course_Code__c>();
                        
            for(Contact con : [Select Id,Name, Course_Code_Count__c, (Select Id, Contact__c, Course_Code__c, CourseRoleCount__c, Current__c, Primary__c,Total_Association_Count__c from Contact_Course_Code__r) from Contact where Id IN: consCourseCodeMap.keySet()]) {
                
                if(con.Course_Code_Count__c > 0) {
                    for(courseCodeWrapper cdWrap : consCourseCodeMap.get(con.Id) ) {
                        
                        //Integer currentCounter = cdWrap.currentCheck ? 1 : 0;
                        //Integer primaryCounter = cdWrap.primaryCheck ? 1 : 0;
                        
                        for(Contact_Course_Code__c conCD : con.Contact_Course_Code__r) {
                            if(conCD.Course_Code__c == cdWrap.CourseCodeId) {                                
                                if(conCD.Total_Association_Count__c > 1) {
                                    conCD.CourseRoleCount__c -= source == 'Course' ? 1 : 0;  //system.assert(false, 'This is your SOURCE: '+source);
                                    conCD.EvalCount__c -= source == 'Eval' ? 1 : 0;
                                    //conCD.Current__c += currentCounter;
                                    //conCD.Primary__c += primaryCounter;
                                    conCourseCodesUpdate.add(conCD);
                                }else{
                                    conCourseCodesDEL.add(conCD);
                                }
                            }
                        }
                    }
                    if(conCourseCodesUpdate.size() > 0) {
                    	update conCourseCodesUpdate;
                    }
                    if(conCourseCodesDel.size() > 0) {
                        delete conCourseCodesDel;
                    }
                }
            }
        }    	
    }

 

Best Answer chosen by Admin (Salesforce Developers) 
JimmyK12JimmyK12

I think you're missing EvalCount__c from your nested query in the 2nd contact for loop. one would think you would see "SObject row was retrieved via SOQL without querying the requested field:EvalCount__c" but it might have different rules when it comes to the inner queries.

 

Add the red part

 

for(Contact con : [Select Id,Name, Course_Code_Count__c, (Select Id, Contact__c, Course_Code__c, CourseRoleCount__c, Current__c, Primary__c,Total_Association_Count__c, EvalCount__c from Contact_Course_Code__r) from Contact where Id IN: consCourseCodeMap.keySet()])...

All Answers

JimmyK12JimmyK12

I think you're missing EvalCount__c from your nested query in the 2nd contact for loop. one would think you would see "SObject row was retrieved via SOQL without querying the requested field:EvalCount__c" but it might have different rules when it comes to the inner queries.

 

Add the red part

 

for(Contact con : [Select Id,Name, Course_Code_Count__c, (Select Id, Contact__c, Course_Code__c, CourseRoleCount__c, Current__c, Primary__c,Total_Association_Count__c, EvalCount__c from Contact_Course_Code__r) from Contact where Id IN: consCourseCodeMap.keySet()])...

This was selected as the best answer
akallioWileyakallioWiley
THANKS!!