+ Start a Discussion
Gopikrishnan NedumparambumanaGopikrishnan Nedumparambumana 

JSON content display : Attempt to de-reference a null object

Hello,

I am trying to display content from a JSON feed on my Salesforce page using APEX/visualforce page. From information gathered, I am able to get the details of the JSON on variable. But when running the visualforce page, I get the below error. Any help much appreciated..

Apex Code 
global class loraGeneralEventsController {

	public String title{get;set;}
	public String link{get;set;}
	public String event_date{get;set;}
	public location location{get;set;}
	public List<Items> dr{get;set;}

	public loraGeneralEventsController(){

	}

	public void requestM(){

		String url = 'https://www.semtech.com/company/events-json/lora-general-event';

		HttpRequest req = new HttpRequest();
		req.setEndpoint(url);
		req.setMethod('GET');

		Http http = new Http();
		HttpResponse res = http.send(req);
		String responseBody = res.getBody();
		system.debug('JSON Content : ' + responseBody);
		
		ResCls resItem = (ResCls)JSON.deserialize(responseBody, ResCls.class);
		List<Items> rl = resItem.items;
		system.debug('Item List : ' + resItem.items);
		dr = new List<Items>();
		for(Items it:rl){
			system.debug('Item Name: ' + it);
			title = it.title;
			link = it.link;
			system.debug('event_date: ' + it.event_date);
			system.debug('location: ' + it.location);
			dr.add(it);
		}   
	   system.debug('List: ' + dr);
	}

	public class ResCls{
		List<Items> items;
	}
		
	public class Items{

		public String title {get;set;}
		public String link {get;set;}
		public String event_date {get;set;}
		public String description {get;set;}
		public String location {get;set;}
		public String categories {get;set;}
		public String pubdate {get;set;}
	}


}

Visual force page :
<apex:page Controller="loraGeneralEventsController" action="{!requestM}" sidebar="false">
    <apex:pageBlock>
        <apex:pageblockSection >
            <apex:pageblockTable value="{!dr}" var="dd">
                    <apex:column headerValue="Title"><apex:outputText value="{!dd.title}"/></apex:column>
                    <apex:column headerValue="URL"><apex:outputText value="{!dd.link}"/></apex:column>
                    <apex:column headerValue="Date"><apex:outputText value="{!dd.event_date}"/></apex:column>
                    <apex:column headerValue="Location"><apex:outputText value="{!dd.location}"/></apex:column>
            </apex:pageblockTable>
        </apex:pageblockSection>
    </apex:pageBlock>
</apex:page>

Error that I get when runnig the page is 
 
Attempt to de-reference a null object
Error is in expression '{!requestM}' in component <apex:page> in page lorageneralevents: Class.loraGeneralEventsController.requestM: line 30, column 1

An unexpected error has occurred. Your development organization has been notified.

 

 
Best Answer chosen by Gopikrishnan Nedumparambumana
Raj VakatiRaj Vakati
Use this code
 
global class loraGeneralEventsController {
    
    public String title{get;set;}
    public String link{get;set;}
    public String event_date{get;set;}
    public location location{get;set;}
    public List<Items> dr{get;set;}
    
    public loraGeneralEventsController(){
        
    }
    
    public void requestM(){
        
        String url = 'https://www.semtech.com/company/events-json/lora-general-event';
        
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
        
        Http http = new Http();
        HttpResponse res = http.send(req);
        String responseBody = res.getBody();
        dr = new List<Items>();
        Map<String, Object> o = (Map<String, Object>) JSON.deserializeUntyped(responseBody);
        for( String s : o.keyset()){
            Map<String, Object> a  =(  Map<String, Object> )o.get(s);
            for (String s1 :a.keySet()){
                
                Map<String, Object> o12 =(Map<String, Object>)a.get(s1);
                System.debug('o12'+a);
                Items i = new Items();
               i.title = (String)o12.get('title');
               i.link=  (String)o12.get('link');
                dr.add(i);
            }
            // System.debug('o12'+a);
        }
        
        
        
        system.debug('List: ' + dr);
    }
    
    
    
    public class Items{
        
        public String title {get;set;}
        public String link {get;set;}
        public String event_date {get;set;}
        public String description {get;set;}
        public String location {get;set;}
        public String categories {get;set;}
        public String pubdate {get;set;}
    }
    
    
}

 

All Answers

Raj VakatiRaj Vakati
Use this code
 
global class loraGeneralEventsController {
    
    public String title{get;set;}
    public String link{get;set;}
    public String event_date{get;set;}
    public location location{get;set;}
    public List<Items> dr{get;set;}
    
    public loraGeneralEventsController(){
        
    }
    
    public void requestM(){
        
        String url = 'https://www.semtech.com/company/events-json/lora-general-event';
        
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
        
        Http http = new Http();
        HttpResponse res = http.send(req);
        String responseBody = res.getBody();
        dr = new List<Items>();
        Map<String, Object> o = (Map<String, Object>) JSON.deserializeUntyped(responseBody);
        for( String s : o.keyset()){
            Map<String, Object> a  =(  Map<String, Object> )o.get(s);
            for (String s1 :a.keySet()){
                
                Map<String, Object> o12 =(Map<String, Object>)a.get(s1);
                System.debug('o12'+a);
                Items i = new Items();
               i.title = (String)o12.get('title');
               i.link=  (String)o12.get('link');
                dr.add(i);
            }
            // System.debug('o12'+a);
        }
        
        
        
        system.debug('List: ' + dr);
    }
    
    
    
    public class Items{
        
        public String title {get;set;}
        public String link {get;set;}
        public String event_date {get;set;}
        public String description {get;set;}
        public String location {get;set;}
        public String categories {get;set;}
        public String pubdate {get;set;}
    }
    
    
}

 
This was selected as the best answer
Raj VakatiRaj Vakati
global class loraGeneralEventsController {
    
    public String title{get;set;}
    public String link{get;set;}
    public String event_date{get;set;}
    public location location{get;set;}
    public List<Items> dr{get;set;}
    
    public loraGeneralEventsController(){
        
    }
    
    public void requestM(){
        
        String url = 'https://www.semtech.com/company/events-json/lora-general-event';
        
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
        
        Http http = new Http();
        HttpResponse res = http.send(req);
        String responseBody = res.getBody();
        dr = new List<Items>();
        Map<String, Object> o = (Map<String, Object>) JSON.deserializeUntyped(responseBody);
        for( String s : o.keyset()){
            Map<String, Object> a  =(  Map<String, Object> )o.get(s);
            for (String s1 :a.keySet()){
                
                Map<String, Object> o12 =(Map<String, Object>)a.get(s1);
                System.debug('o12'+a);
                Items i = new Items();
                i.title = (String)o12.get('title');
                i.link=  (String)o12.get('link');
                i.pubdate=  (String)o12.get('pubdate');
                
                i.location=  (String)o12.get('location');
                
                dr.add(i);
            }
            // System.debug('o12'+a);
        }
        
        
        
        system.debug('List: ' + dr);
    }
    
    
    
    public class Items{
        
        public String title {get;set;}
        public String link {get;set;}
        public String event_date {get;set;}
        public String description {get;set;}
        public String location {get;set;}
        public String categories {get;set;}
        public String pubdate {get;set;}
    }
    
    
}