Take the 2-minute tour ×

I'm passing an object to a view and am getting a null reference exception, but the object is not null.

enter image description here

In the Action I have this simple code:

return View(db.Users.First());

View code:

    @model User
    @using DG.WEB.Models

        ViewBag.SecondTitle = "Ver";
        ViewBag.MostraEditarTab = "false";
        ViewBag.MostraApagarTab = "false";
        ViewBag.Tab = "tab2";
        Layout = "~/Views/Conds/_Layout.cshtml";

    <div class="container">
        @if (Model != null)

            <div id="container_atalhos">
                <div class="btn-group">
                  <a href="#" class="btn btn-mini"><i class="icon-th-large"></i>&nbsp;Nova frac</a>
                  <a href="#" class="btn btn-mini"><i class="icon-fire"></i>&nbsp;Nova Ocorrência</a>
                  <a href="#" class="btn btn-mini"><i class="icon-shopping-cart"></i>&nbsp;Novo Fornecedor</a>
                  <a href="#" class="btn btn-mini"><i class="icon-file"></i>&nbsp;Novo documento</a>
                  <a href="#" class="btn btn-mini"><i class="icon-pencil"></i>&nbsp;Editar</a>
                  <a href="#" class="btn btn-warning btn-mini"><i class="icon-remove-circle icon-white"></i>&nbsp;Apagar</a>
           @:Not found

EDIT: I just test a small code that prints the name of the first user in the view. And it happens to me the same error! The try / catch apparently works badly ... I do not understand.

Look at the picture enter image description here

EDIT2: I found the error. In the layout page there was an error and for some reason visual studio didnt detect it.

Thanks to all.

share|improve this question
can you paste your remaining part of view please ? –  Shyju Mar 16 '13 at 1:31
Is Nome simply a public string in Model, or is it a property? I'm wondering if, when you access the setter on Nome (assuming there is a setter), you might be accessing some other variable that is null. –  Grant Winney Mar 16 '13 at 2:02
Name is a property –  rmlm Mar 16 '13 at 2:10
I added the code of view –  rmlm Mar 16 '13 at 2:11
In your ALL your images it says "Nome" not "Name" (second letter is 'o' not 'a') ¿Is that correct? –  Luxspes Mar 16 '13 at 2:55
show 2 more comments


Your Answer



Browse other questions tagged or ask your own question.