0

我使用 实现了一个登录示例Facebook OAuth,但在输入登录凭据后遇到了问题。

被拉入TBInfos文本框中的字段之一导致null reference 用户可能没有其中一个字段的值。

我通常会使用类似这样的方法检查 null ,s == null || s == String.Empty;但我不确定如何将其应用于以下有问题的代码。

我的问题是,如何对拉入文本框中的每个字段执行 null 或空检查?

抛出的完整错误如下:

 An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll but was not handled in user code

    Additional information: Cannot perform runtime binding on a null reference

//create instance of FB client
private FacebookClient fbC;

这是从 Facebook 提取值的有问题的代码:

public InfoWindow(FacebookClient FBClient)
{
    InitializeComponent();
    fbC = FBClient;
    dynamic me = FBClient.Get("Me");
    TBInfos.Text = "Name : " + me.name.ToString() + "\n\r"
                   + "Gender : " + me.gender.ToString() + "\n\r"
                   + "Link : " + me.link.ToString() + "\n\r"
                   + "Quote : " + me.quotes.ToString() + "\n\r"
                   + "Sports : ";
    foreach (var item in me.sports)
    {
        TBInfos.Text += item.name + " , ";
    }
    TBInfos.Text += "\n\r" + "Favorite Athletes : ";
    foreach (var item in me.favorite_athletes)
    {
        TBInfos.Text += item.name + " , ";
    }
    TBInfos.Text += "\n\r" + "Languages : ";
    foreach (var item in me.languages)
    {
        TBInfos.Text += item.name + " , ";
    }

}
4

1 回答 1

0

我通过为每个被拉入的字段添加一个空检查来解决上述空指针异常。

以下修复了它:(me.gender ?? (object)string.Empty).ToString()

The issue was that for some of the Facebook profiles, one or more of the fields may have been blank/null, which is what caused the exception.

于 2015-01-06T19:53:10.930 回答