+ Start a Discussion
Navneeth RajNavneeth Raj 

System.NullPointerException: Attempt to de-reference a null object ERROR for CustomSettings ListExample

Example
User-added image
APEXClass

public class CustomSettingsExample {
    public String city {get;set;}
    public String state {set;get;}
    public String code {get;set;}
    
    public void getData(){
        zipcodes__c zip=zipcodes__c.getValues(city);
        state=zip.state__c;
        city=zip.city__c;
        code=zip.code__c;        
    }
}
Related Vfp
<apex:page controller="CustomSettingsExample">
    <apex:form >
    <apex:pageblock title="Table" id="ONE">
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="city"/>
                <apex:inputText value="{!city}">
                    <apex:actionSupport event="onchange" action="{!getData}"  reRender="ONE"/>
                </apex:inputText>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="state"/>
                <apex:inputText value="{!state}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="code"/>
                <apex:inputText value="{!code}"/>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        </apex:pageblock>
            </apex:form>
</apex:page>

ERROR
User-added image
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you
public class CustomSettingsExample {
    public String city {get;set;}
    public String state {set;get;}
    public String code {get;set;}
    
    public void getData()
	{
       // zipcodes__c zip=zipcodes__c.getValues(city);

	   Map<String, zipcodes__c> zipMap = zipcodes__c.getAll();
        
		if( city != null && zipMap.containsKey(city) != null)
		{
			state = zipMap.get(city).state__c;
			city = zipMap.get(city).city__c;
			code = zipMap.get(city).code__c;
		}		
    }
}

Please let us know if this will help u

Thanks
Amit Chaudhary