I am getting a NullReferenceException in my codebehind for a Facebook IFRAME application. For the life of me I can not figure out why I am getting it.
Survey s = Survey.find(myUserID);
List<Survey> list = s.FindMatches(rdblResultsType.SelectedIndex);
if (list.Count > 0)
{
lblResultsCount.Text = list.Count + " survey match" + (list.Count == 1 ? "" : "es");
rdblResultsType.Items[1].Text = s.onCampus ? "On Campus" : "Off Campus";
tblResults.Visible = true;
foreach (Survey p in list)
tblResults.Rows.Add(CreateRequestRow(null, p)); //Error taking place here
TableRow footer = new TableRow();
TableCell bottomcell = new TableCell();
bottomcell.ColumnSpan = 2;
bottomcell.Text = "<br/><br/>";
footer.Cells.Add(bottomcell);
tblResults.Rows.Add(footer);
}
And here is the method CreateRequestRow:
protected TableRow CreateRequestRow(RoommateRequest r, Survey s)
{
long uid = (r != null ? ((r.RequesteeID == myUserID) ? r.RequesterID : r.RequesteeID) : s.facebookID.Value);
//Facebook.Schema.user user = Api.Users.GetInfo(r != null ? ((r.RequesteeID == myUserID) ? r.RequesterID : r.RequesteeID) : s.facebookID.Value);
TableRow tr = new TableRow();
TableCell c1 = new TableCell();
TableCell c2 = new TableCell();
Button link = new Button();
link.CssClass = "linkbutton";
link.CommandArgument = uid.ToString();
link.Text = s != null ? s.app.Name : (((r.RequesteeID == myUserID) ? r.RequesterName : r.RequestedName));
if (link.Text.Trim() == "") link.Text = "Facebook User";
link.Click += new EventHandler(btnView_Click);
c2.Controls.Add(link);
if (r != null)
{
c2.Controls.Add(new LiteralControl("<br/>"));
c2.Controls.Add(new LiteralControl(r.DateSent.ToShortDateString()));
if (r.Closed)
c2.Controls.Add(new LiteralControl("<br/>Request Closed"));
else if (r.Accepted)
c2.Controls.Add(new LiteralControl("<br/><b>Accepted</b>"));
else c2.Controls.Add(new LiteralControl("<br/>Awaiting Reply"));
}
tr.Cells.Add(c1);
tr.Cells.Add(c2);
return tr;
}
I have been able to step through the CreateRequestRow method without the error being thrown but it seems to take place when the returned TableRow is added to tblResults.
Any help would be greatly appreciated.
EDIT: Forgot the stack trace.
[NullReferenceException: Object reference not set to an instance of an object.] Facebook_DisplayRequests.CreateRequestRow(RoommateRequest r, Survey s) +313 Facebook_DisplayRequests.UpdateRequests() +2389 Facebook_DisplayRequests.Page_Load(Object sender, EventArgs e) +343 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 Facebook_Graph_Toolkit.CanvasPage.OnLoad(EventArgs e) +772 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627