0

I am new to unity, i am learning how to make a unity app with Facebook integration using the Facebook SDK for Unity. I have been trying to make a json result from a graph request that is executed in a class method from unity, my problem is that everytime i try to access a propery of the supposed deserialized json to class it throws this error:

Object reference not set to an instance of an object

This error is thrown at the line where i am trying to access the property and not where the object is created.. this happens with both json utilities from Facebook and Unity, i've no idea how to properly transform a json data into a class, the code i have is this:

private void FbGetPhotos() {
    actionText.text = "Requesting photos";

    FB.API("/me/photos", HttpMethod.GET, result => {
        NodesResult nodesResult = NodesResult.FromJSON(result.RawResult);

        Debug.Log(nodesResult.data.Count);
    });
}

NodesResult class

using System.Collections.Generic;
using UnityEngine;

namespace classes {
    public class NodesResult {
        public List<object> data;
        public object paging;

        public static NodesResult FromJSON(string raw) {
            return JsonUtility.FromJson<NodesResult>(raw);
        }
    }
}

JSON String

{
  "data": [
    {
      "created_time": "2018-08-12T08:07:35+0000",
      "name": "Horas de horas para lograr esto Javier Andres Mura Rios Estoy orgulloso.",
      "id": "1919500911450147"
    }
  ],
  "paging": {
    "cursors": {
      "before": "QVFIUnpNaTZAqSW1NMDJya0ZAtWUZAiRWxRWW1lUHdmYy1rVERHaldsVllXSFBab0h4YW1HcEtXTmhNdW0tNGxVVjRQbXotdHo3alBvUzRUa09TaVJiVzJYc2ln",
      "after": "QVFIUnpNaTZAqSW1NMDJya0ZAtWUZAiRWxRWW1lUHdmYy1rVERHaldsVllXSFBab0h4YW1HcEtXTmhNdW0tNGxVVjRQbXotdHo3alBvUzRUa09TaVJiVzJYc2ln"
    }
  }
}

Like i said, the error is thrown when i try to access the property, in this case is where the Debug.Log is.

Note: I know there are many examples out there but none of them worked for me, also the facebook example handles the data using Dictionary object only, but i believe that using Dictionary only would make the code a bit hard to understand, also this is likely my first time parsing a json string in C# so i am very interested on what is the recommended way to properly parse the json string into a class and sub-classes

Oscar Reyes
  • 3,510
  • 7
  • 34
  • 61
  • @ScottChamberlain What problem is happening, why the problem is happening when i am accesing and not when creating the object?, i really tried to find an answer by myself before asking this in here – Oscar Reyes Oct 06 '18 at 04:48
  • @ScottChamberlain Both the editor and unity display that the error comes at the line where the Debug.Log is, as said in the question – Oscar Reyes Oct 06 '18 at 04:49
  • Are you sure that the JSON you received from FB complies with the class structure of `NodesResult`? – Chetan Ranpariya Oct 06 '18 at 04:49
  • @ChetanRanpariya yes, i edited the question showing how the json is – Oscar Reyes Oct 06 '18 at 04:50
  • @ScottChamberlain I edited the question to show the json string – Oscar Reyes Oct 06 '18 at 04:51
  • Please show us what the debugger shows for the value of `nodesResult` when you put a breakpoint in after it. Also show what the debugger shows as the value of `result.RawResult` inside the debugger. (also clean up your old comments a lot of previous comments where deleted when they where no longer relevent due to your updates) – Scott Chamberlain Oct 06 '18 at 04:52
  • put the the json [here](http://json2csharp.com/), generate the objects. Read the troubleshooting section in the duplicate if you have issues. If you still can't fix it, edit your question with the latest non-working code – Programmer Oct 06 '18 at 04:55
  • @ScottChamberlain `result.RawResult` is the exact value as shown in the edited question as a string, also @Programmer i will try that tool – Oscar Reyes Oct 06 '18 at 05:00

0 Answers0