0

i have an error in c# of NullReferenceException: Object reference not set to an instance of an object SimpleJSON.JSONNode.Parse (System.String aJSON) (at Assets/Scenes/scripts/SimpleJSON.cs:553)

I found this post in the link below but i didn't understand the solution SimpleJSON returns null when handling a call from Postmen

Can any one explain it for me, please?

the error is : NullReferenceException: Object reference not set to an instance of an object SimpleJSON.JSONNode.Parse (System.String aJSON) (at Assets/Scenes/scripts/SimpleJSON.cs:553) SimpleJSON.JSON.Parse (System.String aJSON) (at Assets/Scenes/scripts/SimpleJSON.cs:1363) Control.Update () (at Assets/Control.cs:123)

knowing that i'm sending json data using websocket and i'm receiving it in the console unity. i use simpleJSON script to deserialize the data.

here where the error is provided in line JSONNode root = JSON.Parse(last_message); :

private void Update()
    {
        if (ws == null)
        {
            return;
        }
        List<GameObject> object_to_keep = new List<GameObject>(); 
        JSONNode root = JSON.Parse(last_message);        
        JSONNode Face = root["streams"][0]["objects"][0];

        {
            int id = Face["mono"]["id"].AsInt; 
           

            JSONNode position = Face["mono"]["position"];         
            Vector3 pos = new Vector3(position["x"].AsFloat, position["y"].AsFloat, position["z"].AsFloat);
            
            GameObject mono = GetMonoObject();
            WintualFaceController win_controller = mono.GetComponent<WintualFaceController>();
            win_controller.face_id = id;
            if(mono)
            {
                mono.transform.localPosition = pos;
                object_to_keep.Add(mono);
            }
            else
                print("Mono not found");
        }
}

The error in SImpleJSON script wan in line: while (i < aJSON.Length)

public static JSONNode Parse(string aJSON)
        {
            Stack<JSONNode> stack = new Stack<JSONNode>();
            JSONNode ctx = null;
            int i = 0;
            StringBuilder Token = new StringBuilder();
            string TokenName = "";
            bool QuoteMode = false;
            bool TokenIsQuoted = false;
            while (i < aJSON.Length)           
            {
                switch (aJSON[i])
                {
                    case '{':
                        if (QuoteMode)
                        {
                            Token.Append(aJSON[i]);
                            break;
                        }
                        stack.Push(new JSONObject());
                        if (ctx != null)
                        {
                            ctx.Add(TokenName, stack.Peek());
                        }
                        TokenName = "";
                        Token.Length = 0;
                        ctx = stack.Peek();
                        break;
 
                    case '[':
                        if (QuoteMode)
                        {
                            Token.Append(aJSON[i]);
                            break;
                        }
 
                        stack.Push(new JSONArray());
                        if (ctx != null)
                        {
                            ctx.Add(TokenName, stack.Peek());
                        }
                        TokenName = "";
                        Token.Length = 0;
                        ctx = stack.Peek();
                        break;
 
                    case '}':
                    case ']':
                        if (QuoteMode)
                        {
 
                            Token.Append(aJSON[i]);
                            break;
                        }
                        if (stack.Count == 0)
                            throw new Exception("JSON Parse: Too many closing brackets");
 
                        stack.Pop();
                        if (Token.Length > 0 || TokenIsQuoted)
                        {
                            ParseElement(ctx, Token.ToString(), TokenName, TokenIsQuoted);
                            TokenIsQuoted = false;
                        }
                        TokenName = "";
                        Token.Length = 0;
                        if (stack.Count > 0)
                            ctx = stack.Peek();
                        break;
 
                    case ':':
                        if (QuoteMode)
                        {
                            Token.Append(aJSON[i]);
                            break;
                        }
                        TokenName = Token.ToString();
                        Token.Length = 0;
                        TokenIsQuoted = false;
                        break;
 
                    case '"':
                        QuoteMode ^= true;
                        TokenIsQuoted |= QuoteMode;
                        break;
 
                    case ',':
                        if (QuoteMode)
                        {
                            Token.Append(aJSON[i]);
                            break;
                        }
                        if (Token.Length > 0 || TokenIsQuoted)
                        {
                            ParseElement(ctx, Token.ToString(), TokenName, TokenIsQuoted);
                            TokenIsQuoted = false;
                        }
                        TokenName = "";
                        Token.Length = 0;
                        TokenIsQuoted = false;
                        break;
 
                    case '\r':
                    case '\n':
                        break;
 
                    case ' ':
                    case '\t':
                        if (QuoteMode)
                            Token.Append(aJSON[i]);
                        break;
 
                    case '\\':
                        ++i;
                        if (QuoteMode)
                        {
                            char C = aJSON[i];
                            switch (C)
                            {
                                case 't':
                                    Token.Append('\t');
                                    break;
                                case 'r':
                                    Token.Append('\r');
                                    break;
                                case 'n':
                                    Token.Append('\n');
                                    break;
                                case 'b':
                                    Token.Append('\b');
                                    break;
                                case 'f':
                                    Token.Append('\f');
                                    break;
                                case 'u':
                                    {
                                        string s = aJSON.Substring(i + 1, 4);
                                        Token.Append((char)int.Parse(
                                            s,
                                            System.Globalization.NumberStyles.AllowHexSpecifier));
                                        i += 4;
                                        break;
                                    }
                                default:
                                    Token.Append(C);
                                    break;
                            }
                        }
                        break;
 
                    default:
                        Token.Append(aJSON[i]);
                        break;
                }
                ++i;
            }

this is my json:

{ "cmd": "data", "frame_time": "2021-06-30T09:04:16.464836+00:00", "start_time": "2021-06-30T07:51:33.452079+00:00", "streams": [ { "name": "usb-0001:012.0-3", "objects": [ { "name": "face", "mono": { "id": "190", "position": { "x": "-0.012259", "y": "0.059731", "z": "0.707695" } }, "multi": [ { "id": "190", "position": { "x": "-0.012263", "y": "0.059753", "z": "0.707151" } } ] } ] } ] }

SAL55
  • 13
  • 3

0 Answers0