5
I am trying to serialize an XML of the electronic invoice, but when I arrived at the ide element where is an Array object I unpacked, as I receive the error "Undefined object reference to an instance of an object." Based on an Nfe XML I generated the classes to be serialized, using the Visual Studio automatic option, special edit/paste/Paste XML as classes.
And to test I made the following code.
private void button1_Click_1(object sender, EventArgs e)
{
try
{
nfeProc nota = new nfeProc();
nota.versao = 3.10M;
nota.NFe = new nfeProcNFe();
nota.NFe.infNFe = new nfeProcNFeInfNFe();
nota.NFe.infNFe.Id = "NFe13130604501136000136650020000973882010222458";
nota.NFe.infNFe.versao = 3.10M;
nota.NFe.infNFe.ide = new nfeProcNFeInfNFeIde();
nota.NFe.infNFe.ide.cUF = 11;
nota.NFe.infNFe.ide.cNF = 01022245;
nota.NFe.infNFe.ide.natOp = "VENDA DE MERCADORIA";
nota.NFe.infNFe.ide.indPag = 0;
nota.NFe.infNFe.ide.mod = 65;
nota.NFe.infNFe.ide.serie = 1;
nota.NFe.infNFe.ide.nNF = 23354;
nota.NFe.infNFe.ide.dhEmi = DateTime.Now;
nota.NFe.infNFe.ide.tpNF = 1;
nota.NFe.infNFe.ide.idDest = 1;
nota.NFe.infNFe.ide.cMunFG = 1100114;
nota.NFe.infNFe.ide.tpImp = 4;
nota.NFe.infNFe.ide.tpEmis = 1;
nota.NFe.infNFe.ide.cDV = 8;
nota.NFe.infNFe.ide.tpAmb = 2;
nota.NFe.infNFe.ide.finNFe = 1;
nota.NFe.infNFe.ide.indFinal = 1;
nota.NFe.infNFe.ide.indPres = 1;
nota.NFe.infNFe.ide.procEmi = 0;
nota.NFe.infNFe.ide.verProc = "1.0";
nota.NFe.infNFe.emit = new nfeProcNFeInfNFeEmit();
nota.NFe.infNFe.emit.CNPJ = 55555555555555;
nota.NFe.infNFe.emit.xNome = "LOJA FAKE CONFECCOES";
nota.NFe.infNFe.emit.enderEmit = new nfeProcNFeInfNFeEmitEnderEmit();
nota.NFe.infNFe.emit.enderEmit.xLgr = "AVENIDA TESTE I";
nota.NFe.infNFe.emit.enderEmit.nro = 1257;
nota.NFe.infNFe.emit.enderEmit.xCpl = "COMERCIO";
nota.NFe.infNFe.emit.enderEmit.xBairro = "BAIRRO";
nota.NFe.infNFe.emit.enderEmit.cMun = 1100114;
nota.NFe.infNFe.emit.enderEmit.xMun = "JARULANDIA";
nota.NFe.infNFe.emit.enderEmit.UF = "RO";
nota.NFe.infNFe.emit.enderEmit.CEP = 76590000;
nota.NFe.infNFe.emit.enderEmit.cPais = 1058;
nota.NFe.infNFe.emit.enderEmit.xPais = "BRASIL";
nota.NFe.infNFe.emit.enderEmit.fone = 333333333;
nota.NFe.infNFe.emit.IE = 2222222;
nota.NFe.infNFe.emit.CRT = 1;
nota.NFe.infNFe.dest = new nfeProcNFeInfNFeDest();
nota.NFe.infNFe.dest.CPF = 98765432198;
nota.NFe.infNFe.dest.xNome = "";
nota.NFe.infNFe.dest.enderDest = new nfeProcNFeInfNFeDestEnderDest();
nota.NFe.infNFe.dest.enderDest.xLgr = "";
nota.NFe.infNFe.dest.enderDest.nro = 0;
nota.NFe.infNFe.dest.enderDest.xCpl = "";
nota.NFe.infNFe.dest.enderDest.xBairro = "";
nota.NFe.infNFe.dest.enderDest.cMun = 0;
nota.NFe.infNFe.dest.enderDest.xMun = "";
nota.NFe.infNFe.dest.enderDest.UF = "";
nota.NFe.infNFe.dest.enderDest.CEP = 0;
nota.NFe.infNFe.dest.enderDest.cPais = 0;
nota.NFe.infNFe.dest.enderDest.xPais = "";
nota.NFe.infNFe.dest.enderDest.fone = 0;
nota.NFe.infNFe.dest.indIEDest = 0;
/*Aqui o problema ao instanciar a classe ide, tentei fazer a instancia usando a linha que está comentada abaixo e deu o mesmo erro*/
//nota.NFe.infNFe.det = new nfeProcNFeInfNFeDet[1];
nota.NFe.infNFe.det[1]= new nfeProcNFeInfNFeDet();
nota.NFe.infNFe.det[0].nItem = 1;
/*fim do campo com problemas*/
XmlSerializer ser = new XmlSerializer(typeof(nfeProc));
FileStream arquivo = new FileStream("xmls/serializado.xml",FileMode.OpenOrCreate);
ser.Serialize(arquivo,nota);
MessageBox.Show("Xml Gerado");
}
catch (Exception erro)
{
MessageBox.Show(erro.Message,"Erro ao gerar NFe");
}
}
Example Nfe XML link View or Download XML
I only installed the class det
and assigns a value to the attribute nItem
.
This exception (Nullreferenceexception) is triggered when you try to access something "something" that has value
null
, or that you have never assigned any value to her. Go thrashing your code, put breakpoints and notice what is the "thing" (class, variable, property) that you try to access whose value isnull
. If you can, I recommend reading from here. In your case, from the little information I have, it may be that thenota
is empty when you access it, can be thenota.NFe
, as well as thenota.NFe.infNFE
or thenota.NFe.infNFe.det[n]
.– vinibrsl
Your note.NFe.infNFe.det, is an array of ? as it is being created in its class nfeProcNFeInfNFe?
– Marco Souza
Yes what is empty is the note.NFe.infNFe.det[n] but Exception occurs when you create the instance note.NFe.infNFe.det[n]=new note.NFe.infNFe.det(); if you passed this point, then I would just add the values to the attributes of this object.
– RonyElias
@GOKU Ssjgod is an Array of nfeProcNFeInfNFe, follows the link of the https://drive.google.com/open?id=0B_S5T_wFQISqM2dQZ05ZNHh6dncproject
– RonyElias
Try generating the nfe classes with XSD.EXE to test, or test the classes in the Zeusnfe library. One more thing, Procnfe is the distribution xml, it already has the authorization protocol. To create the note you must use the Nfe class.
– Robss70
Consider adding an answer to your question, if any is satisfactory.
– vinibrsl
@Ronyelias Consider marking the answer as
aceita
, I’m glad I helped you.– rubStackOverflow