1

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.
2
  • I think something goes wrong with the way you parse the json and that causes the rl list to be null in the loop. I think part of the trouble might be that the the json contains a unique object for each event but that's a guess. It would be appreciated if you'd place a comments in your apex code at the line number where the error is. That way it's easy for others to see here. We unfortunately can't see line numbers on stack exchange.
    – Kasper
    Aug 10, 2018 at 21:55
  • 1
    Please update your question to include the JSON you are trying to parse.
    – gNerb
    Aug 10, 2018 at 22:32

1 Answer 1

2

For better or for worse I was able to get a copy of the JSON by visiting your endpoint.

{
  "events":{

    "smtc-event-13753": {"title":"Mobile World Congress Americas",
      "link":"https://www.mwcamericas.com",
      "event_date":"September 12-14, 2018",
      "description":"",
      "location":"Los Angeles Convention Center",
      "categories":"LoRa Wireless RF, LoRa Wireless RF for the IoT, LoRa Applications, LoRa General Event",
      "pubdate":"1536764400"
    },
    "smtc-event-13755": {"title":"Express Logistics and Supply Chain Conference",
      "link":"https://10times.com/fmcg-supply-chain",
      "event_date":"October 4-5, 2018",
      "description":"",
      "location":"Taj Lands End, Mumbai, India",
      "categories":"LoRa Wireless RF, LoRa Wireless RF for the IoT, LoRa Applications, LoRa General Event",
      "pubdate":"1538654400"
    },
    ...
}

Overview

I think the biggest issue you made was that you named your member items when the deserializer was expecting a member named events. This would cause it to skip the events member and it would put a null value into your items member. You also used a list but the JSON returns an object, not an array, which means even if you got the name correct, you would have had a type conversion error.

Fix

Here is a bit of an in-depth explanation of dynamic JSON in SF.

In your case, you'll want to define your class as such:

public class ResCls{
    public Map<String, Event> events {get; set;}

    public ResCls() {}
}

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

    public Event () {}
}

Then you can serialize like this:

ResCls result = (ResCls) JSON.deserialize(response.getBody());

Loop:

for (Event e : result.events.values()) {/* Do Stuff */}

This works because you won't necessarily know the name of the events or how many you will get but you will know what properties the events will have. The name of the event will be the key in the map and the event object will be it's value.

Note: Event may or may not be a reserved word so consider changing the class name to be something more specific to your use case.

VisualForce

To Display it on the VF page

 <apex:pageBlockTable value="{!result}" var="key">
     <apex:column headerValue="Title"><apex:outputText value="{!result[key].title}"/></apex:column>
     <apex:column headerValue="URL"><apex:outputText value="{!result[key].link}"/></apex:column>
     <apex:column headerValue="Date"><apex:outputText value="{!result[key].event_date}"/></apex:column>
     <apex:column headerValue="Location"><apex:outputText value="{!result[key].location}"/></apex:column>
 </apex:pageBlockTable>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.