+ Start a Discussion

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

//no. of rows added/records in the inner class list
public Integer count = 1;

////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++)


lstcontact .add(lstInner[j].acct);
upsert lstcontact;
return pr;

//add one more row
public void Add()
count = count+1;
public void addMoreconst()
//call to the iner class constructor
innerClass objInnerClass = new innerClass(count);
List<Contact> lstCon = getContacts();

for(Contact c: lstCon){
objInnerClass = new innerClass(count);
objInnerClass.acct = c;
//add the record to the inner class list

}/* 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
}/* end addMore*/

/* begin delete */
public void Del()
system.debug('selected row index---->'+selectedRowIndex);
count = count - 1;

}/*End del*/

public MultiAdd(ApexPages.StandardController ctlr)

lstInner = new List<innerClass>();
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

public contact acct

/*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: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: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:panelGrid title="SPD" >
<apex:facet name="header">LastName</apex:facet>
<apex:inputfield value="{!e1.acct.LastName}"/>

<apex:panelGrid >
<apex:facet name="header">FirstName</apex:facet>
<apex:inputfield value="{!e1.acct.FirstName}"/>

<apex:panelGrid >
<apex:facet name="header">TotalNumber</apex:facet>
<apex:inputfield value="{!e1.acct.TotalNumber__c}"/>

<apex:panelGrid >
<apex:facet name="header">DoctorDescription</apex:facet>
<apex:inputfield value="{!e1.acct.DoctorDescription__c}"/>





Pl help me out





Saravanan @CreationSaravanan @Creation



Can you change the line 51


List<Contact> lstCon = getContacts();  




List<Contact> lstCon =new List<Contact>();



like this.




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


(Contact:{Name=Ashley TestVf22, AccountId=0019000000OmGsCAAV, FirstName=Ashley, Id=0039000000P7oI9AAJ, LastName=TestVf22}, Contact:{Name=TestVRMR23 TestVf22, AccountId=0019000000OmGsCAAV, FirstName=TestVRMR23, Id=0039000000lLjYUAA0, LastName=TestVf22}, Contact:{Name=TestVFMR22 TestVf22, AccountId=0019000000OmGsCAAV, FirstName=TestVFMR22, Id=0039000000lLjYyAAK, LastName=TestVf22, testvantage__TotalNumber__c=1234})









try below code...

List<Contact> lstCon = getContacts();



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.

Cool Sfdc

If i write the above code the contact is not getting associated to Account.

pl help me out how to write the code
HI Rockz,

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
Ashok S 7Ashok S 7
when i am writing the test class for a controller then following error is comming
System.NullPointerException: Attempt to de-reference a null object 
here is my test class
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.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;
     //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
     List<Apexpages.Message> pageMessages = ApexPages.getMessages();