How to treat this Nullreferenceexception

Asked

Viewed 488 times

0

I don’t understand why I’m getting one NullReferenceException, in that beautiful code:

pessoaJuridica.InscricaoEstadual = viewmodel.PessoaJuridica.InscricaoEstadual;

What I’m doing wrong?

Follows entire 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: Reference of object not defined for an object instance.

Class Personal:

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; }
    }
  • you could post the complete error message?

  • 2

    Your viewmodel.PessoaJuridica It’s null, stupid. You gotta see this.

  • I’ve debugged the value of viewmodel.PessoaJuridica.InscricaoEstadualand the value is coming.

  • in which line the error?

  • @Dorathoto in that pessoaJuridica.InscricaoEstadual = viewmodel.PessoaJuridica.InscricaoEstadual;

  • 1

    or the viewmodel, or PessoaJuridica are null, it would be good to post more code there, even because if you are doing async things complicate a little more

Show 1 more comment

4 answers

1

viewmodel.PessoaJuridica is empty, so it’s not possible for you to access his sublevel.. can give a breack point and check. do something like var teste = viewmodel.PessoaJuridica; and see

and in the end it will stay like this

if(viewmodel.PessoaJuridica != null)
{ 
  pessoaJuridica.InscricaoEstadual = viewmodel.PessoaJuridica.InscricaoEstadual;
}

1

Check if viewmodel.Personal is void.

if(viewmodel.PessoaJuridica != null)
{
    pessoaJuridica.InscricaoEstadual = viewmodel.PessoaJuridica.InscricaoEstadual;
}

1

The variable pessoaJuridica was not instantiated

Instate:

var pessoaJuridica = new PessoaJuridica();

If you want to assign:

var pessoaJuridica = p;

Enter attributes in your class constructor:

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; }
}
  • Okay, but when I wrote this excerpt: p = new PessoaJuridica();&#xA; // atribui a pessoa da viewmodel para o objeto pessoa&#xA; p = viewmodel.Pessoa;&#xA; var pessoaJuridica = p as PessoaJuridica;Would not be instantiating P and add in Personal?

  • If you have said that your viewmodel.PersonJuridica.Subscriptionstate is not null, then the personal variable Uridic was not instantiated.

  • Okay thanks, but how do I then assign the data from viewModel.Pessoa for the legal person?

  • How is your Personal class? could you put the code?

  • From what I understood it would be enough for you to do: p = new Personjuridica(); p = viewmodel.Personjuridical;

  • edited the question

  • I modified my answer.

Show 2 more comments

0

Ok, the Nullreferenceexception is raised in the following line:

pessoaJuridica.InscricaoEstadual = viewmodel.PessoaJuridica.InscricaoEstadual;

This means that the indirect operator . is being called in a null object. Like the objects receiving the operator . sane pessoaJuridica, viewmodel and viewmodel.PessoaJuridica, the culprit is necessarily one of the three.

Let’s see if we can trace 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: she is a reinterpretation of the variable p (type Pessoa) as PessoaJuridica. This, in turn, is instantiated with an object Pessoa empty on line 8, and this object is immediately discarded in favor of the member Pessoa of the variable viewmodel, which is a function argument. So, if viewmodel come up with a Pessoa uninspired, p becomes null on line 10 and raises the exception on line 13.

Let us examine, then, viewmodel: this is the argument of the function, but as it has had other members accessed (starting with viewmodel.TipoPessoa in line 6), then it cannot be null, since it is not assigned in the method.

Finally, viewmodel.PessoaJuridica, who is a member of viewmodel. This is also not assigned in the method, so the only possibility is to get it null in the function.

In conclusion, the viewmodel that you are receiving as parameter must have or the property Pessoa or the property PessoaJuridica null (or both). I would then check the code that instance this viewmodel before he reaches the method...

Browser other questions tagged

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