Click here to Skip to main content
15,936,395 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public class RootJson
{
    public string App { get; set; }
    public List<SourcesFile> ListSourcesFile { get; set; }
}

public class SourcesFile
{
    public string Source { get; set; }
}

This is my root

Now:

C#
RootJson rootjson = new RootJson()
rootjson.App = appIdentifier.AppName;

using (StreamWriter file = File.CreateText(@"c:\test\source.json"))
{
   JsonSerializer serializer = new JsonSerializer();
   serializer.Serialize(file, rootjson);
}


This works fine too. But this doesn't work:

C#
rootjson.ListSourcesFile[0].Source = "Fdf";


It gives this error

Object reference not set to an instance of an object.

I think it's because the reference is not initialized. But how can i do this?

What I have tried:

ourcesFile ListSourcesFile = new SourcesFile ();
Posted
Updated 8-Mar-17 9:06am

The first problem is that you haven't initialized the list, so rootjson.ListSourcesFile returns null, and any attempt to index into the list throws a NullReferenceException.

The second problem is that you haven't added anything to the list. When you try to retrieve the first item of an empty list, you'll get an ArgumentOutOfRangeException telling you that the index you're trying to access does not exist.

Try initializing the list before you access it:
C#
RootJson rootjson = new RootJson
{
    App = appIdentifier.AppName,
    ListSourcesFile = new List<SourcesFile>
    {
        new SourcesFile
        {
            Source = "Fdf",
        },
    },
};

// Or:
RootJson rootjson = new RootJson();
rootjson.App = appIdentifier.AppName;
rootjson.ListSourcesFile = new List<SourcesFile>();

SourcesFile file = new SourcesFile();
file.Source = "Fdf";
rootjson.ListSourcesFile.Add(file);
 
Share this answer
 
Comments
Amien90 7-Mar-17 15:47pm    
Thanks!

Works fine .. but one last additional question; how can i add two SourcesFiles to the same app?

Adding another : rootjson.ListSourcesFile.Add(file);
will not work
Richard Deeming 7-Mar-17 15:49pm    
Why will it not work?

Either:
RootJson rootjson = new RootJson
{
    App = appIdentifier.AppName,
    ListSourcesFile = new List<SourcesFile>
    {
        new SourcesFile
        {
            Source = "Fdf",
        },
        new SourcesFile
        {
            Source = "Sdr",
        },
    },
};
or:
SourcesFile file = new SourcesFile();
file.Source = "Fdf";
rootjson.ListSourcesFile.Add(file);

file = new SourcesFile();
file.Source = "Sdr";
rootjson.ListSourcesFile.Add(file);
will work.
Amien90 8-Mar-17 13:45pm    
Yes. . this works .. but i need to run a loop to add "SourceFiles" elements. So each App contains several SourceFiles. Something like this (see code below) .. but with both your codes .. i can't use a while loop

string contents = app.GetScript();
using (StringReader reader = new StringReader(contents))
{
string line;
while ((line = reader.ReadLine()) != null)
{
file = new SourcesFile();
file.Source = line;
rootjson.ListSourcesFile.Add(file);
}
}
Richard Deeming 8-Mar-17 13:59pm    
Why not? That code should work. What's the error?
Amien90 8-Mar-17 15:05pm    
my bad .. your right .. i misplaced a }
RootJson rootjson = new RootJson
{
App = appIdentifier.AppName,
ListSourcesFile = new List<sourcesfile>
{
new SourcesFile
{
Source = "Fdf",
},
new SourcesFile
{
Source = "Sdr",
},
},
};
or:
SourcesFile file = new SourcesFile();
file.Source = "Fdf";
rootjson.ListSourcesFile.Add(file);

file = new SourcesFile();
file.Source = "Sdr";
rootjson.ListSourcesFile.Add(file);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900