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

0

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

When I go to a form of payment, it gives the following error in the fields: txtFPCnpj.Text = objFPagto.FPNFCeCpnjCred.ToString(); txtBandeira.Text = objFPagto.FPBandeira.ToString();

  

Object reference not set to an instance of an object.

I would like to know some way to handle this error, even though the information is Null

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 method that queries the form of payment) :

public int QueryForPath (string codEmpresa, int codFP)         {             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;
        }
    
asked by anonymous 02.08.2017 / 22:04

1 answer

1

Your code is kind of strange, but to treat 'Object reference not set to an instance of an object.' it's just you 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 ... maas can start by taking the try{} catch{} that is not serving at all or treat the exception, then improve the syntax of:

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

for

ckbPgtoMult.Checked = (objFPagto.FPPgtoMult == "S");
    
02.08.2017 / 23:41