2

I know there are a lot of questions regarding JSON spread all over the internet and here on StackOverflow. But I can't get my JSON to work as it should, so I hope I can get some help around here. [Working in C#, Visual Studio]

I've got the following code:

public void CheckRFID(string RFIDtag) {
    [...SOME WORKING CODE]
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();            
    System.Diagnostics.Debug.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());  

    currentuser = Newtonsoft.Json.JsonConvert.DeserializeObject<User[]>(new StreamReader(response.GetResponseStream()).ReadToEnd());
    System.Diagnostics.Debug.WriteLine(currentuser[0].UserID);
}

This code keeps giving me "A first chance exception of type 'System.NullReferenceException' occurred in Database.dll. Additional information: Object reference not set to an instance of an object.".

As you can see I'm writing the response to debug log to see if I get a JSON, the result of this debug is [{"ID":"2","username":"IJK","gender":"F","color":"yellow"}]. Looks like a proper JSON line to me.

So, I'm stuck. I have no idea where to look, what I've done wrong...

And just for more info, this is the User.cs class:

public class User {
    public int UserID { get; set; }
    public string username { get; set; }
    public string gender { get; set; }
    public string color { get; set; }
}
4
  • can you tell your platform you're working on? Winforms? ASP.NET Webforms? ASP.NET MVC? Commented Nov 7, 2013 at 12:38
  • Which line is throwing the exception? Commented Nov 7, 2013 at 12:42
  • Yeah Sorry, will edit the post. Commented Nov 7, 2013 at 12:42
  • System.Diagnostics.Debug.WriteLine(currentuser[0].UserID); is throwing exception Commented Nov 7, 2013 at 12:43

2 Answers 2

4

The StreamReader().ReadToEnd() call will advance the source stream (response.GetResponseStream()) to the end. Because of that the second time you are creating it you will get an empty string back. This will result in currentuser being null.

Change your code like this:

public void CheckRFID(string RFIDtag)
{
    [...SOME WORKING CODE]
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    string responseString = (new StreamReader(response.GetResponseStream())).ReadToEnd();
    System.Diagnostics.Debug.WriteLine(responseString);  

    currentuser = Newtonsoft.Json.JsonConvert.DeserializeObject<User[]>(responseString);
    System.Diagnostics.Debug.WriteLine(currentuser[0].UserID);
}

You should also expect to have default value in your UserID property as it correct name should be `ID' (so it will not be bound properly) - I advice you change the name.

Sign up to request clarification or add additional context in comments.

2 Comments

Besides that string response should have different name it works! Thank you!
@Serellyn Sorry I have used the same name for variable twice, edited, should be fine now.
0

The first field on the JSON response is ID, not UserID, the name should not be the same?

4 Comments

Why would that cause a null reference exception?
If the json string and the class's property don't match, then UserID will be null. So, currentUser[0].UserID will evaluate to null. And his debug statement will evaluate to WriteLine(null);. None of these would throw a null ref exception.
It just skip field, if you have different name
Not the answer but a good thing to take a look at, I indeed need to change it to ID instead of UserID. Thanks for noticing Salvatore :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.