I implemented a login example using Facebook OAuth
, but I ran into an issue after login credentials are input.
One of the fields that are being pulled in to the TBInfos
text box is causing a null reference
due to possibly the user not having a value for one of the fields.
I would usually check for null using something like this, s == null || s == String.Empty;
but I'm not sure how to apply it to the below code in question.
My question is, how can I implement a null or empty check for each of the fields pulled into the text box?
The complete error thrown is as follows:
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;
This is the code in question that pulls in the values from 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 + " , ";
}
}
I resolved the above null pointer exception by adding a null check for each of the fields being pulled in.
The following fixed it: (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.