You need to sign in to do that
Don't have an account?
Rajesh 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
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
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
Thanks,
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 .
Hope this helps.
I was not instantiating Student__c object inside no urgement constructor .
Public WrapStudent ()
{
std = new Student__c();
}