How to handle the Object reference error not set for an object instance

Asked

Viewed 1,933 times

0

Create 2 new columns in my table dbo.FormaPgto - FPNFCeCpnjCred and FPBandeira both Allow Null

When I consult a payment method, give the following error in the fields
txtFPCnpj.Text = objFPagto.FPNFCeCpnjCred.ToString(); txtBandeira.Text = objFPagto.FPBandeira.ToString();

Undefined object reference for an object instance.

I would like to know some way to deal with that mistake, even if the information is Null

Code(Code Behind):

 protected void SetarPropriedadesPagina(Formas_Pagamento objFPagto)
{
    try
    {
        txtCodigo.Text = objFPagto.FPCodigo.ToString();
        txtDescricao.Text = objFPagto.FPDescricao.Trim();
        txtEmpresa.Text = objFPagto.FPEmpresa.Trim();
        ddlEmpresa.SelectedValue = objFPagto.FPEmpresa.Trim();
        txtBanco.Text = objFPagto.FPCodBanco.Trim();
        txtAgencia.Text = objFPagto.FPAgencia.Trim();
        txtConta.Text = objFPagto.FPConta.Trim();
        txtCarteira.Text = objFPagto.FPCarteira.Trim();
        txtFPCnpj.Text = objFPagto.FPNFCeCpnjCred.ToString();
        txtBandeira.Text = objFPagto.FPBandeira.ToString();
        ddlFormaPgto.SelectedValue = objFPagto.FPNFCeCodPagto.Trim();
        ddlCodBandeira.SelectedValue = objFPagto.FPNFCeCodBandeira.Trim();
        ckbCartaoCredito.Checked = (string.Compare(objFPagto.FPCartCred, "S", true) == 0 ? true : false);
        ckbPgtoAntecipado.Checked = (string.Compare(objFPagto.FPPgAntecipado, "S", true) == 0 ? true : false);
        ckbPedidoWeb.Checked = (string.Compare(objFPagto.FPPedidoWeb, "S", true) == 0 ? true : false);
        ckbCadastroWeb.Checked = (string.Compare(objFPagto.FPCadastroWeb, "S", true) == 0 ? true : false);
        ckbPedido.Checked = (string.Compare(objFPagto.FPPedido, "S", true) == 0 ? true : false);
        ckbCobranca.Checked = (string.Compare(objFPagto.FPCobranca, "S", true) == 0 ? true : false);
        ckbPgtoMult.Checked = (string.Compare(objFPagto.FPPgtoMult, "S", true) == 0 ? true : false);
        if (objFPagto.FPCodBandeira > 0)
        {
            txtCodBandeira.Text = objFPagto.FPCodBandeira.ToString();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }

}

Class(only part of the payment method):

public int Query { Try {

            strSQL = new StringBuilder();
            strSQL.Append("SELECT * FROM FormaPagto WHERE FPEmpresa = @FPEmpresa AND FPCodigo = @FPCodigo ");
            SqlParameter[] parametros = {new SqlParameter("@FPEmpresa", codEmpresa),
                                         new SqlParameter("@FPCodigo", codFP)};
            dt = SqlDAO.consultarSQL(strSQL, parametros);

            if (dt.Rows.Count > 0)
            {
                this.FPEmpresa = (string)dt.Rows[0]["FPEmpresa"];
                this.FPCodigo = Convert.ToInt32(dt.Rows[0]["FPCodigo"]);
                if (dt.Rows[0]["FPDescricao"] != DBNull.Value)
                {
                    this.FPDescricao = (string)dt.Rows[0]["FPDescricao"];    
                }                                        
                this.FPPgAntecipado = (string)dt.Rows[0]["FPPgAntecipado"];
                this.FPPedido = (string)dt.Rows[0]["FPPedido"];
                this.FPCobranca = (string)dt.Rows[0]["FPCobranca"];                    
                if (dt.Rows[0]["FPCodBanco"] != DBNull.Value)
                {
                    this.FPCodBanco = (string)dt.Rows[0]["FPCodBanco"];
                }
                if (dt.Rows[0]["FPAgencia"] != DBNull.Value)
                {
                    this.FPAgencia = (string)dt.Rows[0]["FPAgencia"];
                }
                if (dt.Rows[0]["FPConta"] != DBNull.Value)
                {
                    this.FPConta = (string)dt.Rows[0]["FPConta"];
                }
                if (dt.Rows[0]["FPCarteira"] != DBNull.Value)
                {
                    this.FPCarteira = (string)dt.Rows[0]["FPCarteira"];
                }
                if (dt.Rows[0]["FPCartCred"] != DBNull.Value)
                {
                    this.FPCartCred = (string)dt.Rows[0]["FPCartCred"];
                }                    
                if (dt.Rows[0]["FPPedidoWeb"] != DBNull.Value)
                {
                    this.FPPedidoWeb = (string)dt.Rows[0]["FPPedidoWeb"];
                }
                if (dt.Rows[0]["FPCadastroWeb"] != DBNull.Value)
                {
                    this.FPCadastroWeb = (string)dt.Rows[0]["FPCadastroWeb"];
                }
                if (dt.Rows[0]["FPIdInc"] != DBNull.Value)
                {
                    this.FPIdInc = (string)dt.Rows[0]["FPIdInc"];
                }
                if (dt.Rows[0]["FPDtInc"] != DBNull.Value)
                {
                    this.FPDtInc = dt.Rows[0]["FPDtInc"].ToString();
                }
                if (dt.Rows[0]["FPIdAlt"] != DBNull.Value)
                {
                    this.FPIdAlt = (string)dt.Rows[0]["FPIdAlt"];
                }
                if (dt.Rows[0]["FPDtAlt"] != DBNull.Value)
                {
                    this.FPDtAlt = dt.Rows[0]["FPDtAlt"].ToString();
                }

                if (dt.Rows[0]["FPCodBandeira"] != DBNull.Value)
                {
                    this.FPCodBandeira = Convert.ToInt32(dt.Rows[0]["FPCodBandeira"]);
                } 
                if(dt.Rows[0]["FPPgtoMult"] != DBNull.Value)
                {
                    this.FPPgtoMult = (string)dt.Rows[0]["FPPgtoMult"];
                }
                if(dt.Rows[0]["FPNFCeCpnjCred"] != DBNull.Value)
                {
                    this.FPNFCeCpnjCred = (string)dt.Rows[0]["FPNFCeCpnjCred"];
                }
                if(dt.Rows[0]["FPBandeira"] != DBNull.Value)
                {
                    this.FPBandeira = (string)dt.Rows[0]["FPBandeira"];
                }
                if(dt.Rows[0]["FPNFCeCodPagto"] != DBNull.Value)
                {
                    this.FPNFCeCodPagto = (string)dt.Rows[0]["FPNFCeCodPagto"];
                }
                if(dt.Rows[0]["FPNFCeCodBandeira"] != DBNull.Value)
                {
                    this.FPNFCeCodBandeira = (string)dt.Rows[0]["FPNFCeCodBandeira"];
                }
            }
            return dt.Rows.Count;
        }
        catch (SqlException ex)
        {
            throw ex;
        }
  • Ask the question the class code Formas_Pagamento to get to the bottom of this problem.

  • 1

    @Cypherpotato edited.

  • instead of: ckbPgtoMult.Checked = (string.Compare(objFPagto.FPPgtoMult, "S", true) == 0 ? true : false); it’s no better: ckbPgtoMult.Checked = objFPagto.FPPgtoMult == "S"; ?

1 answer

1


Your code is a little strange, but to treat 'Object reference not defined for an instance of an object. 'You just test if the object is null:

if (objFPagto != null)
{

    txtCodigo.Text = objFPagto.FPCodigo == null ? "Nulo" : objFPagto.FPCodigo.ToString();
    txtDescricao.Text = objFPagto.FPDescricao == null ? "Nulo tambem" : objFPagto.FPDescricao.Trim();

...
}

and so on... but you can start by taking the try{} catch{} which is not serving for anything or treating the exception, then improving the syntax of:

ckbPgtoMult.Checked = (string.Compare(objFPagto.FPPgtoMult, "S", true) == 0 ? true : false);

for

ckbPgtoMult.Checked = (objFPagto.FPPgtoMult == "S");

Browser other questions tagged

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