Object reference not defined for an object instance. in an object declaration

Asked

Viewed 15,042 times

0

There is an error in my code only when I publish, when debugging works normally. the error is as follows: "Undefined object reference for an object instance." 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
    {
    }
}

If I comment on the line "Mkffile file = new Mkffile();" it works normally, someone can tell me where the mistake is?

  • The font is exactly the way you put it in the question?

  • On which line the error occurs?

  • Anything new?

1 answer

5

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

I understand you want to declare it 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
    {
    }
}
  • 1

    I was going to answer, but I think the kid abandoned the question. + 1.

  • Yeah, he must have noticed the mistake and didn’t post it here. :)

  • actually I was trying to use a variable I thought was set on my debug web.config, and not on the web.config q I was using on Publish... sorry, I had totally forgotten the question

  • @Leonelsanchesdasilva I believe that nor should have returned to the stackoverflow. just like many, came only to get the answer to a problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.