2

I am receiving the error:

System.NullReferenceException occurred HResult=-2147467261 Message=Object reference not set to an instance of an object when attempting to access a view.

The error is pointing to this line:

<span>@(d.AcceptPropagation == null ? string.Empty : "Accepts Propagation")</span> 

I set a breakpoint at the foreach loop and stepped through to find where the null was occurring. I do not receive an error until the 5th time through the loop and I am getting a null at DocumentTemplate. I thought I was handling the null correctly. Do I need additional null checks? Do I need to handle this differently? Below is the view. I also changed "" to string.Empty in the code.

@if (criteria != null)
        {
            foreach (var d in criteria)
            {
                <tr>
                    <td style="width: 80px;">
                        <a class="blue button edit-template" data-reveal-id="edit-form" id="@d.ID">Edit</a>
                    </td>
                    <td>
                        <strong>@(d.DocumentTemplate == null ? string.Empty: d.DocumentTemplate.Name)</strong><br />
                        <div>@d.GroupCode</div>
                        <span>@d.PlanCode</span>
                        <span>@d.SubPlanCode</span>
                        <span>@d.ActionCode</span>
                        <span>@d.AdmitTerm</span>
                        <span>@d.AdmitTypeCode</span>
                        <span>@d.ProgramCode</span>
                        <span>@d.ResidentCode</span>
                        <span>@(d.V19 == true ? "V19" : string.Empty)</span>
                        <span>@(d.A23 == true ? "A23" : string.Empty)</span>
                        <span>@(d.AcceptPropagation == null ? string.Empty : "Accepts Propagation")</span> 
                        <div><a href="@Url.Content(d.DocumentTemplate.Path)">@(Url.Content(d.DocumentTemplate.Path)==null? string.Empty : d.DocumentTemplate.Path)</a></div>
                    </td>
                </tr>
            }
        }
octavioccl
  • 35,665
  • 6
  • 76
  • 95
kidaiu
  • 91
  • 1
  • 10
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – usr Jul 07 '15 at 20:06

2 Answers2

1

You're not checking to see if DocumentTemplate is null in this line

<div><a href="@Url.Content(d.DocumentTemplate.Path)">@(Url.Content(d.DocumentTemplate.Path)==null? string.Empty : d.DocumentTemplate.Path)</a></div>
JamieD77
  • 13,358
  • 1
  • 15
  • 25
1

It is actually the line below the one you mentioned causing the issue. Razor view NullReferenceExceptions for some reason point toward the line above many times. Change the line below it to have the same null checks and it will work:

<div>
    @if(d.DocumentTemplate != null)
    {
        <a href="@Url.Content(d.DocumentTemplate.Path)">@d.DocumentTemplate.Path</a>    
    }
</div>

See, this link where a user initially found the issue.

Community
  • 1
  • 1
Brad C
  • 2,619
  • 19
  • 32
  • This solved it. I knew I was not checking something correctly, was unsure which DocumentTemplate line it was. Thanks! – kidaiu Jul 07 '15 at 19:14