1

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.

here is my code

mypage.xaml.cs

    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        DataContractJsonSerializer ser = null;
        try
        {
            ser = new DataContractJsonSerializer(typeof(ObservableCollection<User>));
            ObservableCollection<User> User = ser.ReadObject(e.Result) as ObservableCollection<User>;
            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);
            }
    }

User.cs

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; }
}

i just can not see where I'm going wrong the uri is correct and the output of the JSON is

{"Id":1,"Username":"Test1","Firstname":"Fir1","Surname":"Sur1","Email":"Test1@contoso.com","LiveId":"LID1","Language":"1","Subjects":"1"}

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

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

and this is also displayed after that addition

System.NullReferenceException: Object reference not set to an instance of an object.

at c_Sharp_WP8_Clo_Edu.viewinfo.webClient_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)

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.

this is a MAJOR forward move i feel so any more help please please let me know

Mark
  • 11
  • 1
  • 3
  • 1
    Welcome to Stack Overflow! Even on Windows Phone, almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Feb 08 '13 at 02:33
  • Have you tried stepping through in Visual Studio? That should help you determine what object is null... – nithins Feb 08 '13 at 02:33
  • spender - i have copied this word for word from an sample as i have never touched JSON in my life, Nithins - stepping just shows the null reference at the try from what i can see John - i will try there but its the phone that is saying "Object reference not set to an instance of an object" which is throwing me – Mark Feb 08 '13 at 02:40
  • For starters, it might be helpful to know the exact line of code where the exception is being raised. You can determine that one of two ways: 1. Single Step thru the code in the Debugger 2. Change your catch (Exception ex) to dump the Exception stack-trace catch(Exception ex) { System.Diagnostics.Debug.WriteLine(ex); MessageBox.Show(ex.Message); } This will give you the exact line of code (loc) where the failure is happening. Do that and update the post so we can help with the specific line of failure. – John Feb 08 '13 at 02:49

1 Answers1

0

Your JSON data is not an array, it's a singular object, so when you try to deserialize to an ObservableCollection, it just returns null. Try this code instead

void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    DataContractJsonSerializer ser = null;
    try
    {
        ser = new DataContractJsonSerializer(typeof(User));
        var user = ser.ReadObject(e.Result) as User;
        txbName.Text = "Username: " + user.Username;
        txbFirstName.Text = "FirstName:" + user.FirstName;
        txbSurname.Text ="Surname: " + user.Surname;
        txbEmail.Text = "Email: " + user.Email;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Connie Hilarides
  • 1,685
  • 15
  • 13