error "Undefined object reference to an instance of an object." when displaying a null value in the view

Asked

Viewed 4,575 times

2

Within C# MVC5, I am creating a user registration form with several fields. One of them is the Function (Bonus) field, which the user may or may not have.

Follows the User class:

public class Usuarios
{

    public int Id { get; set; }

    [Required, StringLength(60)]
    public string Nome { get; set; }

    [DisplayName("Matrícula"),StringLength(7)]
    public string Matricula {get;set;}

    [DisplayName("DV"),Required]
    public int MatriculaDV {get;set;}

    [StringLength(20)]
    public string OAB {get;set;}

    [DisplayName("Gênero")]
    public Genero Genero { get; set; } 
    public bool Ativo {get;set;}

    [DisplayName("Data de Nascimento"), DisplayFormat(DataFormatString="{0:d}")]
    public DateTime DataNascimento { get; set; }

    [DisplayName("Unidade")]
    public virtual int? UnidadesID {get;set;}
    public virtual Unidades Unidade {get;set;}

    [DisplayName("Cargo")]
    public virtual int? CargosID { get; set; }
    public virtual Cargos Cargo { get; set; }

    [DisplayName("Função")]
    public virtual int? FuncoesID {get;set;} //eis a funcao
    public virtual Funcoes Funcao {get;set;}

}

And the class Functions:

public class Funcoes
{
    public int Id { get; set; }
    public int Codigo { get; set; }
    [StringLength(70)]
    public string Funcao { get; set; }

    public virtual IList<Usuarios> Usuario { get; set; }
}

The inclusion form allows adding a new record with no function.

However, when trying to list them, the view containing the table generates the error below on the line where it tries to display a null function record (line @usuario.Funcao.Function):

An Exception of type 'System.Nullreferenceexception' occurred in App_web_tneo00cv.dll but was not handled in user code

Additional information: Object reference not defined for a instance of an object.

Follow the User Index view:

@model IList<Aplicacoes.Entidades.Usuarios>

@{
    ViewBag.Title = "Usuários";
}
<h4>Lista de Usuários</h4>
<a class="btn btn-primary" href="Usuario/Form">Novo Usuário</a>
<a class="btn btn-default" href="Unidades/Index">Unidades</a>
<a class="btn btn-default" href="Cargo/Index">Cargos</a>
<a class="btn btn-default" href="Funcao/Index">Funções</a>
@*@Html.ActionLink("Novo Usuário", "Form", "Usuario", new { @class="btn btn-primary"})
@Html.ActionLink("Unidades", "Index", "Unidades", new { @class = "btn btn-default" })*@
<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th>Matricula</th>
            <th>DV</th>
            <th>Nome</th>
            <th>Cargo</th>
            <th>Função</th>
            <th>Unidade</th>
            <th>OAB</th>
            <th>Data Nasc.</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var usuario in @Model.OrderBy(usuario => usuario.Nome))
        {
            <tr>
                <td>@usuario.Matricula.ToUpper()</td>
                <td>@usuario.MatriculaDV</td>
                <td>@usuario.Nome.ToUpper()</td>
                <td>@usuario.Cargo.Cargo</td>
                <td>@usuario.Funcao.Funcao</td> //o erro ocorre aqui
                <td>@usuario.Unidade.Sigla</td>
                <td>@usuario.OAB</td>
                <td>@usuario.DataNascimento.ToShortDateString()</td>

            </tr>
        }
    </tbody>
</table>

I tried to get around that way in the View itself, but it didn’t work:

if(@usuario.Funcao == null)
{
    <td>-</td>
}else{
    <td>@usuario.Funcao.Funcao</td> //o erro acontece aqui novamente
}

Follow the User Controller to verify:

public class UsuarioController : Controller
{
    // GET: Usuario
    private UsuarioDAO uDao;
    private CargosDAO cDao;
    private FuncoesDAO fDao;
    private UnidadesDAO udDao;

    public UsuarioController(UsuarioDAO uDao, CargosDAO cDao, FuncoesDAO fDao, UnidadesDAO udDao)
    {
        this.uDao = uDao;
        this.cDao = cDao;
        this.fDao = fDao;
        this.udDao = udDao;
    }

    public ActionResult Index()
    {
        IList<Usuarios> usuarios = uDao.Lista();
        return View(usuarios);
    }

    public ActionResult Form()
    {
        ViewBag.Cargos = cDao.Lista().OrderBy(Cargos =>Cargos.Cargo);
        ViewBag.Funcoes = fDao.Lista().OrderBy(Funcoes => Funcoes.Funcao);
        ViewBag.Unidades = udDao.Lista().OrderBy(Unidades => Unidades.Nome);

        return View();
    }

    public ActionResult Adiciona(Usuarios usuario)
    {
        uDao.Adiciona(usuario);
        return RedirectToAction("Index");
    }

    public ActionResult Remove(int id)
    {
        Usuarios usuario = uDao.BuscaPorId(id);
        uDao.Remove(usuario);
        return RedirectToAction("Index");
    }
    public ActionResult Detalhes(int id)
    {
        Usuarios usuario = uDao.BuscaPorId(id);
        return View(usuario);
    }

    public ActionResult Atualiza(Usuarios usuario)
    {
        uDao.Atualiza(usuario);
        return RedirectToAction("Index");

    }

}

I understand the error prompts me to initiate the variable first. But I can’t imagine where I can start this variable, since it’s a "class within a class".

  • Guys, I’m still in need of help to overcome this problem. Thank you :)

  • Have you checked if all users on the list have Functions? In your view try something like this: <td>@usuario.Funcao?.Funcao</td>

  • Where’s your list method ()

2 answers

1

Have you tried adding a constructor to the user class?

  public Usuarios()
    {
        this.FuncoesID = 0;
        this.Funcoes = new Funcoes();
    }
  • Good morning, I made an attempt but with this constructor he cleaned all user functions that have this information =( I tried public Usuarios()&#xA; {&#xA; if (this.Funcao == null)&#xA; {&#xA; this.FuncoesID = 0;&#xA; this.Funcao = new Funcoes();&#xA; }&#xA; } but it didn’t work tb

  • You can try building a constructor for the function class by starting the error fields.

  • I tried to public Funcoes()&#xA; {&#xA; if (this.Funcao== null)&#xA; {&#xA; this.Id = 0;&#xA; this.Funcao = "-";&#xA; this.Codigo = 0;&#xA; }&#xA; } in the function class, but it didn’t work out either. Returned the same error =/ (error mentioned in question, did not list users without function names)

  • @romalito if you remove the piece of code from the view, does it work? Just to make sure it’s this...

  • It does work. Actually, if I delete the record in the table with the Function field that is null, the list normally processes.

  • I had this problem with SQL and C#, so I started to send a value "0" (zero) or " (space) where it would be null not to error the return.

Show 1 more comment

0

You can use ? to print possible null objects.

   <tbody>
    @foreach (var usuario in @Model.OrderBy(usuario => usuario.Nome))
    {
        <tr>
            <td>@usuario.Matricula?.ToUpper()</td>
            <td>@usuario.MatriculaDV</td>
            <td>@usuario.Nome.ToUpper()</td>
            <td>@usuario.Cargo?.Cargo</td>
            <td>@usuario.Funcao?.Funcao</td> //o erro ocorre aqui
            <td>@usuario.Unidade?.Sigla</td>
            <td>@usuario.OAB</td>
            <td>@usuario.DataNascimento?.ToShortDateString()</td>

        </tr>
    }
</tbody>

Browser other questions tagged

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