You need to sign in to do that
Don't have an account?
surya teja 46
how to avoid system.nullpointer exception in my code
<apex:page controller="onaccountdata">
<apex:form id="out">
<apex:pageBlock title="Account">
<apex:pageBlockSection >
<apex:inputtext label="Account Name" value="{!enteredname}"/>
<apex:commandButton value="Click" action="{!getcontacts}" reRender="out"/>
<apex:inputtext label="Industry" value="{!industry}"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:outputPanel id="e">{!error}</apex:outputPanel>
<apex:pageBlock title="Contacts">
<apex:commandButton value="save" action="{!customsave}" reRender="e"/>
<apex:pageblockSection >
<apex:inputtext label="First Name" value="{!firstname}"/>
<apex:inputtext label="Last Name" value="{!lastname}"/>
<apex:inputtext label="Phone" value="{!phone}"/>
<apex:inputtext label="Email" value="{!email}"/>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
================================================================================================
public class onaccountdata
{
public onaccountdata() {
}
public onaccountdata(ApexPages.StandardController controller) {
}
public string enteredname{get; set;}
public string industry {get; set;}
public string firstname{get; set;}
public string lastname{get; set;}
public string phone{get; set;}
public string email{get; set;}
public exception error{get; set;}
public contact con{get;set;}
public void getcontacts()
{
account acc = [select id, name, industry from account where name =: enteredname limit 1];
con = [select id, firstname, lastname, phone, email from contact where accountid =: acc.id limit 1];
industry = acc.industry;
firstname = con.firstname;
lastname = con.lastname;
phone = con.phone;
email = con.email;
}
public void customsave()
{
if(con.id != null && con.id != '')
{
con.firstname = firstname;
con.lastname = lastname;
con.phone = phone;
con.email = email;
try
{
update con;
}
catch (exception ex)
{
error = ex;
}
}
else
{
contact con = new contact();
con.firstname = firstname;
con.lastname = lastname;
con.phone = phone;
con.email = email;
try
{
insert con;
}
catch (exception ex)
{
error = ex;
}
}
}
}
<apex:form id="out">
<apex:pageBlock title="Account">
<apex:pageBlockSection >
<apex:inputtext label="Account Name" value="{!enteredname}"/>
<apex:commandButton value="Click" action="{!getcontacts}" reRender="out"/>
<apex:inputtext label="Industry" value="{!industry}"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:outputPanel id="e">{!error}</apex:outputPanel>
<apex:pageBlock title="Contacts">
<apex:commandButton value="save" action="{!customsave}" reRender="e"/>
<apex:pageblockSection >
<apex:inputtext label="First Name" value="{!firstname}"/>
<apex:inputtext label="Last Name" value="{!lastname}"/>
<apex:inputtext label="Phone" value="{!phone}"/>
<apex:inputtext label="Email" value="{!email}"/>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
================================================================================================
public class onaccountdata
{
public onaccountdata() {
}
public onaccountdata(ApexPages.StandardController controller) {
}
public string enteredname{get; set;}
public string industry {get; set;}
public string firstname{get; set;}
public string lastname{get; set;}
public string phone{get; set;}
public string email{get; set;}
public exception error{get; set;}
public contact con{get;set;}
public void getcontacts()
{
account acc = [select id, name, industry from account where name =: enteredname limit 1];
con = [select id, firstname, lastname, phone, email from contact where accountid =: acc.id limit 1];
industry = acc.industry;
firstname = con.firstname;
lastname = con.lastname;
phone = con.phone;
email = con.email;
}
public void customsave()
{
if(con.id != null && con.id != '')
{
con.firstname = firstname;
con.lastname = lastname;
con.phone = phone;
con.email = email;
try
{
update con;
}
catch (exception ex)
{
error = ex;
}
}
else
{
contact con = new contact();
con.firstname = firstname;
con.lastname = lastname;
con.phone = phone;
con.email = email;
try
{
insert con;
}
catch (exception ex)
{
error = ex;
}
}
}
}
You can try the above code sample. Let me know if the provided code sample solves the issue. Always remember that querying, inserting or updating an object is best done when done through a collection rather doing it with an object. Since you are manually typing the name of the account and getting the related contacts and industry check if the account exists if not that can also throw null pointer exception