Note that there are some explanatory texts on larger screens.

plurals
  1. POsystem.nullreferenceexception when parsing json
    primarykey
    data
    text
    <p>i am trying to parse a JSON file and am having major problems with the above the device says "Object reference not set to an instance of an object." so I'm lost.</p> <p>here is my code</p> <p>mypage.xaml.cs</p> <pre><code> void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { DataContractJsonSerializer ser = null; try { ser = new DataContractJsonSerializer(typeof(ObservableCollection&lt;User&gt;)); ObservableCollection&lt;User&gt; User = ser.ReadObject(e.Result) as ObservableCollection&lt;User&gt;; foreach (User em in User) { txbName.Text = "Username: " + em.Username; txbFirstName.Text = "FirstName:" +em.FirstName; txbSurname.Text ="Surname: " +em.Surname ; txbEmail.Text = "Email: " + em.Email; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void btnGetData_Click(object sender, EventArgs e) { try { WebClient webClient = new WebClient(); Uri uri = new Uri("http://beta.cloud-education.cu.cc/api/User/1"); webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); webClient.OpenReadAsync(uri); } catch (Exception ex) { MessageBox.Show(ex.Message); } } </code></pre> <p>User.cs</p> <pre><code>class User { public int id {get; set;} public string Username {get; set;} public string FirstName {get; set;} public string Surname {get;set;} public string Email {get;set;} public string LiveId { get; set; } public int Language { get; set; } public int Subjects { get; set; } } </code></pre> <p>i just can not see where I'm going wrong the uri is correct and the output of the JSON is </p> <pre><code>{"Id":1,"Username":"Test1","Firstname":"Fir1","Surname":"Sur1","Email":"Test1@contoso.com","LiveId":"LID1","Language":"1","Subjects":"1"} </code></pre> <p>UPDATE- i didnt realize that one of the strings was not set right but still same error UPDATE 2 - John when i run the app i get this from the intermediate window</p> <p>A first chance exception of type 'System.NullReferenceException' occurred in c_Sharp_WP8_Clo_Edu.DLL An exception of type 'System.NullReferenceException' occurred in c_Sharp_WP8_Clo_Edu.DLL and wasn't handled before a managed/native boundary A first chance exception of type 'System.NullReferenceException' occurred in c_Sharp_WP8_Clo_Edu.DLL An exception of type 'System.NullReferenceException' occurred in c_Sharp_WP8_Clo_Edu.DLL and wasn't handled before a managed/native boundary</p> <p>and this is also displayed after that addition </p> <pre><code>System.NullReferenceException: Object reference not set to an instance of an object. </code></pre> <p>at c_Sharp_WP8_Clo_Edu.viewinfo.webClient_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)</p> <p>Update 3 - i have scanned everywhere i can see but with no success. Update 4 - so i implimented the new code and got this error on the phone Type'c_sharp_WP8_Clou_Edu.User' cannot be serialized. Consider making it with the DataContractAttribute attribute, and marking all of its members you want serialized with the SataMemberAttribute attribute. Alturnatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialied and no attributes will be reqired.</p> <p>this is a MAJOR forward move i feel so any more help please please let me know</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload