How to handle this NullReferenceException

0

I do not understand the reason for receiving a NullReferenceException , in this beautiful code:

pessoaJuridica.InscricaoEstadual = viewmodel.PessoaJuridica.InscricaoEstadual;

What am I doing wrong?

Follow whole code

 public async Task<ActionResult> Create(ClienteViewModel viewmodel)
        {
           // verifica se o Model CLIENTEVIEWMODEL está válido 
            if (ModelState.IsValid)
            {
                Pessoa p;
                //verifica o tipo de pessoa para add no entity
                if (viewmodel.TipoPessoa.Equals(Models.Enum.TipoPessoa.Juridica))
                {
                    //cria a pessoa juridica
                    p = new PessoaJuridica();
                    // atribui a pessoa da viewmodel para o objeto pessoa
                    p = viewmodel.Pessoa;
                    var pessoaJuridica = p as PessoaJuridica;

                    pessoaJuridica.InscricaoEstadual = viewmodel.PessoaJuridica.InscricaoEstadual;

                    db.PessoaJuridica.Add(pessoaJuridica);
                }

EDIT

full error message:

  

Exception Details: System.NullReferenceException:   object not set to an instance of an object.

Class PersonJuridica :

public class PessoaJuridica : Pessoa
{
    [DisplayName("Inscrição Estadual")]
    [StringLength(20)]
    public String InscricaoEstadual { get; set; }
}

ViewModel :

public class ClienteViewModel
   {

        public Pessoa Pessoa { get; set; }

        public TipoPessoa TipoPessoa { get; set; }

        public PessoaJuridicaViewModels PessoaJuridica { get; set; }
    }
    
asked by anonymous 29.06.2017 / 21:53

4 answers

1

viewmodel.PessoaJuridica is empty, so it is not possible to access its sub-level .. you can give a breack point and verify. do something like var teste = viewmodel.PessoaJuridica; and see

and in the end it will look like this

if(viewmodel.PessoaJuridica != null)
{ 
  pessoaJuridica.InscricaoEstadual = viewmodel.PessoaJuridica.InscricaoEstadual;
}
    
29.06.2017 / 22:06
1

Check if viewmodel.Jessid is invalid.

if(viewmodel.PessoaJuridica != null)
{
    pessoaJuridica.InscricaoEstadual = viewmodel.PessoaJuridica.InscricaoEstadual;
}
    
29.06.2017 / 22:06
1

The variable pessoaJuridica was not instantiated

Instance:

var pessoaJuridica = new PessoaJuridica();

If you want to assign:

var pessoaJuridica = p;

Instantiate the attributes in the constructor of your class:

public class ClienteViewModel
{
    public ClienteViewModel() 
    {
        Pessoa = new Pessoa();
        TipoPessoa = new TipoPessoa();
        PessoaJuridica = new PessoaJuridicaViewModels();
    }

    public Pessoa Pessoa { get; set; }

    public TipoPessoa TipoPessoa { get; set; }

    public PessoaJuridicaViewModels PessoaJuridica { get; set; }
}
    
29.06.2017 / 22:07
0

Ok, the NullReferenceException is raised in the following line:

pessoaJuridica.InscricaoEstadual = viewmodel.PessoaJuridica.InscricaoEstadual;

This means that the . indirection operator is being called on a null object. Since the objects that receive the . operator are pessoaJuridica , viewmodel and viewmodel.PessoaJuridica , the culprit is necessarily one of the three.

Let's see if we can draw from where each one comes from:

public async Task<ActionResult>
Create(ClienteViewModel viewmodel) {
    // verifica se o Model CLIENTEVIEWMODEL está válido 
    if (ModelState.IsValid) {
        Pessoa p;
        //verifica o tipo de pessoa para add no entity
        if (viewmodel.TipoPessoa.Equals(Models.Enum.TipoPessoa.Juridica)) {
            //cria a pessoa juridica
            p = new PessoaJuridica();
            // atribui a pessoa da viewmodel para o objeto pessoa
            p = viewmodel.Pessoa;
            var pessoaJuridica = p as PessoaJuridica;

            pessoaJuridica.InscricaoEstadual = viewmodel.PessoaJuridica.InscricaoEstadual;

            db.PessoaJuridica.Add(pessoaJuridica);
        }
        /* O método continua... */
    }
}

Let's start with pessoaJuridica : it is a reinterpretation of the variable p (type Pessoa ) as PessoaJuridica . This, in turn, is instantiated with an empty% object on line 8, and this object is immediately discarded in favor of the Pessoa member of the Pessoa variable, which is an argument of the function. So if viewmodel comes with a viewmodel not instantiated, Pessoa becomes p on line 10 and raises the exception on line 13.

Let's now examine null : this is the argument of the function, but since it already had other members accessed (starting with viewmodel on line 6), then it can not be null, since it is not assigned in the method .

Finally, viewmodel.TipoPessoa , which is a member of viewmodel.PessoaJuridica . This is also not assigned in the method, so the only possibility is that it gets null in the function.

By the way, the% w / o% you're receiving as a parameter must have or property viewmodel or property viewmodel nulls (or both) . I would then check the code that instantiates this Pessoa before it gets into the method ...

    
29.06.2017 / 22:46