Null reference error if JSonData key does not exist

Post here all your questions related with SmartFoxServer .Net/Unity3D API

Moderators: Lapo, Bax

shaitaaan
Posts: 11
Joined: 02 Apr 2009, 04:29
Contact:

Null reference error if JSonData key does not exist

Postby shaitaaan » 06 Apr 2009, 09:53

Hi,

Please see the code below:

Code: Select all

public void OnExtensionResponse(object data, string type)
{
    if (type == SmartFoxClient.XTMSG_TYPE_JSON)
    {
        JsonData responseObject = (JsonData)data;
        if(responseObject[key] != null)
        {
             value = responseObject[key];
        }
    }
}


If the key is not present, then a null reference exception is thrown. JSonData does not have a .Contains method either. How do I check if the object contains a certain key?

Thank you.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 06 Apr 2009, 14:54

Usually by convention.

E.g. I always have a "cmd" that I _always_ use in each object sent, so I know what I got in my hand. From there it is simply a matter of knowing that e.g. cmd == "move" has a "x" and "y" coordinate that contains ints.

LitJson is pretty bare bones - but fast and small. Tradeoffs when deciding what to use

/Thomas

Return to “.Net / Unity3D API”

Who is online

Users browsing this forum: No registered users and 4 guests