+ Start a Discussion
surya teja 46surya 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;
            }
        }
    }
}​
bharath kumar 52bharath kumar 52
public void getcontacts()
    {
 try {
        account acc = [select id, name, industry from account where name =: enteredname   limit 1]; // make it a list instead of assigning querying data and assigning it to an object List<Account> acc =[Your query];
        con = [select id, firstname, lastname, phone, email from contact where accountid =: acc.id limit 1]; //Same for this as well
        industry = acc.industry;
        firstname = con.firstname;
        lastname = con.lastname;
        phone = con.phone;
        email = con.email;
    }
catch(Exception e){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage());
ApexPages.addMessage(myMsg);

}
}
Hi Surya,

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