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; }
}
Why are you trying to convert the list to an Array, the Archive property being a List?
– Laerte
@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.
– Maniero
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()
– War Lock
ta error giving here: file.Arquivoversoes = list.Toarray();
– War Lock
And what happens when you just use
arquivo.ArquivoVersoes = list;?– Maniero
Error 1 Cannot implicitly Convert type 'System.Collections.Generic.List<Client.MeuService.Arquivoversao>' to 'Client.MeuService.Arquivoversao[]
– War Lock
No, this mistake has to happen when you use the
.ToArray(). What happens when you don’t use it?– Maniero
when not using, happens this, when using has no error
– War Lock
I have not understood why, since Archvoversoes is already the list
– War Lock
So it’s working when you take the
.ToArray()it had initially. That mistakeSystem.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– Maniero
Let’s go continue this discussion in chat.
– War Lock
Replace the ".File file = list;" with the ".FilefilesVersoes.Addrange(list);"
– Matheus Bessa
Matthew can’t find this Addrange.
– War Lock
Strange... What if you assign as "file.Architem.Add(list)"?
– Matheus Bessa
The snippet you put up has no problems, you identified the wrong part of the code.
– Malkaviano