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
Ethan WestEthan West 

list.add Apex Class error.

Hi All,
 

I have a VisualForce page that should allow me to mass create assigments.
 

I have a couple rows in my VF page that look like such;
 

<apex:pageBlockButtons >
                    <apex:commandButton value="Add Assignment Row" action="{!addAssignment}"/>
                    <apex:commandButton value="Save Assignments" action="{!saveAssignment}"/>
                </apex:pageBlockButtons>
When I try to use the "Add Assignment Row" button it comes with the following error.

"Attempt to de-reference a null object
Error is in expression '{!addAssignment}' in component <apex:commandButton> in page mass_create_assignment: Class.AddmultipleAssignmentController.addAssignment: line 17, column 1"


This refers to my extension within my Apex Class; 
public class AddmultipleAssignmentController {

    public AddmultipleAssignmentController(ApexPages.StandardSetController controller) {

    }

    pse__Assignment__c assignment = new pse__Assignment__c();
    public list<pse__Assignment__c> listAssignment{ get; set; }
    
    public AddmultipleAssignmentController() {
        listAssignment=new list<pse__Assignment__c>();
        listAssignment.add(assignment);
    }
    
    Public void addAssignment() {
        pse__Assignment__c TC = new pse__Assignment__c();
        listAssignment.add(assignment);
    }
    public PageReference saveAssignment() {
        for(Integer i=0; i<listAssignment.size(); i++) {
            insert listAssignment;
        }
        return Page.Allassignmentsaved;
    }
}
In particular L17;C1 - 
listAssignment.add(assignment);
which I have tried as - 
listAssignment.add(TC);
&
listAssignment.add(pse__assignment__c);

(which definitely doesn't work)

Could someone point out what I'm doing wrong within my Apex Class code that's not allowing me to add a row.

Thanks,

Ethan



 

Best Answer chosen by Ethan West
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ethan,

Greetings to you!

This error occurs when your variable (sobject, list, set or any other data type) is not initialized (allocated memory). In order to use the non-primitive data type in the code, we need to initialize the memory first. If we don’t do that it may result in an Attempt to de-reference a null object error.

So, initialize listAssignment in constructor: listAssignment = new List<pse__Assignment__c>();
 
public class AddmultipleAssignmentController {

    public AddmultipleAssignmentController(ApexPages.StandardSetController controller) {
        pse__Assignment__c assignment = new pse__Assignment__c();
        listAssignment=new list<pse__Assignment__c>();
        listAssignment.add(assignment);
    }

    
    public list<pse__Assignment__c> listAssignment{ get; set; }
    
    
    Public void addAssignment() {
        pse__Assignment__c TC = new pse__Assignment__c();
        listAssignment.add(TC);
    }

    public PageReference saveAssignment() {
        for(Integer i=0; i<listAssignment.size(); i++) {
            insert listAssignment;
        }
        return Page.Allassignmentsaved;
    }
}



I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Ethan,

Greetings to you!

This error occurs when your variable (sobject, list, set or any other data type) is not initialized (allocated memory). In order to use the non-primitive data type in the code, we need to initialize the memory first. If we don’t do that it may result in an Attempt to de-reference a null object error.

So, initialize listAssignment in constructor: listAssignment = new List<pse__Assignment__c>();
 
public class AddmultipleAssignmentController {

    public AddmultipleAssignmentController(ApexPages.StandardSetController controller) {
        pse__Assignment__c assignment = new pse__Assignment__c();
        listAssignment=new list<pse__Assignment__c>();
        listAssignment.add(assignment);
    }

    
    public list<pse__Assignment__c> listAssignment{ get; set; }
    
    
    Public void addAssignment() {
        pse__Assignment__c TC = new pse__Assignment__c();
        listAssignment.add(TC);
    }

    public PageReference saveAssignment() {
        for(Integer i=0; i<listAssignment.size(); i++) {
            insert listAssignment;
        }
        return Page.Allassignmentsaved;
    }
}



I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Ethan WestEthan West
Hi Khan, 

Thanks for all of that - didn't realise I needed to initialise the public void section as well! Thanks for the help!

Kind regards,
Ethan