I'm getting the following error when I try to load the RssReader.ascx control from my Default.aspx page. Here is the error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 24: if (value.StartsWith("/") || value.StartsWith("~/"))
Line 25: {
Line 26: url = (this.Page as BasePage).FullBaseUrl + value;
Line 27: url = url.Replace("~/", "");
Line 28: }
Source File: c:\sdalternative_v2\Controls\RssReader.ascx.cs Line: 26
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
MW.Alternative.UI.Controls.RssReader.set_RssUrl(St ring value) in c:\sdalternative_v2\Controls\RssReader.ascx.cs:26
ASP.default_aspx.__BuildControlRssReader1() in c:\sdalternative_v2\Default.aspx:5
ASP.default_aspx.__BuildControlContent1(Control __ctrl) in c:\sdalternative_v2\Default.aspx:4
System.Web.UI.CompiledTemplateBuilder.InstantiateI n(Control container) +32
ASP.alternative_master.__BuildControlMainContent() in c:\sdalternative_v2\Alternative.master:170
ASP.alternative_master.__BuildControlMain() in c:\sdalternative_v2\Alternative.master:41
ASP.alternative_master.__BuildControlTree(alternat ive_master __ctrl) in c:\sdalternative_v2\Alternative.master:1
ASP.alternative_master.FrameworkInitialize() in c:\sdalternative_v2\Alternative.master.cs:912307
System.Web.UI.UserControl.InitializeAsUserControlI nternal() +46
System.Web.UI.UserControl.InitializeAsUserControl( Page page) +35
System.Web.UI.MasterPage.CreateMaster(TemplateCont rol owner, HttpContext context, VirtualPath masterPageFile, IDictionary contentTemplateCollection) +1206
System.Web.UI.Page.get_Master() +72
System.Web.UI.Page.ApplyMasterPage() +30
System.Web.UI.Page.PerformPreInit() +48
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1446
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using MW.Alternative.UI;
namespace MW.Alternative.UI.Controls
{
public partial class RssReader : System.Web.UI.UserControl
{
public string RssUrl
{
get { return lnkRss.NavigateUrl; }
set
{
string url = value;
if (value.StartsWith("/") || value.StartsWith("~/"))
{
url = (this.Page as BasePage).FullBaseUrl + value;
url = url.Replace("~/", "");
}
lnkRss.NavigateUrl = url;
}
}
public string Title
{
get { return lblTitle.Text; }
set { lblTitle.Text = value; }
}
public int RepeatColumns
{
get { return dlstRss.RepeatColumns; }
set { dlstRss.RepeatColumns = value; }
}
public string MoreUrl
{
get { return lnkMore.NavigateUrl; }
set { lnkMore.NavigateUrl = value; }
}
public string MoreText
{
get { return lnkMore.Text; }
set { lnkMore.Text = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (this.RssUrl.Length == 0)
throw new ApplicationException("The RssUrl cannot be null.");
// create a DataTable and fill it with the RSS data,
// then bind it to the Repeater control
XmlDataDocument feed = new XmlDataDocument();
feed.Load(this.RssUrl);
XmlNodeList posts = feed.GetElementsByTagName("item");
DataTable table = new DataTable("Feed");
table.Columns.Add("Title", typeof(string));
table.Columns.Add("Description", typeof(string));
table.Columns.Add("Link", typeof(string));
table.Columns.Add("PubDate", typeof(DateTime));
foreach (XmlNode post in posts)
{
DataRow row = table.NewRow();
row["Title"] = post["title"].InnerText;
row["Description"] = post["description"].InnerText;
row["Link"] = post["link"].InnerText;
row["PubDate"] = post["pubDate"].InnerText;
table.Rows.Add(row);
}
dlstRss.DataSource = table;
dlstRss.DataBind();
}
catch (Exception)
{
{
this.Visible = true;
}
}
}
}
}