You need to sign in to do that
Don't have an account?
SalesforceLearner
Attempt to de-reference a null object when calling external URL.
I have end point url
http://proxy.abc.com/aproxy/rssFeed.jsp?rssUrl=http://news.abc.com/rss.jkl%3Fsection_id%4F5
when opened it is in the form of XML as shown below which displays news information.
I wrote below code to display on VF page but it is giving "Attempt to de-reference a null object" Error. When I debugged it is throwing error at RSS classDom.XMLNode rss = doc.getRootElement(); I dont no where I am making mistake.
Page :
Controller :
http://proxy.abc.com/aproxy/rssFeed.jsp?rssUrl=http://news.abc.com/rss.jkl%3Fsection_id%4F5
when opened it is in the form of XML as shown below which displays news information.
<title> <![CDATA[ ABCDEFGH ]]> </title> <link> https://news.com </link> <description> <![CDATA[ ]]> </description>
I wrote below code to display on VF page but it is giving "Attempt to de-reference a null object" Error. When I debugged it is throwing error at RSS classDom.XMLNode rss = doc.getRootElement(); I dont no where I am making mistake.
Page :
<apex:page controller="RSSNewsReader" sidebar="false" showHeader="false" cache="false"> <apex:pageBlock id="rssBlock" tabStyle="Lead"> <apex:pageBlockSection title="Channel" columns="2"> <apex:pageBlockSectionItem > <apex:outputLabel value="title" /> <apex:outputText value="{!RSSFeed.title}" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="link" /> <apex:outputText value="{!RSSFeed.link}" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="description" /> <apex:outputText value="{!RSSFeed.description}" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="category" /> <apex:outputText value="{!RSSFeed.category}" /> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:page>
Controller :
public class RSSNewsReader { public String rssQuery {get;set;} private String rssURL {get;set;} public RSSNewsReader() { rssURL = 'http://news.abc.com/rss.jkl%3Fsection_id%4F5'; rssQuery = ''; //default on load } public RSS.channel getRSSFeed() { return RSS.getRSSData(rssURL); } }class:
public class channel { public String title {get;set;} public String link {get;set;} public String description {get;set;} public String author {get;set;} public String category {get;set;} public String copyright {get;set;} public String docs {get;set;} public RSS.image image {get;set;} public list<RSS.item> items {get;set;} public channel() { items = new list<RSS.item>(); } } public class image { public String url {get;set;} public String title {get;set;} public String link {get;set;} } public class item { public String title {get;set;} public String guid {get;set;} public String link {get;set;} public String description {get;set;} public String pubDate {get;set;} public String source {get;set;} public Date getPublishedDate() { Date result = (pubDate != null) ? Date.valueOf(pubDate.replace('T', ' ').replace('Z','')) : null; return result; } public DateTime getPublishedDateTime() { DateTime result = (pubDate != null) ? DateTime.valueOf(pubDate.replace('T', ' ').replace('Z','')) : null; return result; } } public static RSS.channel getRSSData(String FeedURL) { HttpRequest req = new HttpRequest(); req.setEndpoint(FeedURL); req.setMethod('GET'); Dom.Document doc = new Dom.Document(); Http h = new Http(); Dom.XMLNode rss = doc.getRootElement(); //first child element of rss feed is always channel Dom.XMLNode channel = rss.getChildElements()[0]; RSS.channel result = new RSS.channel(); list<RSS.item> rssItems = new list<RSS.item>(); //for each node inside channel for(Dom.XMLNode elements : channel.getChildElements()) { if('title' == elements.getName()) { result.title = elements.getText(); } if('link' == elements.getName()) { result.link = elements.getText(); } if('description' == elements.getName()) { result.description = elements.getText(); } if('category' == elements.getName()) { result.category = elements.getText(); } if('copyright' == elements.getName()) { result.copyright = elements.getText(); } if('docs' == elements.getName()) { result.docs = elements.getText(); } if('image' == elements.getName()) { RSS.image img = new RSS.image(); //for each node inside image for(Dom.XMLNode xmlImage : elements.getChildElements()) { if('url' == xmlImage.getName()) { img.url = xmlImage.getText(); } if('title' == xmlImage.getName()) { img.title = xmlImage.getText(); } if('link' == xmlImage.getName()) { img.link = xmlImage.getText(); } } result.image = img; } if('item' == elements.getName()) { RSS.item rssItem = new RSS.item(); //for each node inside item for(Dom.XMLNode xmlItem : elements.getChildElements()) { if('title' == xmlItem.getName()) { rssItem.title = xmlItem.getText(); } if('guid' == xmlItem.getName()) { rssItem.guid = xmlItem.getText(); } if('link' == xmlItem.getName()) { rssItem.link = xmlItem.getText(); } if('description' == xmlItem.getName()) { rssItem.description = xmlItem.getText(); } if('pubDate' == xmlItem.getName()) { rssItem.pubDate = xmlItem.getText(); } if('source' == xmlItem.getName()) { rssItem.source = xmlItem.getText(); } } //for each item, add to rssItem list rssItems.add(rssItem); } } //finish RSS.channel object by adding the list of all rss items result.items = rssItems; return result; } }
All Answers
The reason you are getting the Null Pointer Exception (NPE) is because there is nothing in the content body to parse. If these URLs exist but are behind a firewall / VPN you will not be able to use them from Salesforce.
Thanks for the reply, but I tried with "http://feeds.abcnews.com/abcnews/topstories" and it has content in the body but still facing the same error.
You'll also want to actually show the items in your Visualforce page:
[1] http://salesforce.stackexchange.com/questions/34692/how-to-convert-an-dom-xmlnode-to-its-string-representation-in-order-to-get-cdata