0

I am trying to build an editable attendance sheet which can record new attendance records and edit old records based on date and course. The user can select a course which then queries the DB for enrolled students and creates a table based on the following APEX in visualforce:

{!Roster} is my query result on students enrolled in a particular class

{!attendanceOptions} are the 3 radio options for students attendance, "present," "tardy," and "absent."

<apex:pageBlockTable value="{!Roster}" var="Roster">
  <apex:column >
    <apex:facet name="header">Student Name</apex:facet>
    <apex:outputText value="{!Roster['Name']}"/> 
  </apex:column>
  <apex:column >
    <apex:facet name="header">Attendance</apex:facet>
    <apex:selectRadio value="{!attendanceRecord}" disabled="{!lockRecords}">
      <apex:selectOptions value="{!attendanceOptions}"/>
    </apex:selectRadio>
  </apex:column>
</apex:pageBlockTable>

I am would like to set the radio buttons value equal to the students' attendance records (present, tardy, or absent) if they exist for a particular day or to "present" is no records exist by changing the {!attendanceRecord} variable.

Here is the relevant class code for this visualforce page:

public with sharing class TestDisplayQueryList {
  public List<String> attendanceRecord {get;set;}
  public List<String> getAttendanceRecord(){
    if(attendanceRecord == null)
        List<String> attendanceRecord = new List<String>();
    return null;
  }
  public List<Contact> getRoster() {
    string record;
    List<Contact> roster = new List<Contact>();
    for(Contact Name : [SELECT Contact.Name, contact.id FROM contact WHERE contact.id IN (SELECT hed__Course_Enrollment__c.hed__Contact__c FROM hed__Course_Enrollment__c WHERE hed__Course_Enrollment__c.hed__Course_Offering__c=:className)]){
        roster.add(Name);
        for(Course_Attendance__c attendance : [SELECT Attendance_Record__c FROM Course_Attendance__c WHERE Date__c =:dat AND Contact__c =:name.id]){
           record = attendance.Attendance_Record__c;
           record = record.toLowerCase();
           attendanceRecord.add(record);
           roster.add(new string attendanceRecord);
        }
    }
 }

The problem is that I keep getting the error "Attempt to de-reference a null object" referencing the attendanceRecord.add(record); line of code as if the variable attendanceRecord doesn't exist. What am I doing wrong that is preventing this public variable from being used in this function?

1

You need to initialize the attendanceRecord Object.

The below method creates a local variable and the public class level variable is never initialized.

public List<String> getAttendanceRecord(){
    if(attendanceRecord == null)
        List<String> attendanceRecord = new List<String>();
    return null;
  }

I would suggest to initialize the attendanceRecord object like this:

attendanceRecord = new List<String>();

i.e. without providing the type: List<String> again locally

Also, I would suggest to initialize this in the constructor and change the variable name to something like: attendanceRecordObj, in order to avoid confusion what is being referred, getAttendanceRecord() or attendanceRecord

So change to the class code like this, should work for you:

public with sharing class TestDisplayQueryList {
  public List<String> attendanceRecord {get;set;}
  public TestDisplayQueryList(){
      attendanceRecord = new List<String>();
  }
  public List<Contact> getRoster() {
    string record;
    List<Contact> roster = new List<Contact>();
    for(Contact Name : [SELECT Contact.Name, contact.id FROM contact WHERE contact.id IN (SELECT hed__Course_Enrollment__c.hed__Contact__c FROM hed__Course_Enrollment__c WHERE hed__Course_Enrollment__c.hed__Course_Offering__c=:className)]){
        roster.add(Name);
        for(Course_Attendance__c attendance : [SELECT Attendance_Record__c FROM Course_Attendance__c WHERE Date__c =:dat AND Contact__c =:name.id]){
           record = attendance.Attendance_Record__c;
           record = record.toLowerCase();
           attendanceRecord.add(record);
           roster.add(new string attendanceRecord);
        }
    }
 }

or initializing in the method itself, should also work:

public List<Contact> getRoster() {
    attendanceRecord = new List<String>(); //Initialized here
    string record;
    List<Contact> roster = new List<Contact>();
    for(Contact Name : [SELECT Contact.Name, contact.id FROM contact WHERE contact.id IN (SELECT hed__Course_Enrollment__c.hed__Contact__c FROM hed__Course_Enrollment__c WHERE hed__Course_Enrollment__c.hed__Course_Offering__c=:className)]){
        roster.add(Name);
        for(Course_Attendance__c attendance : [SELECT Attendance_Record__c FROM Course_Attendance__c WHERE Date__c =:dat AND Contact__c =:name.id]){
           record = attendance.Attendance_Record__c;
           record = record.toLowerCase();
           attendanceRecord.add(record);
           roster.add(new string attendanceRecord);
        }
    }
  • public List<String> attendanceRecordObj = new List<String>(); public List<String> getAttendanceRecord(){ return attendanceRecordObj; } Something like this? – alutz Oct 11 '17 at 16:16
  • editited the response, try this – Hemant Jain Oct 11 '17 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.