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
Rajesh Kumar 455Rajesh Kumar 455 

Issue with Visualforce Grid Save Button

I am facing issue with Custom Visualforce Grid During Save .
When i click Save Changes Button I am getting below error 

**System.ListException: DML statement found null SObject at position 3
Error is in expression '{!Save1}' in component <apex:commandButton> in page r_gridpage: Class.R_GridClass.Save1: line 24, column 1
Class.R_GridClass.Save1: line 24, column 1** 


Below is my visualforce and Apex codes 

Visualforce Code
----------------

<apex:page standardController="Student__c" extensions="R_GridClass">  
    <apex:form id="formId">
        <apex:pageBlock >
        <apex:pageBlockButtons >
        <apex:commandButton value="Delete Selected Item" action="{!DeleteSelected}"/> 
        <apex:commandButton value="Add Student" action="{!AddSelected}"/> 
        <apex:commandButton value="Save Changes" action="{!Save1}"/>
        <apex:commandButton value="Update Selected Row" action="{!UpdateSelected}"/> 
        </apex:pageBlockButtons>
        <apex:pageBlockTable value="{!studentList}" var="st">
            <apex:column headerValue="Selection">
            <apex:inputCheckbox value="{!st.Selected}"/>
            </apex:column>
                <apex:column headerValue="Name">
                    <apex:inputField value="{!st.std.First_Name__c}" />
                </apex:column>
                <apex:column headerValue="Mobile">
                    <apex:inputField value="{!st.std.Mobile__c}" />
                </apex:column>
                <apex:column headerValue="Email">
                    <apex:inputField value="{!st.std.Email__c}" />
                </apex:column>
                <apex:column headerValue="Occupation">
                    <apex:inputField value="{!st.std.Occupation__c}" />
                </apex:column>
                <apex:column headerValue="DOJ">
                    <apex:inputField value="{!st.std.Date_of_Joining__c}" />
                </apex:column>
            </apex:pageBlockTable>
          <apex:pageBlockButtons >
         <!--<apex:commandButton value="Add New Student" action="{!addNew}"/><br/>-->
         <!--<apex:commandButton value="Save Records" action="{!Save}"/> --->
    </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Apex Code
----------------

public class R_GridClass {
Public List<WrapStudent> studentList {get;set;}
    public R_GridClass(ApexPages.StandardController ctrlr)
    {
      studentList = new List<WrapStudent>();
        for (Student__c stud :[SELECT Id, Name, OwnerId, First_Name__c, Last_Name__c, Mobile__c, Email__c, Date_of_Joining__c, Occupation__c FROM Student__c])
        {
           studentList.add(new WrapStudent(stud)); 
        }
    }
    
    public void AddSelected()
{
   studentList.add(new WrapStudent() );
}  
 public PageReference Save1()
{
List<Student__c> slist = new List<Student__c>();
for (Integer i = 0;i<studentList.size();i++)
{
 slist.add(studentList[i].std );
}
upsert slist;
 System.debug('Size of Selected List is -'+studentList.size());   
   PageReference tempPage = ApexPages.currentPage();            
   tempPage.setRedirect(true);
    return tempPage ;
}  
public PageReference DeleteSelected()
{
    List<Student__c> listToDelete = new List<Student__c>();
    for (WrapStudent wd : studentList)
    {
        if(wd.selected == true)
        {
            listToDelete.add(wd.std);
        }
    }
    if (listToDelete.size()>0)
    {
        
    Delete listToDelete;
        }
   PageReference tempPage = ApexPages.currentPage();            
   tempPage.setRedirect(true);
    return tempPage ;
    
     /*PageReference myVFPage = new PageReference('/'+'a007F000004tfiq');
         return myVFPage;*/

    
    public PageReference UpdateSelected()
{
    List<Student__c> listToUpdate = new List<Student__c>();
    for (WrapStudent wd : studentList)
    {
        if(wd.selected == true)
        {
            listToUpdate.add(wd.std);
        }
    }
    update listToUpdate;
   PageReference tempPage = ApexPages.currentPage();            
   tempPage.setRedirect(true);
    return tempPage ;
    
     /*PageReference myVFPage = new PageReference('/'+'a007F000004tfiq');
         return myVFPage;*/
}  
    
   
    
public class WrapStudent 
{
    
    Public Student__c std {get;set;}
    Public Boolean selected {get;set;} 
    Public WrapStudent (Student__c s)
    {
       std = s;
       selected = false; 
        
    }
    
    Public WrapStudent ()
    {
       
        
    }
}
}



Please help me on the above issue
 
Damon ShawDamon Shaw
Hi Rajesh,

The error message isn't something I would expect to see pointing to a debug line?

I would debug the slist before trying to upsert and then check the size of slist to make sure there are records before trying to upsert them, 

try something like this 
public PageReference Save1()
{
    List<Student__c> slist = new List<Student__c>();
    for (WrapStudent ws :studentList)
    {
        slist.add(ws.std);
    }
    
    System.debug('Size of Selected List is -'+slist.size());
    if(slist.size() > 0)
    {
        upsert slist; 
    }
     
    PageReference tempPage = ApexPages.currentPage();            
    tempPage.setRedirect(true);
    return tempPage;
} 

 
Dushyant SonwarDushyant Sonwar
public PageReference Save1(){
    List<Student__c> slist = new List<Student__c>();
    for (Integer i=0 ; i<studentList.size() ;i++){
      if(studentList[i]!=null && studentList[i].std!= null) { 
          slist.add(studentList[i].std);
      }
    }
    

    System.debug('Size of Selected List is -'+slist.size());
    if(slist.size() > 0){    
        upsert slist; 
    }
     
    PageReference tempPage = ApexPages.currentPage();            
    tempPage.setRedirect(true);
    return tempPage;
}
You have null in your sbject of studentList . Try with above code snippet.
Thanks,
 
Rajesh Kumar 455Rajesh Kumar 455
@Dushyant and Damon : Guys thanks for response but my issue is once i click the Add Student Button it will give me a row to Add and then after filling data in that once i click Save Record Button it should save to Student__c Object data model .

But here for new record after filling tha detail for new data also it is stroing null and i do not know why since wrapper list is get set it should capture the new data but not capturing and storing null value for new data . Your help on this would be highly appreciated .
Dushyant SonwarDushyant Sonwar
Remove or Commente below line in your class. You dont need a empty constructor for this. Your code will work fine if you also remove the below constructor
/*
Public WrapStudent ()
    {
       
        
    }
*/

Hope this helps.
Rajesh Kumar 455Rajesh Kumar 455
It is not working I tried that too
Rajesh Kumar 455Rajesh Kumar 455
Hi I got the issue and i must say it was very silly mistake by me .
I was not instantiating Student__c object inside no urgement constructor .

Public WrapStudent ()
   {
         std = new Student__c();     
}