Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange NullRefereneceException in Razor helper
    primarykey
    data
    text
    <p>I'm using Razor Helpers in a C# <strong><em>Web Forms</em></strong> application. </p> <p>The following code compiles and renders A-OK when called:</p> <pre><code>@helper MemberListItem(string firstname, string lastname, string avatarUrl) { &lt;li&gt; @firstname @lastname @avatarUrl &lt;/li&gt; } </code></pre> <p>Output (two calls to the helper):</p> <pre><code>&lt;li&gt;Bryan Arnold ../../Resources/Images/Placeholders/generic-user-image.jpg&lt;/li&gt; &lt;li&gt;Doug Bland ../../Resources/Images/Placeholders/generic-user-image.jpg&lt;/li&gt; </code></pre> <p>But when I change the helper so that <code>avatarUrl</code> is placed in the <code>src</code> attribute of an <code>img</code> tag, I get a <code>NullReferenceException</code> on <code>firstname</code>. Yes, the <code>NullReferenceException</code> is on <code>firstname</code>.</p> <p>The following code compiles but throws a NRE when called:</p> <pre><code>@helper MemberListItem(string firstname, string lastname, string avatarUrl) { &lt;li&gt; @firstname @lastname &lt;img src="@avatarUrl"/&gt; &lt;/li&gt; } </code></pre> <p>Keep in mind that I am not changing a thing besides the placement of <code>@avatarUrl</code> in the helper.</p> <p>How do I get the image to display?</p> <p><strong>Update 1:</strong> I have also tried wrapping my <code>img</code> code in <code>&lt;text&gt;&lt;/text&gt;</code> (to no avail):</p> <pre><code>&lt;li&gt; @firstname @lastname &lt;text&gt; &lt;img src="@avatarUrl"/&gt; &lt;/text&gt; &lt;/li&gt; </code></pre> <p><strong>Update 2:</strong> Here is the error:</p> <pre><code>Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 11: { Line 12: &lt;li&gt; Line 13: @firstname @lastname Line 14: &lt;img src="@avatarUrl"/&gt; Line 15: &lt;/li&gt; Source File: RazorHelpers\Family.cshtml Line: 13 </code></pre> <p><strong>Update 3:</strong> I forgot to mention that I am defining the @helper in a separate file. Then, I am calling that helper from an aspx template like this: </p> <pre><code>&lt;%= @HelperFile.Helper(parameters).ToString() %&gt; </code></pre> <p>Also, I think @Luaan might be on to something. I have tried fiddling with <code>~</code> for relative pathing to my image files, thinking that Razor might be throwing a NRE because it cannot locate the file. It would appear that no setup with <code>~</code> works in my project. </p> <p><em>With a static file path:</em></p> <p>This works: </p> <pre><code>&lt;img src="/Resources/Images/Placeholders/generic-user-image.jpg"/&gt; </code></pre> <p>This doesn't (NullReferenceException): </p> <pre><code>&lt;img src="~/Resources/Images/Placeholders/generic-user-image.jpg"/&gt; </code></pre> <p><em>With a dynamic file path:</em></p> <p>This doesn't work (NullReferenceException): </p> <pre><code>&lt;img src="@avatarUrl"/&gt; </code></pre> <p>Neither does this (NullReferenceException): </p> <pre><code>&lt;img src="~@avatarUrl"/&gt; </code></pre> <p>Note: The <code>Resources</code> directory is in the root of my website.</p> <p><strong>Update 4:</strong></p> <p>I have deserted my original helper (the one mentioned at the beginning of this question) and gone with a pure ASPX template due to time constraints with my project. However, I am trying to use a Razor helper for a different feature in this application and I am having the same problem I have described prior. </p> <p>Here is a similar helper (source and compiled code). The helper is supposed to display a list of links that allow the user to sign-up/sign-in for my application using various identity providers (google, facebook, twitter, yahoo). The story is the same here; the helper executes without an error when I just print the img src but it throws a NullReferenceException when I put the img src into an actual <code>&lt;img/&gt;</code> tag.</p> <p><em>Source:</em></p> <pre><code>@helper ListGroupGrid(IEnumerable&lt;ExternalIdentityProvider&gt; providers) { &lt;div&gt; @foreach(var provider in providers){ @provider.Name &lt;img src="@provider.IconUrl"/&gt; } &lt;/div&gt; } </code></pre> <p><em>Compiled code (from Temporary ASP.NET Files in c:\Windows):</em></p> <pre><code>#pragma checksum "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "46B0FEE2042706017F4AE53D4EA612F3E73EDF8B" //------------------------------------------------------------------------------ // &lt;auto-generated&gt; // This code was generated by a tool. // Runtime Version:4.0.30319.18052 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // &lt;/auto-generated&gt; //------------------------------------------------------------------------------ namespace ASP.RazorHelpers { using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Web; using System.Web.Helpers; using System.Web.Security; using System.Web.UI; using System.Web.WebPages; using System.Web.WebPages.Html; #line 1 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" using Aqha.DatabaseHelpers; #line default #line hidden #line 2 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" using Aqha.RazorExtensions; #line default #line hidden #line 3 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" using DevExpress.Utils.Drawing.Helpers; #line default #line hidden public class IdentityProvider : System.Web.WebPages.HelperPage { #line hidden #line 5 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" public static System.Web.WebPages.HelperResult ListGroupGrid(IEnumerable&lt;IdentityProviderData.ExternalIdentityProvider&gt; providers) { #line default #line hidden return new System.Web.WebPages.HelperResult(__razor_helper_writer =&gt; { #line 5 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" #line default #line hidden BeginContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 202, 11, true); WriteLiteralTo(__razor_helper_writer, " &lt;div&gt;\r\n"); EndContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 202, 11, true); #line 7 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" #line default #line hidden #line 7 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" foreach(var provider in providers){ #line default #line hidden BeginContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 272, 13, false); #line 8 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" WriteTo(__razor_helper_writer, provider.Name); #line default #line hidden EndContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 272, 13, false); #line 8 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" #line default #line hidden BeginContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 287, 16, true); WriteLiteralTo(__razor_helper_writer, " &lt;img"); EndContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 287, 16, true); WriteAttributeTo(__razor_helper_writer, "src", Tuple.Create(" src=\"", 303), Tuple.Create("\"", 326) #line 9 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" , Tuple.Create(Tuple.Create("", 309), Tuple.Create&lt;System.Object, System.Int32&gt;(provider.IconUrl #line default #line hidden , 309), false) ); BeginContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 327, 4, true); WriteLiteralTo(__razor_helper_writer, "/&gt;\r\n"); EndContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 327, 4, true); #line 10 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" } #line default #line hidden BeginContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 342, 12, true); WriteLiteralTo(__razor_helper_writer, " &lt;/div&gt;\r\n"); EndContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 342, 12, true); #line 12 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" #line default #line hidden }); #line 12 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" } #line default #line hidden public IdentityProvider() { } protected static ASP.global_asax ApplicationInstance { get { return ((ASP.global_asax)(Context.ApplicationInstance)); } } } } </code></pre> <p><strong>Update 5:</strong> I did some more testing. To illustrate the issue as clearly as possible, I eliminated all input parameters and extra markup.</p> <p>Consider the following three helpers:</p> <pre><code>@helper BaseCase() { &lt;img src="/Resources/Images/Placeholders/generic-user-image.jpg"/&gt; } @helper VariableBaseCase() { var src = "/Resources/Images/Placeholders/generic-user-image.jpg"; &lt;img src="/Resources/Images/Placeholders/generic-user-image.jpg"/&gt; &lt;text&gt;the source is @src&lt;/text&gt; } @helper Variable() { var src = "/Resources/Images/Placeholders/generic-user-image.jpg"; &lt;img src="@src"/&gt; &lt;text&gt;the source is @src&lt;/text&gt; } </code></pre> <p>When I call the first two helpers from an ASPX, they display output (and the images appear normally) without exception:</p> <pre><code>&lt;%= RazorHelpers.ImageTest.BaseCase().ToString() %&gt; &lt;%= RazorHelpers.ImageTest.VariableBaseCase().ToString() %&gt; </code></pre> <p>When I call the last helper, I get the same error I have been getting.</p> <pre><code>&lt;%= RazorHelpers.ImageTest.Variable().ToString() %&gt; </code></pre> <p>I hope now I am correct in believing that this is somehow a relative path issue. How do I get Razor to display images with dynamic <code>src</code> properties in my Web Forms application?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload