1
votes

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

2
Do you have a stack trace of the exception?Tejs
Where is tblResults declared and where is it constructed?Jonathan Henson
tblResults is created on the .aspx page <asp:table runat="server" id="tblResults">Zintouk

2 Answers

3
votes

I think this line:

   link.Text = s != null ? s.app.Name : (((r.RequesteeID == myUserID) ? r.RequesterName : r.RequestedName));  

will throw an object ref not set because "r" is null. The conditional expression is based on "s" variable and you are freely using "r".

Edit: By the way, to debug this more effectively you should go to Debug/Exceptions and enable the breakpoints when certain exceptions occur, like this:

enter image description here

2
votes
link.Text = s != null ? s.app.Name : (((r.RequesteeID == myUserID) ? r.RequesterName : r.RequestedName));

you do not check that r is not null here and r will be used if s == null. That may not be your problem, but it will definitely produce a bug.