-3

I want to write all names of each library in textBox1 from a .json file. So far I have this code:

client.DownloadFile("https://s3.amazonaws.com/Minecraft.Download/versions/1.8.8/1.8.8.json", "1.8.8.json");
Library libs = JsonConvert.DeserializeObject<Library>(File.ReadAllText("1.8.8.json"));
foreach (var item in libs.name) { textBox1.Text += item; }
public class Library { public string name { get; set; } }

It makes no errors, but when I run it the error is System.NullReferenceException. Can you please tell me what am I doing wrong?

rysroma
  • 13
  • 5
  • In which line the exception thrown? – Bigeyes Nov 16 '16 at 21:40
  • I do not want general answer, I have Google. I spent 4 hours figuring what I do wrong and I still dont get it, please help me if you know how. – rysroma Nov 16 '16 at 21:41
  • @Bigeyes var item in libs.name – rysroma Nov 16 '16 at 21:44
  • Have you carefully read the link mybirthname provided? There are several issues with your code, but they won't be clear unless you understand why you got the NullReferenceException in a general way. – vbnet3d Nov 16 '16 at 21:47
  • Yes I did and I know what NullReferenceException is, so what are my mistakes? – rysroma Nov 16 '16 at 21:51
  • @rysroma. I suggest you to look at json string first. `var x = File.ReadAllText("1.8.8.json")`. Perhaps `x` is null. – Bigeyes Nov 16 '16 at 21:53
  • It is not, textBox1.Text = File.ReadAllText("1.8.8.json"); will return correct text in JSON format. But libs are returning null do not know why :( – rysroma Nov 16 '16 at 21:56
  • @rysroma, can you post a screenshot or stacktrace so we can help you. – Bigeyes Nov 16 '16 at 21:57
  • What is the json inside the document? Please paste it into the question. –  Nov 16 '16 at 22:00
  • @rysroma Check out my answer. It is the best I can come up with from what you have in your questions. – vbnet3d Nov 16 '16 at 22:09

2 Answers2

0

I think your problem is associated with the File.ReadAllText. The syntax of this method is the following:

public static string ReadAllText(string path)

I doubt that 1.8.8.json is a path.

You could try to use the following:

var path = Path.Combine(Application.StartupPath,"1.8.8.json");
var libs = JsonConvert.DeserializeObject<Library>File.ReadAllText(path);

However, even this change will not solve your problem ! Please have a careful look at the file, which is served if we request the url you have mentioned. How this structure is associated with a JSON object, which has a property called name?

Furthermore, even if you fix this, what does the following?

The libs, if all goes well, is an instance of the class Library. It isn't an IEnumerable<T>, in order to iterate through it.

foreach (var item in libs.name) { textBox1.Text += item; }

This would have made sense, if you had something like the following:

var libs = JsonConvert.DeserializeObject<IEnumerable<Library>>File.ReadAllText(path);

In other words, if after the parsing of the file in the specified path you had a sequence of Library objects.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • It is not, textBox1.Text = File.ReadAllText("1.8.8.json"); will return correct text in JSON format. But libs are returning null do not know why. – rysroma Nov 16 '16 at 21:50
  • @rysroma Have you tested this? I don't see how that can be true, given the method's signature. – Christos Nov 16 '16 at 22:05
0

It seems like you may have a structural conflict between your class definition and the data you are requesting. For example, your class is defined this way:

    public class Library { public string name { get; set; } }

This means that each library has a single string variable containing its name. So far, so good. But then, when you try to deserialize the Json, it seems like you are looking for an array of Library class objects:

    Library libs = JsonConvert.DeserializeObject<Library>(File.ReadAllText("1.8.8.json"));

So when the Json deserializer tries to to deserialize an array of Library, or Library[], it fails to perform the mapping, and returns a null value. When the next line is executed, libs is null, and so is name, and you get the NullReferenceException:

    foreach (var item in libs.name) { textBox1.Text += item; }

Furthermore, your foreach loop is actually not trying to go from one Library to the next, but rather through each character of the Library name. You should write your code more like this:

Library[] libs = JsonConvert.DeserializeObject<Library[]>(File.ReadAllText("1.8.8.json"));
foreach (Library lib in libs) {textBox1.Text += lib.name; }
vbnet3d
  • 1,151
  • 1
  • 19
  • 37