c# - Object reference not set to an instance of an object error in json -


i trying develop json feed below format

{     "contacts": [         {                 "id": "c200",                 "name": "ravi tamada",                 "email": "ravi@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "male",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c201",                 "name": "johnny depp",                 "email": "johnny_depp@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "male",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },     ] } 

using below codes

public class phone {     public string mobile { get; set; }     public string home { get; set; }     public string office { get; set; } }  public class cont {     public string sno { get; set; }     public string name { get; set; }     public string em { get; set; }     public string address { get; set; }     public string gender { get; set; }     public phone phone { get; set; } }  public class rootobject {     public list<cont> contacts { get; set; } } 

and

var objecttoserialize = new rootobject();       //  var aa = new cont();         objecttoserialize.contacts = new list<cont>                            {                              new cont { sno = "test1", name = "index1" , address = "index1",gender="male",em="scd", phone={mobile="ff",home="ff",office="ff"}}                            //  new item { name = "test2", index = "index2" }                           };          javascriptserializer serializer = new javascriptserializer();         response.write(serializer.serialize(objecttoserialize)); 

using code unable output ,i getting object reference not set instance ..

i have added code .

any 1 wrong in code

this problem, in object initializer:

phone={mobile="ff",home="ff",office="ff"} 

that's trying set properties of existing phone object. in other words, it's performing:

var tmp = new cont(); tmp.sno = "test1"; ... tmp.phone.mobile = "ff"; tmp.phone.home = "ff"; ... 

... without ever setting value of tmp.phone non-null reference.

you either want:

phone=new phone {mobile="ff",home="ff",office="ff"} 

or need change cont class give constructor initialize phone:

public cont() {     phone = new phone(); } 

i'd strongly advise follow .net naming conventions properties, , give class full name of contact rather cont. avoid abbreviating pointlessly.

you should able configure serializer use lower-case names in json without making .net names ugly.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -