Question by
Victor_S · Mar 20, 2016 at 07:30 AM ·
nullreferenceexceptionfacebookdictionary
NullReferenceException: A null value was found where an object instance was required. at InitScript.LoginCallback2
Dear Unity developers, I'm facing a problem with getting id, name and email from Facebook. My code:
public void CallFBLogin()
{
FB.Login("public_profile,email,user_friends", LoginCallback);
}
private void LoginCallback(FBResult result)
{
Debug.Log("LoginCallback");
if (result.Error != null)
lastResponse = "Error Response:\n" + result.Error;
else if (!FB.IsLoggedIn)
{
lastResponse = "Login cancelled by Player";
}
else
{
lastResponse = "Login was successful!";
FB.API("/me?fields=first_name,email,id", Facebook.HttpMethod.GET, LoginCallback2);
}
}
void LoginCallback2(FBResult result)
{
Debug.Log("LoginCallback2");
Debug.Log("name responce received " + result.Text);
IDictionary dict = Facebook.MiniJSON.Json.Deserialize(result.Text) as IDictionary;
fbname = dict["first_name"].ToString();
fbemail = dict["email"].ToString();
fbid = dict["id"].ToString();
Debug.Log("your name is: " + fbname + " email: " + fbemail + " id = " + fbid);
if (fbemail == null)
{
fbemail = "null@email.com";
}
RegisterUser(fbid, fbname, fbemail);
}
RegisterUser method will register player with my backend. When testing in Unity editor with Fb User Token all works great and user registers with my backend. When testing on iOs devise I'm getting an error in XCode:
NullReferenceException: A null value was found where an object instance was required.
at InitScript.LoginCallback2 (.FBResult result) [0x00000] in <filename unknown>:0
at Facebook.AsyncRequestString+<Start>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0
This code is not mine and I'm actually not native Unity(C#) developer so maybe it's something with Dictionary alloc? Thanks
Comment
I would check result for null. then I would save the result of the deserialization in a separate variable and log it. then I'd cast it into IDictionary and see if THAT is null.