Error: Unable to bind runtime (when location / hometown / any setting is not set in facebook profile)

I encountered an error

Unable to bind runtime to null reference

Description: An unhandled exception occurred during the execution of the current web request. Check the stack trace for more information about the error and where it appeared in the code.

Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Unable to bind while executing a null reference

Source Error:

Line 27: lblBirthday.Text = (myInfo.birthday == null? String.Empty: DateTime.Parse (myInfo.birthday) .ToString ("dd-MMM-yy")); Line 28: lblHometown.Text = (myInfo.hometown.name == null? String.Empty: myInfo.hometown.name); Line 29: lblLocation.Text = (myInfo.location.name == null? String.Empty: myInfo.location.name); Line 30: pnlHello.Visible = true; Line 31:}

Here is my code:

var fb = new FacebookWebClient(); dynamic myInfo = fb.Get("me"); lblName.Text = myInfo.name; imgProfile.ImageUrl = "https://graph.facebook.com/" + myInfo.id + "/picture"; lblBirthday.Text = (myInfo.birthday == null ? string.Empty : DateTime.Parse(myInfo.birthday).ToString("dd-MMM-yy")); lblHometown.Text = (myInfo.hometown.name == null ? string.Empty : myInfo.hometown.name); lblLocation.Text = (myInfo.location.name == null ? string.Empty : myInfo.location.name); pnlHello.Visible = true; 
+8
c # facebook facebook-c # -sdk
source share
2 answers

First check that myInfo.location is null:

 lblLocation.Text = myInfo.location == null ? "" : myInfo.location.name ?? ""; 

(And similarly for similar members.)

It's a bit of a pain, admittedly, but basically you need to consider everything that can be null to make sure you're not trying to dereference it.

+13
source share

I changed my code as:

 var fb = new FacebookWebClient(); dynamic myInfo = fb.Get("me"); lblName.Text = myInfo.name; imgProfile.ImageUrl = "https://graph.facebook.com/" + myInfo.id + "/picture"; lblBirthday.Text = (myInfo.birthday == null ? string.Empty : DateTime.Parse(myInfo.birthday).ToString("dd-MMM-yy")); lblHometown.Text = (myInfo.hometown == null ? string.Empty : myInfo.hometown.name); lblLocation.Text = (myInfo.location == null ? string.Empty : myInfo.location.name); pnlHello.Visible = true; 
0
source share

All Articles