locked
Object reference not set to an instance of object! RRS feed

  • Question

  • User895691971 posted

    Hi, the error pops up when I write this line of code

    <div class="photo-post"><img src="@Href("~/user_files/images", row.ImageName)" alt="Photo" /></div>

    I have tried span too. When I write this line, this error pops up for this line 

    <span class="message-post">@messagePost</span>

    But when the above line isn't present, this message line is OK! And I have also tried @row.Message So thats not the issue

    Monday, July 8, 2013 1:21 PM

Answers

  • User281315223 posted

    You could probably use the String.Format() method to shorten / clean things up a bit : 

    <div class="photo-post">
        <img src='@String.Format("~/user_files/images/{0}",row.ImageName)' alt='Photo' />
    </div>

    When working with the Razor View engine, you should always be mindful of ensuring that you only use '@' characters within the correct scope (as sometimes within loops they can be unnecessary) and consider alternating between the two major types of quotes (single and double).

    As a rule of thumb, when working directly with client-side code (HTML, Javascript and CSS) I typically prefer to use single-quotes and reserve double-quotes for server-side related code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 8, 2013 1:39 PM

All replies

  • User895691971 posted

    Sorry to say, but pretty shitty! O.o This code worked!

    <div class="photo-post"><img src="~/user_files/images/ " + @row.ImageName + " "alt="Photo" /></div>

    Pretty lengthy, isn't it?

    Monday, July 8, 2013 1:27 PM
  • User281315223 posted

    You could probably use the String.Format() method to shorten / clean things up a bit : 

    <div class="photo-post">
        <img src='@String.Format("~/user_files/images/{0}",row.ImageName)' alt='Photo' />
    </div>

    When working with the Razor View engine, you should always be mindful of ensuring that you only use '@' characters within the correct scope (as sometimes within loops they can be unnecessary) and consider alternating between the two major types of quotes (single and double).

    As a rule of thumb, when working directly with client-side code (HTML, Javascript and CSS) I typically prefer to use single-quotes and reserve double-quotes for server-side related code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 8, 2013 1:39 PM
  • User895691971 posted

    If I use this code 

    <div class="photo-post">
        <img src='@String.Format("~/user_files/images/{0}",row.ImageName)' alt='Photo' />
    </div>

    Then the error pops us, saying Value cannot be null. When there is a message attached with the post (Message is not null)

    Tuesday, July 9, 2013 1:43 AM
  • User895691971 posted

    Thanks for this code line, and your guideline! I appreciate that. The error was in Database, the col named: "ImageAttached" was null one was filled, but other was null, so the error was popping out!

    But I can't still understand, why for the line of Message. Where message was even filled in both rows!

    Any guideline on this one will be appreciated!

    Tuesday, July 9, 2013 1:55 AM