WebGrid Replace function in string null reference exception

I keep getting

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 54: string review = item.Review; Line 55: //review = review.Replace("\r\n", "<br/>"); Line 56: return review.Replace("\r\n", "<br/>"); Line 57: }), Line 58: grid.Column(format: @<a href="~/[email protected]">Delete</a>)

....................... Line: 56

When I try to use the Replace function for the string review. I create and set the instance of review on line 54 and it will output the text I am expecting. But when I try to do a replace I get this error. Why?

-------------Problems Reply------------

Line 54 does not create an instance of review, rather it just points to the item.Review value that may or may not be null.

Presumably in this case item.Review is itself null, and thus review is also null.

One thing you could do to work around this is change line 54 to:

string review = item.Review ?? "";

This will ensure that review is never null. It'll either have the contents of item.Review or it will be an empty string (which is not null).

Category:string Views:2 Time:2014-09-18

Related post

  • Create expression from string (null reference) 2012-02-01

    Here is the problem: We use table object to allow the users to perform some functionality like search, sort, pagination etc. Those tables works great. But there is a problem with one of the functionality : The sort (=OrderBy). In fact, to allow the s

  • Why couldn't I check for a null reference on a connection string? 2009-07-24

    Probably a C# noob question, so don't flame me. I was trying to do this: if (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"].ConnectionString != null) { // ... } But I kept getting a System.NullReferenceException. I thought since it

  • calling F# functions from C# and getting null reference exceptions 2009-10-27

    I'm using Visual Studio 2008 with the October 2009 F# CTP installed. I'm trying to call some F# code from my C# program. Most types of F# functions seem to work, but some are not getting initialized in F# and are throwing NullReferenceExceptions. The

  • Debug string replace function in C 2011-05-31

    I tried to code a function which replace all string s1 to s2, in a given string s. however, i don't know why my program stop at the line *p=0 in that replace function without any error reported? @@ #include <stdio.h> #include <stdlib.h> #

  • Javascript- Use function for string.replace replacement text 2011-11-23

    I'm attempting to replace url's in a string by calling a function that shortens the url's and then replaces the text. The shortening logic works perfectly, and I end up with the proper replacement url, however i cannot get the replace function to wor

  • String replace function 2008-11-18

    I have following string String str = "replace :) :) with some other string"; And I want to replace first occurance of :) with some other string And I used str.replaceFirst(":)","hi"); it gives following exception "Unmatched closing ')'" I tried using

  • app.configs and MSTest Project - null reference for a connection string 2009-05-30

    When I try to run Unit Tests (mstest) I run into this issue. The line of code: _mainCnStr = System.Configuration.ConfigurationManager. ConnectionStrings["main"].ConnectionString; Comes back as a null reference It doesn't do this in the main UI projec

  • passing a string by reference to a function would speed things up? (php) 2009-07-23

    Possible Duplicate: In PHP (>= 5.0), is passing by reference faster? I wonder if by declaring the parameter pass by reference, the PHP interpreter will be faster for not having to copy the string to the function's local scope? The script turns XML

  • SharePoint designer string replace function 2009-12-28

    SharePoint Designer does not support the replace function directly. What would be a function or call Template for this? I want to replace "-Y" with "Yes" and "-N" with "NO" in a string. E.g.: Sample-Y, Sample1-N Result as: Sample Yes Sample1 No -----

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.150 (s). 11 q(s)