+ Start a Discussion
Sam1980Sam1980 

Visualforce Error - System.NullPointerExeption

Not sure why i am getting this error, please help

System.NullPointerExeption: Attempted to upsert a null list
Error s in expressoin {!save} in component < apex: commandButton> in page oladdclient: Class.OLaddcientctrl.save:Line 50, column 1


Here is the Apex code


<apex:page controller="OLaddClientctrl">
   <script>
            function CloseChaiData(){
                document.getElementById("ChaiDiv").style.display = 'none'; 
               document.getElementById('ClientDiv').style.display='';
               return false;
            }
            function ShowChaiCaseData(){
                document.getElementById("ChaiDiv").style.display = 'none'; 
                document.getElementById('ChaiCaseDiv').style.display='';
               return false;
            }
</script>
<apex:pageMessages ></apex:pageMessages>
  <apex:form >
      <apex:pageBlock >
      <div id="ClientDiv" style="display: none;">
      <apex:pageblockSection title="Client Basic Information">
                <apex:inputField value="{!san.Name}" />
                <apex:inputField value="{!san.First_Name__c}" />
                <apex:inputField value="{!san.Middle_Name__c}" />
                <apex:inputField value="{!san.Last_Name__c}" />
                <apex:inputField value="{!san.Gender__c}" />
                <apex:inputField value="{!san.Home_Phone_Number__c}" />
                <apex:inputField value="{!san.Home_Street_Address__c}" />
                <apex:inputField value="{!san.Home_City__c}" />
                <apex:inputField value="{!san.Home_State__c}" />
            <apex:inputField value="{!san.Home_Zip_Code__c}" />
            <apex:inputField value="{!san.Work_Phone_Number__c}" />

</apex:pageblockSection>
</div>
      
          <div id="ChaiDiv">
           
            <apex:pageBlockSection title="ADD/VIEW CHAI DATA" >
            <apex:inputField value="{!chai.SANClientID__c}" />
            <apex:inputField value="{!chai.Spouse_Immigration_Status__c}" />
            <apex:inputField value="{!chai.Spouse_First_name__c}" />
            <apex:inputField value="{!chai.Spouse_Health_Insurance__c}" />
            <apex:inputField value="{!chai.Spouse_Last_name__c}" />
            <apex:inputField value="{!chai.Type_of_Health_Insurance__c}" />
            <apex:inputField value="{!chai.Spouse_Gender__c}" />
            <apex:inputField value="{!chai.Spouse_last_Dr_Visit__c}" />
            <apex:inputField value="{!chai.Spouse_DOB__c}" />
            <apex:inputField value="{!chai.Spouse_reason_for_last_Dr_Visit__c}" />
            <apex:inputField value="{!chai.Spouse_Primary_Language__c}" />
            <apex:inputField value="{!chai.Spouse_Education__c}" />
            <apex:inputField value="{!chai.Income_per_month__c}" />
            
            <apex:inputField value="{!chai.Spouse_Income_per_month__c}" />
            <apex:inputField value="{!chai.Spouse_Yrs_in_US__c}" />
            <apex:inputField value="{!chai.Total_Household_Income__c}" />
            <apex:inputField value="{!chai.Spouse_Ethnicity__c}" />
           </apex:pageBlockSection>
           <apex:pageBlockSection title="Information">
            <apex:inputField value="{!chai.Last_Doctors_Appointment__c}" />
            <apex:inputField value="{!chai.Health_Insurance__c}" />
            <apex:inputField value="{!chai.Reason_for_last_Dr_Visit__c}" />
            <apex:inputField value="{!chai.Type_of_Health_Insurance__c}" />
            
            </apex:pageBlockSection>
         </div>
         
         <div id="ChaiCaseDiv" style="display: none;">
            <apex:pageBlockSection title="Update,Open or View a Case File">
            <apex:inputField value="{!chaicase.SANClientID__c}" />    
            <apex:inputField value="{!chaicase.Case_Open_Date__c}" />
            <apex:inputField value="{!chaicase.Case_Worker__c}" />
            <apex:inputField value="{!chaicase.Next_Follow_Up_Date__c}" />
            <apex:inputField value="{!chaicase.Current_Case_Status__c}" />
             <apex:inputField value="{!chaicase.Case_Close_Date__c}" />
             <apex:inputField value="{!chaicase.Case_Notes__c}" />
               
            </apex:pageBlockSection>
        </div>
        
         <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}"/>
            <apex:commandButton value="CHAI Cases" onclick="ShowChaiCaseData(); return false;"/>
            <apex:commandButton value="Close Form" action="{!GoTOClientData}"/>
         </apex:pageBlockButtons>
      </apex:pageBlock>
  </apex:form>
</apex:page>















public class OLaddClientctrl 
{
    public SAN_Client__c san{get;set;}
    public ID sID{get;set;}
    public CHAI__c chai{get;set;}
    public CHAI_Case__c chaicase{get;set;}
    private void addError (string msg) 
   {
    ApexPages.addMessage (new ApexPages.Message(ApexPages.Severity.ERROR,msg));
    }
    
    public OLaddClientctrl ()
{
        san= [select        id, Name, First_Name__c, Middle_Name__c, Last_Name__c, Gender__c,  Home_Street_Address__c, Home_City__c,Home_State__c,
        Home_Phone_Number__c,Home_Zip_Code__c,Work_Phone_Number__c from 
        SAN_Client__c where id =: ApexPages.currentPage().getParameters().get('id')];     
            try
              {     
                    chai = [select id, SANClientID__c, Spouse_Immigration_Status__c, Spouse_First_name__c, Spouse_Health_Insurance__c, Spouse_Last_name__c,
Spouse_Gender__c, Spouse_last_Dr_Visit__c, Spouse_DOB__c, Spouse_reason_for_last_Dr_Visit__c,Spouse_Primary_Language__c,Spouse_Education__c,              Income_per_month__c,Spouse_Income_per_month__c,Spouse_Yrs_in_US__c,Total_Household_Income__c,Spouse_Ethnicity__c,Last_Doctors_Appointment__c,
 Health_Insurance__c,Reason_for_last_Dr_Visit__c,Type_of_Health_Insurance__c from 
                    CHAI__c where SANClientID__c =: ApexPages.currentPage().getParameters().get('id')];
                    chaicase = [select id, SANClientID__c, Case_Open_Date__c, Case_Worker__c, Next_Follow_Up_Date__c, Current_Case_Status__c,
                    Case_Close_Date__c,Case_Notes__c from 
                    CHAI_Case__c where SANClientID__c =: ApexPages.currentPage().getParameters().get('id')];
              }
              catch(Exception e)
              {
                ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'No Record Found' );
                ApexPages.addmessage(msg);       
              }
    } 
    public SAN_Client__c getsan(){
    return san;
    }
    public PageReference save() {
        try
        {
        upsert san;
        upsert chai;
        upsert chaicase;
        }
        catch(DmlException e)
        {
           System.debug('The following exception has occurred: ' + e.getMessage());
           addError ('Could not save changes: '  + e.getMessage());  
        }
        return new pageReference('/apex/oldAddClient?id='+ApexPages.currentPage().getParameters().get('id'));
    }
    
    public PageReference GoTOClientData() {
        PageReference pref = new ApexPages.StandardController(san).view();
        return pref;
    }
    
    
}

Ankit AroraAnkit Arora
You've to debug your code that why they are not returning any results in list, for now you can change your code like this to avoid errors :

if(san.size() > 0)
upsert san;
if(chai.size() > 0)
upsert chai;
if(chaicase.size() > 0)
upsert chaicase;