1
@foreach (var a in Model.MESSAGES.OrderBy(p => p.DATE))
{
    var id = a.SENDERID == ViewBag.KullaniciId ? a.SENTID : a.SENDERID;
    var name = Model.USERS.FirstOrDefault(p => p.ID == id)?.NAMESURNAME;
    <tr id="tr@(a.ID)">
        <td>@name</td>
        <td>@a.MESSAGE.Substring(0, Math.Min(100, a.MESSAGE.Length))</td>
    </tr>
}

I get the following error :

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

CoreFramework.Entities.Concrete.MEESAGES.Message.get returned null.

2 Answers2

1

use null conditional operators

<td>@a.MESSAGE?.Substring(0, Math.Min(100, a.MESSAGE.Length))</td>
Mohammad Aghazadeh
  • 2,108
  • 3
  • 9
  • 20
0

try using null-conditional-operators

<td>@a.MESSAGE?.Substring(0, Math.Min(100, a.MESSAGE.Length))</td>
Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28