c# - 需要帮助找出 NullReferenceException

标签 c# asp.net facebook iframe nullreferenceexception

我在 Facebook IFRAME 应用程序的代码 Conceal 中收到 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 系统.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain( bool 值 includeStagesBeforeAsyncPoint, bool 值 includeStagesAfterAsyncPoint)+627

最佳答案

我认为这一行:

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

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

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

enter image description here

关于c# - 需要帮助找出 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5875105/

相关文章:

asp.net - 重命名 ASP.NET_SessionId

java - android facebook java使用Graph API获取用户名

php - URL 转换为 guzzle 请求

c# - 在C#中将音频流添加到视频流

C# 左连接两个数据表

c# - 如何将 css 代码引用到 HtmlToImageConverter

Asp.net azure 共存缓存 session 状态设置?

javascript - 为什么我们在从 ajax json 调用获得响应时使用 data.d?

c# - 尝试使用 Nancy SuperSimpleViewEngine 渲染局部时获取 [ERR!] 作为输出

c# - 使用 C# 调试访问 token