You need to sign in to do that
Don't have an account?
System.NullPointerException: Attempt to de-reference a null object
Hi Everyone,
getting the following error
Class.testvantage.MultiAdd.addMoreconst: line 51, column 1
Class.testvantage.MultiAdd.<init>: line 90, column 1
I wrote the Apex class for add and deleting the records in VF page.
When i tried to the save the page i get the following error.
###APEX CLASS############
public class MultiAdd
{
public List<contact> lstcontact = new List<contact>();
public List<Contact> contacts;
public ApexPages.StandardController std;
//list of the inner class
public List<innerClass> lstInner
{ get;set; }
//will indicate the row to be deleted
public String selectedRowIndex
{get;set;}
//no. of rows added/records in the inner class list
public Integer count = 1;
//{get;set;}
////save the records by adding the elements in the inner class list to lstAcct,return to the same page
public PageReference Save()
{
PageReference pr = new PageReference('/apex/jqueryDoctorProfile');
for(Integer j = 0;j<lstInner.size();j++)
{
system.debug('123456789@@@@@@@@@@@@@@@@'+lstInner.size());
lstcontact .add(lstInner[j].acct);
}
system.debug('lstcontactupsert------------------------>'+lstcontact);
upsert lstcontact;
pr.setRedirect(True);
return pr;
}
//add one more row
public void Add()
{
count = count+1;
addMore();
}
public void addMoreconst()
{
//call to the iner class constructor
innerClass objInnerClass = new innerClass(count);
List<Contact> lstCon = getContacts();
system.debug('totalconatctlsit'+lstCon.Size());
for(Contact c: lstCon){
objInnerClass = new innerClass(count);
objInnerClass.acct = c;
lstInner.add(objInnerClass);
}
//add the record to the inner class list
system.debug('lstInner---->'+lstInner);
}/* end addMore*/
/*Begin addMore*/
public void addMore()
{
//call to the iner class constructor
innerClass objInnerClass = new innerClass(count);
//add the record to the inner class list
lstInner.add(objInnerClass);
system.debug('lstInner---->'+lstInner);
}/* end addMore*/
/* begin delete */
public void Del()
{
system.debug('selected row index---->'+selectedRowIndex);
lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
count = count - 1;
}/*End del*/
/*Constructor*/
public MultiAdd(ApexPages.StandardController ctlr)
{
std=ctlr;
lstInner = new List<innerClass>();
addMoreconst();
selectedRowIndex = '0';
}/*End Constructor*/
public Account getAccount()
{
return (Account) std.getRecord();
}
/*Inner Class*/
public class innerClass
{
/*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */
public String recCount
{get;set;}
public contact acct
{get;set;}
/*Inner Class Constructor*/
public innerClass(Integer intCount)
{
recCount = String.valueOf(intCount);
/*create a new account*/
acct = new contact();
}/*End Inner class Constructor*/
}/*End inner Class*/
public List<Contact> getContacts()
{ if ( (null!=getAccount().id) && (contacts == null) )
{
contacts=[SELECT Id, Name, testvantage__TotalNumber__c , testvantage__DoctorDescription__c, LastName, FirstName FROM Contact WHERE AccountId = : getAccount().ID ORDER BY CreatedDate];
} return contacts;
}
}/*End Class*
/########VF PAGE##############
<apex:page StandardController="Account" extensions="MultiAdd" id="thePage">
<apex:form >
<apex:pageblock id="pb" >
<apex:pageBlockButtons >
<apex:commandbutton value="Add" action="{!Add}" rerender="pb1"/>
<apex:commandbutton value="Save" action="{!Save}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Account Details" collapsible="true" id="mainRecord" columns="2" >
<apex:inputField value="{!Account.Name}"/>
<apex:inputField value="{!Account.Type}"/>
<apex:inputField value="{!Account.BillingStreet}"/>
<apex:inputField value="{!Account.Industry}"/>
<apex:inputField value="{!Account.Phone}"/>
</apex:pageBlockSection>
<apex:pageblock id="pb1">
<apex:repeat value="{!lstInner}" var="e1" id="therepeat">
<apex:panelGrid columns="6">
<apex:panelGrid headerClass="Name">
<apex:facet name="header">Del</apex:facet>
<apex:commandButton value="X" action="{!Del}" rerender="pb1">
<apex:param name="rowToBeDeleted" value="{!e1.recCount}" assignTo="{!selectedRowIndex}"></apex:param>
</apex:commandButton>
</apex:panelGrid>
<apex:panelGrid title="SPD" >
<apex:facet name="header">LastName</apex:facet>
<apex:inputfield value="{!e1.acct.LastName}"/>
</apex:panelGrid>
<apex:panelGrid >
<apex:facet name="header">FirstName</apex:facet>
<apex:inputfield value="{!e1.acct.FirstName}"/>
</apex:panelGrid>
<apex:panelGrid >
<apex:facet name="header">TotalNumber</apex:facet>
<apex:inputfield value="{!e1.acct.TotalNumber__c}"/>
</apex:panelGrid>
<apex:panelGrid >
<apex:facet name="header">DoctorDescription</apex:facet>
<apex:inputfield value="{!e1.acct.DoctorDescription__c}"/>
</apex:panelGrid>
</apex:panelgrid>
</apex:repeat>
</apex:pageBlock>
</apex:pageblock>
</apex:form>
</apex:page>
Pl help me out
Regards
Sailer
Hi,
Can you change the line 51
List<Contact> lstCon = getContacts();
To
List<Contact> lstCon =new List<Contact>();
lstCon=getContacts();
like this.
Hi,
I have added that but i was not able to solve the problem.
When i use the System.Debug(); system.debug('totalconatctlsit'+lstCon.Size()); I get the count.of related contact for that account.
Can you please check my constructor if am missing any please let me know.
This is the Debug log
Regards
Sailer
Hi,
try below code...
List<Contact> lstCon = getContacts();
To
List<Contact> lstCon = [SELECT ID,name from Contact limit 1];
Please accept my answer as a solution if my solution was helpful. This will make it available to others as a proper answer. If you felt that I went above and beyond please give me Kudos by clicking on on the star icon.
Thanks,
Cool Sfdc
pl help me out how to write the code
The records get updated even with that error ,but new records are not getting inserted with the same error.Can u please help me to solve
System.NullPointerException: Attempt to de-reference a null object
here is my test class
@istest
public class allPatntsRcrdsCtrlr_Test
{
public static testmethod void test()
{
//inserting a record into the Account object
Account acc = new Account();
acc.Name = 'Sample Account';
insert acc;
//Inserting a Record into contact object
Contact c = new Contact();
c.Accountid = acc.id;
c.Department = 'Testing';
c.LastName = 'Test';
insert c;
//inserting a record into the Paitent object
Patient__c pt = new Patient__c();
pt.BlueStarID__c = 'fdsrt1224565';
pt.Name = 'Sample';
pt.Patient_First_Name__c = 'Test';
pt.Patient_Id__c= 'sder45789';
pt.Patient_Last_Name__c='Sample1';
pt.ExternalID__c= 'sader22244578';
pt.ProviderName__c= '';
pt.SRType__c= 'Yes';
pt.SampleCode__c= 'No';
pt.ServiceRequestID__c= 12456789;
pt.UserID__c= 2456789;
insert pt;
//calling a pagereference in testclass
PageReference pg = page.allPatientRecords;
pg.getparameters().put('id',string.valueOf(pt.id));
test.setcurrentpage(pg);
//calling the controller name
allPatntsRcrdsCtrlr aprc = new allPatntsRcrdsCtrlr();
//calling wrapperclass in test class
allPatntsRcrdsCtrlr.oAuthRecords ar = new allPatntsRcrdsCtrlr.oAuthRecords();
//aprc.patlist = pt;
aprc.strBody = 'Plain';
aprc.idv = 'Test';
aprc.searchVal = '';
aprc.searchFields = '';
aprc.show = true;
aprc.lstrecdstb1 = false;
aprc.queryflds = true;
aprc.pdfrendered = true;
aprc.showmessage = true;
aprc.wordRendered = true;
aprc.xlRendered = true;
//calling methods in testcalss
aprc.clear();
//aprc.patinfo();
//aprc.PatientData();
aprc.retrieve();
//aprc.setsearchFields(Name);
aprc.doSearch();
//aprc.ExpXL();
aprc.ExpPDF();
aprc.ExpWord();
List<Apexpages.Message> pageMessages = ApexPages.getMessages();
}
}