Object reference not defined for an object instance. C# (on installation)

Asked

Viewed 748 times

0

By the error message, it was understood that there is an object not instantiated, in which I would be making reference.

Error:

inserir a descrição da imagem aqui

However, this same application works perfectly on the development computer, when I install the installer and another machine this error occurs.

------------------Specifications:

Application: Desktop.

Visual Studio:2013.

Methods used to generate the instator:

1º Publish(with the requisite requirements).

2º Creation of a Setup-Wizard project (Visual Studio 2010 style).


In my research in the forum itself and in others, this error occurs during development, not in installation. Does anyone know the really correct method of generating the installer in Visual 2013

Carregarpdv():

private void CarregarPDV()
    {
        PDV = Model_PDV.Buscar(Ctrl_Configuracao.PDV.Id);
        txtPDV.Text = PDV.Descritivo;
        if (PDV.Status.Equals("False"))
        {
            txtSituacao.Text = "FECHADO";
            btnFechar.Enabled = false;
            btAbrir.Enabled = true;
            Ctrl_Configuracao.CaixaStatus = false;
            txtSituacao.BackColor = System.Drawing.Color.Red;
        }
        else
        {
            txtSituacao.Text = "ABERTO";
            btnFechar.Enabled = true;
            btAbrir.Enabled = false;
            Ctrl_Configuracao.CaixaStatus = true;
            txtSituacao.BackColor = System.Drawing.Color.Green;
        }
        Model_LogPDV LogCaixa = new Model_LogPDV();
        LogCaixa = Model_LogPDV.BuscaLog(Ctrl_Configuracao.PDV.Id);
        if (LogCaixa.ID_VENDA_START.Equals(""))
        {
            Ctrl_Configuracao.JaVendeu = false;
        }
    }

Bank class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Gaivota.Controller;
using Gaivota.View;
using Gaivota.Model;

namespace Gaivota.Model
{

    class Banco
    {
       public static SqlConnection Conexao = new SqlConnection("Data Source=" + Ctrl_Configuracao.Config.DataLink
             + "; Initial Catalog=Gaivota; User Id=sa; Password=123456; Integrated Security=false");

        public static void Abrir()
        {             
            try
            {
                Conexao.Open();
            }
            catch (Exception)
            {

                Ctrl_Msg.MensagemInforma(Ctrl_Msg.ErroAbrirBanco);
            }

        }
        public static void Fechar()
        {
            try
            {
                Conexao.Close();
            }
            catch (Exception)
            {

            }

        }
        public static string BuscarCodigoNext(string Tabela)
        {
            string CodigoNovo = "0";
            string sqlstring = "";
            try
            {

                sqlstring = "select ID from " + Tabela + " order by ID desc";
                SqlCommand Comando = new SqlCommand(sqlstring, Banco.Conexao);
                SqlDataReader Ler = Comando.ExecuteReader();

                if (Ler.Read())
                {
                    CodigoNovo = Ler["ID"].ToString();
                    Ler.Close();
                }
                CodigoNovo = (Convert.ToInt32(CodigoNovo) + 1).ToString();
            }
            catch (Exception)
            {

            }
            return (CodigoNovo);

        }
        public static string BuscarCodigoAtual(string Tabela)
        {
            string CodigoAtual = "0";
            string sqlstring = "";
            try
            {

                sqlstring = "select ID from " + Tabela + " order by ID desc";
                SqlCommand Comando = new SqlCommand(sqlstring, Banco.Conexao);
                SqlDataReader Ler = Comando.ExecuteReader();

                if (Ler.Read())
                {
                    CodigoAtual = Ler["ID"].ToString();
                    Ler.Close();
                }
                CodigoAtual = (Convert.ToInt32(CodigoAtual)).ToString();
            }
            catch (Exception)
            {

            }
            return (CodigoAtual);

        }


    }


}

class of settings:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gaivota.Model;
using System.Windows.Forms;
using System.Xml;

