Generic object reference error

Asked

Viewed 1,091 times

3

System.Nullreferenceexception: Undefined object reference for an instance of an object.

This error is giving in the following code snippet:

Arquivo arquivo = new Arquivo();
ArquivoVersao versao = new ArquivoVersao();
versao.XNOME = "teste";
var list = new List<ArquivoVersao>();
list.Add(versao);
arquivo.ArquivoVersoes = list;
//arquivoVersoes é uma lista de Versoes

File class:

public class Arquivo
{
    public string ARQUIVO_GUID { get; set; }
    public string XARQUIVO { get; set; }
    public string TAG { get; set; }
    public string EXTENSAO { get; set; }
    public string URL { get; set; }
    public bool IS_STREAM { get; set; }
    public string ULT_ARQUIVO_VERSAO_GUID { get; set; }
    public string TIPO_DE_ARQUIVO_GUID { get; set; }
    public string DIRETORIO_GUID { get; set; }
    public TipoDeArquivo TipoDeArquivo { get; set; }
    public List<ArquivoVersao> ArquivoVersoes { get; set; }
}

Class File Version:

public class ArquivoVersao
{
        public string XNOME { get; set; }
}
  • 1

    Why are you trying to convert the list to an Array, the Archive property being a List?

  • @Laerte I am writing an answer just beginning with this. I don’t know if this is generating the error or if there are more excerpts where the problem really occurs. I can’t tell if it is with what was posted.

  • because I just can’t add, using Files.add, as I am using a Webservice, this File and Archiversao are services.I actually declare Myuservico.File = Myuservico.File()

  • ta error giving here: file.Arquivoversoes = list.Toarray();

  • And what happens when you just use arquivo.ArquivoVersoes = list;?

  • Error 1 Cannot implicitly Convert type 'System.Collections.Generic.List<Client.MeuService.Arquivoversao>' to 'Client.MeuService.Arquivoversao[]

  • No, this mistake has to happen when you use the .ToArray(). What happens when you don’t use it?

  • when not using, happens this, when using has no error

  • I have not understood why, since Archvoversoes is already the list

  • So it’s working when you take the .ToArray() it had initially. That mistake System.NullReferenceException: Referência de objeto não definida para uma instância de um objeto. doesn’t happen in this code snippet? I test and it’s working as it should: https://dotnetfiddle.net/6Qt4RA

  • Replace the ".File file = list;" with the ".FilefilesVersoes.Addrange(list);"

  • Matthew can’t find this Addrange.

  • Strange... What if you assign as "file.Architem.Add(list)"?

  • 1

    The snippet you put up has no problems, you identified the wrong part of the code.

Show 10 more comments

1 answer

4


I immediately see a problem in the use of .ToArray(). If you have a variable that is a list and will save in another variable that is also a list of the same type have no reason to convert the list into array.

The code presented in the current version of the question (before it had the .ToArray()) does not contain the declared error:

using System.Collections.Generic;

public class Program {
    public static void Main() {
        Arquivo arquivo = new Arquivo();
        ArquivoVersao versao = new ArquivoVersao();
        versao.XNOME = "teste";
        var list = new List<ArquivoVersao>();
        list.Add(versao);
        arquivo.ArquivoVersoes = list;
        //arquivoVersoes é uma lista de Versoes
    }
}

public class Arquivo {
    public string ARQUIVO_GUID { get; set; }
    public string XARQUIVO { get; set; }
    public string TAG { get; set; }
    public string EXTENSAO { get; set; }
    public string URL { get; set; }
    public bool IS_STREAM { get; set; }
    public string ULT_ARQUIVO_VERSAO_GUID { get; set; }
    public string TIPO_DE_ARQUIVO_GUID { get; set; }
    public string DIRETORIO_GUID { get; set; }
//    public TipoDeArquivo TipoDeArquivo { get; set; }
    public List<ArquivoVersao> ArquivoVersoes { get; set; }
}

public class ArquivoVersao {
        public string XNOME { get; set; }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

When I have more information I can improve the answer.

Also you should consider not using names with all uppercase characters and underline (underlined). This escapes from the standard adopted by C#.

  • Really the mistake was coming from the other side. But thanks for the tips.

Browser other questions tagged

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