Object reference not set to an instance of an object. in the declaration of an object

0

An error is occurring in my code only when I post, when I'm debugging it works normally. the error is as follows: " Object reference not set to an instance of an object. " my code:

MkfFile file = new MkfFile();

public class MkfFile
{
    private List<FileData> _files { get; set; }
    private List<string> errors { get; set; }
    public MkfFile()
    {
        this._files = new List<FileData>();
        this.errors = new List<string>();
    }
    private class FileData
    {
    }
}

asked by anonymous 03.12.2014 / 17:25

1 answer

4

You can not declare a variable outside the class! It has to be contextualized in some object. That's why when you comment on the line, everything works fine because there are no variables declared loose in the file outside the class.

So I understand you want to declare it as global:

public class MkfFile
{
    private List<FileData> _files { get; set; }
    private List<string> errors { get; set; }
    MkfFile file = new MkfFile();

    public MkfFile()
    {
        this._files = new List<FileData>();
        this.errors = new List<string>();
    }
    private class FileData
    {
    }
}
    
11.12.2014 / 02:06