需要帮助搞清楚NullReferenceException

时间:2011-05-03 20:10:52

标签: c# asp.net facebook iframe nullreferenceexception

我在代码隐藏中获取了一个Facebook IFRAME应用程序的NullReferenceException。对于我的生活,我无法弄清楚为什么我会得到它。

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);

}

这是方法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;




}

我已经能够逐步执行CreateRequestRow方法而不会抛出错误,但是当将返回的TableRow添加到tblResults时,它似乎就会发生。

非常感谢任何帮助。

编辑:忘记堆栈跟踪。

[NullReferenceException:对象引用未设置为对象的实例。]    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 个答案:

答案 0 :(得分:3)

我认为这一行:

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

将抛出一个未设置的对象,因为“r”为null。 条件表达式基于“s”变量,您可以自由使用“r”。

编辑:顺便说一下,要更有效地调试它,你应该去调试/异常并在发生某些异常时启用断点,如下所示:

enter image description here

答案 1 :(得分:2)

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

你不检查r在这里是否为空,如果s == null则使用r。这可能不是你的问题,但肯定会产生错误。