namespace Gaivota.Controller
{
    class Ctrl_Configuracao
    {
        public static bool RespostaBanco;

        public static bool EstaBuscando;

        public static bool CaixaStatus;

        public static bool JaVendeu = true;

        public static Model_Produto Produto = new Model_Produto();

        public static Model_PDV PDV = new Model_PDV();

        public static Model_Usuario Usuario = new Model_Usuario();

        public static Model_Configuracao Config = new Model_Configuracao();

        public static Model_RelPDV RelPDV = new Model_RelPDV();

        public static string XML_Link = @"C:\Gaivota\config.xml";

        public static  Model_Configuracao LerXML()
        {
            Model_Configuracao Configuracao = new Model_Configuracao();
            Configuracao.PortaCOM = null;
            try
            {
                //Cria uma instância de um documento XML
                XmlDocument oXML = new XmlDocument();

                //carrega o arquivo XML
                oXML.Load(XML_Link);

                //Lê o filho de um Nó Pai específico 
                Configuracao.ModeloPrint = oXML.SelectSingleNode("config").ChildNodes[0].InnerText;
                Configuracao.PortaCOM = oXML.SelectSingleNode("config").ChildNodes[1].InnerText;
                Configuracao.Velocidade = oXML.SelectSingleNode("config").ChildNodes[2].InnerText;
                Configuracao.DataLink = oXML.SelectSingleNode("config").ChildNodes[3].InnerText;
            }
            catch (Exception)
            {
                Ctrl_Msg.MensagemInforma(Ctrl_Msg.ErroLerXML);
            }

            return Configuracao;
        }

        public static void SetConfig()
        {       

         Ctrl_Configuracao.Config  = Ctrl_Configuracao.LerXML();  


        }
        public static bool SomenteNumeros(int key)
        {
            bool x = true;

            if (key < 48 || key > 57)
            {
                if (key == 8)
                {
                    x = false;
                }
                else
                {
                    x = true;
                }
            }
            else
            {
                x = false;
            }

            return (x);
        }

    }
}
  • 1

    Post your frmMenu_load(..) method and load PDV(...)

  • Maybe you need other parts of the class used in frmMenu. The problem may not even be where it is, but where it is used. No use testing to see if it works, you have to test to see if it doesn’t work.

  • Buddy, I posted it, but it’s like the app kills my configuration class.

  • @Jonathanhenrique This is certainly a programming error. I can already say that there are several strange and problematic things in this code. Which is the line where the error occurs. Show it, the number has no way of knowing which is.

  • What returns in Model_PDV.Buscar()? Better still would put it. The same valley Model_LogPDV.BuscaLog().

  • Look, I appreciate the attention. but I don’t think I expressed myself right. The system WORKS on the machine I am developing, when it generates the installer this error occurs.

  • @Jonathanhenrique I will be very honest with you, you may not like, but right now is what I can do best for you. His code is quite confusing and it is very easy for him to be getting lost in a certain situation. Testing in a situation and functioning does not mean anything other than that in that situation it works. Being right is a very different thing. If I was right, it would work in every situation. If you post what I asked for (and probably would have to ask for other things because that’s how you think it’s wrong) I could help more and tell you what has to change in the code.

Show 2 more comments

1 answer

-1

The problem may be in the method Equals(...), it is necessary more details of your problem to know exactly what happens

The problem could be here:

PDV.Status.Equals("False");

Or here:

LogCaixa.ID_VENDA_START.Equals("");

If the PDV.Status or LogCaixa.ID_VENDA_START is null the mentioned error will occur as the object is not instantiated.

I recommend you replace the .Equals(...) for ==

So in your method it would look like this:

if (PDV.Status=="False")
{
   ...
}
  • 2

    Or it could be in several other lines.

  • Probably more details are needed. That was a detail I saw that might be the cause of the error.

  • 1

    So wait for him to put, still can not answer.

  • Dear, if the error was in the if (PDV.Status="False"). Wasn’t it supposed to not work on both machines ? And not only on the test ?

Browser other questions tagged

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