1

I'm showing a list of values in a view. It's working ok, but I need get only the first characters. I'm using Substring to implement it and it's working ok, in another case I use it for other value of my list but it's not working. I got this error

System.NullReferenceException: Object reference not set to an instance of an object.

Html code:

<tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.LeadName</td>
                    <td>@item.CarrierCode</td>
                    <td>@item.OrderRef.Substring(0, 5)...</td>
                    <td>@item.Carrier</td>
                    <td>@item.Ref</td>
                    <td>@item.TicketRef.Substring(0, 5)</td>
                    <td>@item.NoofNts</td>
                    <td>@item.DebtorCode</td>
                    <td>@item.InvDate.ToString("d")</td>
                </tr>
            }
        </tbody>

"OrderRef" is working ok and the other one(TicketRef) doesn't work with the Substring but it's working without it.

gon250
  • 3,405
  • 6
  • 44
  • 75
  • Looks like one of those values is null in that case – Michael B Apr 01 '14 at 11:25
  • Do you return model with your view? – Uriil Apr 01 '14 at 11:26
  • `the other one doesn't work with the Substring` means `Ref` is not working? – Sudhakar Tillapudi Apr 01 '14 at 11:26
  • 1
    The only way to figure this out it so debug your code and see what happens! One of your properties or instances is null. – L-Four Apr 01 '14 at 11:28
  • The model is OK because I print everything OK without substring, problem is in the second case when I try to print using substring. – gon250 Apr 01 '14 at 11:31
  • Probably, `OrderRef` is **null** just use an `if` statement and check whether it is or not... – Selman Genç Apr 01 '14 at 11:34
  • I supposed the problem could be a null value but I have added a break point to make sure it's not null and all the values are null if I put substring and aren't null if I don't use substring.. – gon250 Apr 01 '14 at 11:34
  • I have rebuilt my project and it's working.. I don't know why. Anyway thanks for your help. I'll implement to check before to print the data if it's null to make sure its working ok. – gon250 Apr 01 '14 at 11:59

3 Answers3

2

Sounds like TicketRef is null.

I like to use a "NullSafe" extension (from here) for this kind of thing:

<td>@item.TicketRef.NullSafe(s => s.Substring(0, 5))...</td>

public static TResult NullSafe<TObj, TResult>(
    this TObj obj, 
    Func<TObj, TResult> func, 
    TResult ifNullReturn = default(TResult))
{
    return obj != null ? func(obj) : ifNullReturn;
}
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
0

Up to your comment that the error comes only when you use Substring it would mean than OrderRef is null, try to catch the NullReferenceException by try catch around the code to see the exact error.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
0

Some of the property values are null in this case. So you need to check before you call substring. If adjusted your sample so that the OrderRef property is checked before it is written to the output. You can adjust the spot where the Substring doesn't work in your code accordingly.

<tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.LeadName</td>
            <td>@item.CarrierCode</td>
            <td>
                @if(item.OrderRef != null)
                {
                    @item.OrderRef.Substring(0, 5)
                    @Html.Raw("...");
                }
            </td>
            <td>@item.Carrier</td>
            <td>@item.Ref</td>
            <td>@item.TicketRef</td>
            <td>@item.NoofNts</td>
            <td>@item.DebtorCode</td>
            <td>@item.InvDate.ToString("d")</td>
        </tr>
    }
</tbody>

As a general approach, please read this excellent post on NullReferenceExceptions and how to fix them.

Community
  • 1
  • 1
Markus
  • 20,838
  • 4
  • 31
  • 55
  • This logic could also be done in the view model itself so that the view can focus on just displaying the values, making it simpler. – L-Four Apr 01 '14 at 11:41