Произошла ошибка system nullreferenceexception

Причина

Вкратце

Вы пытаетесь воспользоваться чем-то, что равно null (или Nothing в VB.NET). Это означает, что либо вы присвоили это значение, либо вы ничего не присваивали.

Как и любое другое значение, null может передаваться от объекта к объекту, от метода к методу. Если нечто равно null в методе «А», вполне может быть, что метод «В» передал это значение в метод «А».

Остальная часть статьи описывает происходящее в деталях и перечисляет распространённые ошибки, которые могут привести к исключению NullReferenceException.

Более подробно

Если среда выполнения выбрасывает исключение NullReferenceException, то это всегда означает одно: вы пытаетесь воспользоваться ссылкой. И эта ссылка не инициализирована (или была инициализирована, но уже не инициализирована).

Это означает, что ссылка равна null, а вы не сможете вызвать методы через ссылку, равную null. В простейшем случае:

string foo = null;
foo.ToUpper();

Этот код выбросит исключение NullReferenceException на второй строке, потому что вы не можете вызвать метод ToUpper() у ссылки на string, равной null.

Отладка

Как определить источник ошибки? Кроме изучения, собственно, исключения, которое будет выброшено именно там, где оно произошло, вы можете воспользоваться общими рекомендациями по отладке в Visual Studio: поставьте точки останова в ключевых точках, изучите значения переменных, либо расположив курсор мыши над переменной, либо открыв панели для отладки: Watch, Locals, Autos.

Если вы хотите определить место, где значение ссылки устанавливается или не устанавливается, нажмите правой кнопкой на её имени и выберите «Find All References». Затем вы можете поставить точки останова на каждой найденной строке и запустить приложение в режиме отладки. Каждый раз, когда отладчик остановится на точке останова, вы можете удостовериться, что значение верное.

Следя за ходом выполнения программы, вы придёте к месту, где значение ссылки не должно быть null, и определите, почему не присвоено верное значение.

Примеры

Несколько общих примеров, в которых возникает исключение.

Цепочка

ref1.ref2.ref3.member

Если ref1, ref2 или ref3 равно null, вы получите NullReferenceException. Для решения проблемы и определения, что именно равно null, вы можете переписать выражение более простым способом:

var r1 = ref1;
var r2 = r1.ref2;
var r3 = r2.ref3;
r3.member

Например, в цепочке HttpContext.Current.User.Identity.Name, значение может отсутствовать и у HttpContext.Current, и у User, и у Identity.

Неявно

public class Person {
    public int Age { get; set; }
}
public class Book {
    public Person Author { get; set; }
}
public class Example {
    public void Foo() {
        Book b1 = new Book();
        int authorAge = b1.Author.Age; // Свойство Author не было инициализировано
                                       // нет Person, у которого можно вычислить Age.
    }
}

То же верно для вложенных инициализаторов:

Book b1 = new Book { Author = { Age = 45 } };

Несмотря на использование ключевого слова new, создаётся только экземпляр класса Book, но экземпляр Person не создаётся, поэтому свойство Author остаётся null.

Массив

int[] numbers = null;
int n = numbers[0]; // numbers = null. Нет массива, чтобы получить элемент по индексу

Элементы массива

Person[] people = new Person[5];
people[0].Age = 20; // people[0] = null. Массив создаётся, но не
                    // инициализируется. Нет Person, у которого можно задать Age.

Массив массивов

long[][] array = new long[1][];
array[0][0] = 3; // = null, потому что инициализировано только первое измерение.
                 // Сначала выполните array[0] = new long[2].

Collection/List/Dictionary

Dictionary<string, int> agesForNames = null;
int age = agesForNames["Bob"]; // agesForNames = null.
                               // Экземпляр словаря не создан.

LINQ

public class Person {
    public string Name { get; set; }
}
var people = new List<Person>();
people.Add(null);
var names = from p in people select p.Name;
string firstName = names.First(); // Исключение бросается здесь, хотя создаётся
                                  // строкой выше. p = null, потому что
                                  // первый добавленный элемент = null.

События

public class Demo
{
    public event EventHandler StateChanged;

    protected virtual void OnStateChanged(EventArgs e)
    {        
        StateChanged(this, e); // Здесь бросится исключение, если на
                               // событие StateChanged никто не подписался
    }
}

Неудачное именование переменных

Если бы в коде ниже у локальных переменных и полей были разные имена, вы бы обнаружили, что поле не было инициализировано:

public class Form1 {
    private Customer customer;

    private void Form1_Load(object sender, EventArgs e) {
        Customer customer = new Customer();
        customer.Name = "John";
    }

    private void Button_Click(object sender, EventArgs e) {
        MessageBox.Show(customer.Name);
    }
}

Можно избежать проблемы, если использовать префикс для полей:

private Customer _customer;

Цикл жизни страницы ASP.NET

public partial class Issues_Edit : System.Web.UI.Page
{
    protected TestIssue myIssue;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Выполняется только на первой загрузке, но не когда нажата кнопка
            myIssue = new TestIssue(); 
        }
    }
    
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        myIssue.Entry = "NullReferenceException здесь!";
    }
}

Сессии ASP.NET

// Если сессионная переменная "FirstName" ещё не была задана,
// то эта строка бросит NullReferenceException.
string firstName = Session["FirstName"].ToString();

Пустые вью-модели ASP.NET MVC

Если вы возвращаете пустую модель (или свойство модели) в контроллере, то вью бросит исключение при попытке доступа к ней:

// Controller
public class Restaurant:Controller
{
    public ActionResult Search()
    {
         return View();  // Модель не задана.
    }
}

// Razor view 
@foreach (var restaurantSearch in Model.RestaurantSearch)  // Исключение.
{
}

Способы избежать

Явно проверять на null, пропускать код

Если вы ожидаете, что ссылка в некоторых случаях будет равна null, вы можете явно проверить на это значение перед доступом к членам экземпляра:

void PrintName(Person p) {
    if (p != null) {
        Console.WriteLine(p.Name);
    }
}

Явно проверять на null, использовать значение по умолчанию

Методы могут возвращать null, например, если не найден требуемый экземпляр. В этом случае вы можете вернуть значение по умолчанию:

string GetCategory(Book b) {
    if (b == null)
        return "Unknown";
    return b.Category;
}

Явно проверять на null, выбрасывать своё исключение

Вы также можете бросать своё исключение, чтобы позже его поймать:

string GetCategory(string bookTitle) {
    var book = library.FindBook(bookTitle);  // Может вернуть null
    if (book == null)
        throw new BookNotFoundException(bookTitle);  // Ваше исключение
    return book.Category;
}

Использовать Debug.Assert для проверки на null для обнаружения ошибки до бросания исключения

Если во время разработки вы знаете, что метод может, но вообще-то не должен возвращать null, вы можете воспользоваться Debug.Assert для быстрого обнаружения ошибки:

string GetTitle(int knownBookID) {
    // Вы знаете, что метод не должен возвращать null
    var book = library.GetBook(knownBookID);  

    // Исключение будет выброшено сейчас, а не в конце метода.
    Debug.Assert(book != null, "Library didn't return a book for known book ID.");

    // Остальной код...

    return book.Title; // Не выбросит NullReferenceException в режиме отладки.
}

Однако эта проверка не будет работать в релизной сборке, и вы снова получите NullReferenceException, если book == null.

Использовать GetValueOrDefault() для Nullable типов

DateTime? appointment = null;
Console.WriteLine(appointment.GetValueOrDefault(DateTime.Now));
// Отобразит значение по умолчанию, потому что appointment = null.

appointment = new DateTime(2022, 10, 20);
Console.WriteLine(appointment.GetValueOrDefault(DateTime.Now));
// Отобразит дату, а не значение по умолчанию.

Использовать оператор ?? (C#) или If() (VB)

Краткая запись для задания значения по умолчанию:

IService CreateService(ILogger log, Int32? frobPowerLevel)
{
    var serviceImpl = new MyService(log ?? NullLog.Instance);
    serviceImpl.FrobPowerLevel = frobPowerLevel ?? 5;
}

Использовать операторы ?. и ?[ (C# 6+, VB.NET 14+):

Это оператор безопасного доступа к членам, также известный как оператор Элвиса за специфическую форму. Если выражение слева от оператора равно null, то правая часть игнорируется, и результатом считается null. Например:

var title = person.Title.ToUpper();

Если свойство Title равно null, то будет брошено исключение, потому что это попытка вызвать метод ToUpper на значении, равном null. В C# 5 и ниже можно добавить проверку:

var title = person.Title == null ? null : person.Title.ToUpper();

Теперь вместо бросания исключения переменной title будет присвоено null. В C# 6 был добавлен более короткий синтаксис:

var title = person.Title?.ToUpper();

Разумеется, если переменная person может быть равна null, то надо проверять и её. Также можно использовать операторы ?. и ?? вместе, чтобы предоставить значение по умолчанию:

// обычная проверка на null
int titleLength = 0;
if (title != null)
    titleLength = title.Length;

// совмещаем операторы `?.` и `??`
int titleLength = title?.Length ?? 0;

Если любой член в цепочке может быть null, то можно полностью обезопасить себя (хотя, конечно, архитектуру стоит поставить под сомнение):

int firstCustomerOrderCount = customers?[0]?.Orders?.Count() ?? 0;

Sergey Vasiliev

  • Из-за чего возникает исключение NullReferenceException?
    • Теория
    • Как в переменную может попасть null-значение
    • Операции с null-значением, приводящие к исключению
  • Как исправить исключение NullReferenceException
  • Как предотвратить исключения NullReferenceException
    • Используйте nullable-контекст
    • Используйте статический анализ

NullReferenceException (NRE) — тип исключения платформы .NET, возникающий при попытке обращения по нулевой ссылке. В заметке рассмотрим причины, из-за которых возникают исключения этого типа, а также способы их предотвращения и исправления.

1049_NullReferenceException_ru/image1.png

Примечание. Эта заметка рассчитана на начинающих программистов. Разработчикам с опытом предлагаю 2 активности:

  • посмотреть перечисленные здесь способы столкнуться с NullReferenceException и проверить, все ли из них вы знаете;
  • сыграть в игру на поиск ошибок.

Из-за чего возникает исключение NullReferenceException?

Теория

Переменные ссылочных типов в C# хранят ссылки на объекты. Чтобы обозначить, что ссылка не указывает на объект, используют значение null. Стоит также отметить, что null — значение выражений ссылочных типов по умолчанию.

Исключение типа NullReferenceException возникает при попытке обращения по нулевой ссылке. Операции, при которых может возникнуть исключение, мы перечислим ниже.

Рассмотрим пример:

Object notNullRef = new Object();
Object nullRef = default;

int hash;
hash = notNullRef.GetHashCode();
hash = nullRef.GetHashCode(); // NullReferenceException (NRE)

В коде объявляются две переменные ссылочного типа ObjectnotNullRef и nullRef:

  • notNullRef хранит ссылку на объект, созданный в результате вызова конструктора типа Object;
  • nullRef содержит default-значение типа Objectnull.

1049_NullReferenceException_ru/image2.png

Вызов метода GetHashCode через ссылку в notNullRef отработает нормально, так как ссылка указывает на объект. При попытке вызова того же метода для nullRef средой CLR будет выброшено исключение типа NullReferenceException.

Ниже мы рассмотрим, откуда могут прийти null-значения и какие операции могут привести к исключению NullReferenceException.

Как в переменную может попасть null-значение

Рассмотрим примеры того, как в переменную может попасть значение null.

1. Явная запись значения null или default.

String name = null;
var len = name.Length; // NRE

Результатом выражения default и default(T) для ссылочных типов также будет null.

Object obj = default; // or default(Object)
var hash = obj.GetHashCode(); // NRE

2. Инициализация поля ссылочного типа по умолчанию.

class A 
{
  private String _name;
  public void Foo()
  {
    var len = _name.Length; // NRE
  }
}

var obj = new A();
obj.Foo();

В этом примере поле _name инициализируется значением по умолчанию. На момент вызова Foo поле _name равно null, поэтому при обращении к свойству Length будет выброшено исключение.

3. Результат работы null-conditional оператора (?.).

String name = user?.Name;
var len = name.Length; // Potential NRE

Если значение user или user.Name будет равно null, в переменную name также будет записано значение null. В таком случае при обращении к свойству Length без проверки на null возникнет исключение.

4. Результат приведения с использованием оператора as.

Object obj = new Object();
String name = obj as String; // unsuccessful cast, name is null
var len = name.Length; // NRE

Результатом преобразования с помощью оператора as будет значение null, если преобразование выполнить не удалось.

В примере выше переменная obj хранит ссылку на экземпляр типа Object. Попытка приведения obj к типу String закончится неудачей, в результате чего в name будет записано значение null.

5. Результат работы *OrDefault метода.

Методы вида *OrDefault (FirstOrDefault, LastOrDefault и т. п.) из стандартной библиотеки возвращают значение по умолчанию, если значение предиката не подходит ни для одного элемента или коллекция пустая.

String[] strArr = ....;
String firstStr = strArr.FirstOrDefault();
var len = firstStr.Length; // Potential NRE

Если в массиве strArr нет элементов, метод FirstOrDefault вернёт значение default(String)null. При разыменовании нулевой ссылки возникнет исключение.

6. Упаковка default значения типа Nullable<T>.

Результатом упаковки экземпляров Nullable<T> с default-значением будет null.

long? nullableLong1 = default;
long? nullableLong2 = null;

Nullable<long> nullableLong3 = default;
Nullable<long> nullableLong4 = null;
Nullable<long> nullableLong5 = new Nullable<long>();


var nullableToBox = ....; // nullableLong1 — nullableLong5

object boxedValue = (Object)nullableToBox; // null
_ = boxedValue.GetHashCode(); // NRE

При записи в переменную nullableToBox любого из значений nullableLong1nullableLong5 и последующей упаковки результатом будет null. При использовании такого значения без проверки на null будет выброшено исключение.

Подробности упаковки значений типа Nullable<T> описаны в статье «Хорошо ли вы помните nullable value types?».

Операции с null-значением, приводящие к исключению

В этом разделе перечислены операции, выполнение которых с null-значением приведёт к исключению NullReferenceException.

1. Явное обращение к члену объекта.

class A
{
  public String _name;
  public String Name => _name;
  public String GetName() { return _name; }
}

A aObj = null;
_ = aObj._name; // NRE
_ = aObj.Name; // NRE
_ = aObj.GetName(); // NRE

То же самое — при разыменовании внутри метода:

void Foo(A obj)
{
  _ = obj.Name; 
}

A aObj = null;
Foo(aObj); // NRE inside method

2. Обращение по индексу.

int[] arr = null;
int val = arr[0]; // NRE

3. Вызов делегата.

Action fooAct = null;
fooAct(); // NRE

4. Итерирование в foreach.

List<long> list = null;
foreach (var item in list) // NRE
{ .... }

Обратите внимание, что оператор ‘?.’ здесь не поможет:

foreach (var item in wrapper?.List) // Potential NRE
{ .... }

Если wrapper или wrapper.List равны null, всё так же будет выброшено исключение. Подробнее ситуацию разобрали в статье «Использование оператора ?. в foreach: защита от NullReferenceException, которая не работает».

5. Использование null-значения в качестве операнда для await.

Task GetPotentialNull()
{
  return _condition ? .... : null;
}
await GetPotentialNull(); // Potential NRE

6. Распаковка null-значения.

object obj = null;
int intVal = (int)obj; // NRE

7. Выброс исключения с null-значением.

InvalidOperationException invalidOpException 
  = flag ? new InvalidOperationException() 
         : null;

throw invalidOpException; // Potential NRE

В переменную invalidOpException может быть записано значение null. В этом случае оператор throw выбросит исключение типа NullReferenceException.

8. Разыменование значения свойства Target у экземпляра типа WeakReference.

void ProcessIfNecessary(WeakReference weakRef)
{
  if (weakRef.IsAlive)
    (weakRef.Target as DataProcessor).Process(); // Potential NRE
}

Ссылка в WeakReference указывает на объект, при этом не защищая его от сборки мусора. Если объект попадёт под сборку мусора после проверки weakRef.IsAlive, но до вызова метода Process, то:

  • значением weakRef.Target будет null;
  • результатом оператора as также будет null;
  • при попытке вызова метода Process будет выброшено исключение NullReferenceException.

9. Использование значения поля ссылочного типа до явной инициализации.

class A
{
  private String _name;
  public A()
  {
    var len = _name.Length; // NRE
  }
}

На момент обращения к свойству Length поле _name проинициализировано значением по умолчанию (null). Результат обращения — исключение.

10. Небезопасный вызов обработчиков события в многопоточном коде.

public event EventHandler MyEvent;

void OnMyEvent(EventArgs e)
{
  if (MyEvent != null)
    MyEvent(this, e); // Potential NRE
}

Если между проверкой MyEvent != null и вызовом обработчиков события MyEvent у него не останется подписчиков, при вызове будет выброшено исключение типа NullRefernceException.

Как исправить исключение NullReferenceException

Чтобы избежать исключений типа NullReferenceException, исключите ситуацию разыменования нулевых ссылок. Для этого:

  • определите, откуда в выражение попадает нулевая ссылка;
  • измените логику работы приложения, чтобы доступа по нулевой ссылке не происходило.

Рассмотрим пример:

foreach (var item in potentialNullCollection?.Where(....))
{ .... }

Если значением potentialNullCollection будет null, оператор ‘?.’ также вернёт значение null. При попытке обхода коллекции в цикле foreach возникнет исключение.

Если potentialNullCollection в данном фрагменте кода никогда не равен null, стоит убрать оператор ‘?.’, чтобы не запутать разработчиков и инструменты анализа кода:

foreach (var item in potentialNullCollection.Where(....))
{ .... }

Если potentialNullCollection может принимать значение null, стоит добавить явную проверку или использовать оператор ‘??’.

// 1
if (potentialNullCollection != null)
{
  foreach (var item in potentialNullCollection.Where(....))
  { .... }
}

// 2
foreach (var item in    potentialNullCollection?.Where(....) 
                     ?? Enumerable.Empty<T>)
{ .... }

Примечание. Добавить проверку на неравенство null — самый простой способ защититься от NullReferenceException. Однако иногда такая правка будет не решать исходную проблему, а только маскировать её. Поэтому при исправлении кода полезно думать о том, достаточно ли будет добавить проверку или нужно исправить в коде что-то ещё.

Как предотвратить исключения NullReferenceException

Кроме достаточно очевидного совета «не разыменовывать нулевые ссылки» есть несколько практик, которые помогут избежать возникновения исключений NRE.

Используйте nullable-контекст

Без использования nullable-контекста значение null считается допустимым для ссылочных типов:

String str = null; // No warnings

Начиная с C# 8, в языке появилась возможность использовать nullable-контекст. Он вводит понятие nullable reference types. В nullable-контексте ссылочные типы считаются не допускающими значения null. Например, при использовании nullable-контекста на код, который мы только что рассмотрели, компилятор выдаст предупреждение:

String str = null; // CS8600

Предупреждение: CS8600 Converting null literal or possible null value to non-nullable type.

Аналогичная ситуация при вызове методов:

void ProcessUserName(String userName)
{
  var len = userName.Length;
  ....
}
....
ProcessUserName(null); // CS8625

Предупреждение компилятора: CS8625 Cannot convert null literal to non-nullable reference type.

Чтобы указать компилятору, что переменная ссылочного типа может принимать значение null, используется символ ‘?’:

String firstName = null; // CS8600
String? lastName = null; // No warning

При попытке разыменовать nullable-переменную без проверки на null компилятор также выдаст предупреждение:

void ProcessUserName(String? userName)
{
  var len = userName.Length; // CS8602
}

Предупреждение компилятора: CS8602 — Dereference of a possibly null reference.

Если нужно указать компилятору, что в конкретном месте кода выражение точно не имеет значения null, можно использовать null-forgiving оператор — ‘!’. Пример:

void ProcessUserName(String? userName)
{
  int len = default;
  if (_flag)
    len = userName.Length; // CS8602
  else
    len = userName!.Length; // No warnings
}

Таким образом, nullable-контекст помогает писать код так, чтобы минимизировать возможность разыменования нулевых ссылок.

Включить nullable-контекст можно несколькими способами:

  • изменить соответствующую опцию в настройках проекта («Nullable» в Visual Studio или «Nullable reference types» в JetBrains Rider);
  • самостоятельно прописать настройку в проектном файле (.csproj): <Nullable>enable</Nullable>;
  • с помощью директив #nullable enable / #nullable disable в коде.

У nullable-контекста куда больше возможностей для настройки. Подробнее о них мы писали в отдельной статье.

Примечание. Обратите внимание, что nullable-context влияет на выдачу предупреждений компилятором, но не на логику исполнения приложения.

String? str = null;
var len = str!.Length;

Компилятор не выдаст предупреждения на этот код, так как в нём используется null-forgiving оператор. Однако на этапе исполнения в коде возникнет исключение типа NullReferenceException.

Используйте статический анализ

Статические анализаторы помогают находить дефекты безопасности и ошибки в коде. В том числе анализаторы помогают находить места возникновения исключений типа NullReferenceException.

Пример такого статического анализатора — PVS-Studio.

Рассмотрим пример C# кода, в котором может возникнуть NullReferenceException.

private ImmutableArray<char>
GetExcludedCommitCharacters(ImmutableArray<CompletionItem> items)
{
  var hashSet = new HashSet<char>();
  foreach (var item in items)
  {
    foreach (var rule in item.Rules?.FilterCharacterRules)
    {
      if (rule.Kind == CharacterSetModificationKind.Add)
      {
        foreach (var c in rule.Characters)
        {
          hashSet.Add(c);
        }
      }
    }
  }

  return hashSet.ToImmutableArray();
}

Во втором цикле foreach разработчики выполняют обход коллекции FilterCharacterRules, для получения которой используют выражение roslynItem.Rules?.FilterCharacterRules. Оператор ‘?.’ предполагает, что свойство Rules может иметь значение null. Однако если результатом выражения будет null, при попытке перебора null-значения в foreach всё равно возникнет NullReferenceException.

PVS-Studio находит эту проблему и выдаёт предупреждение V3153.

1049_NullReferenceException_ru/image3.png

Если items.Rules действительно может иметь значение null, защититься от NullReferenceException можно дополнительной проверкой:

foreach (var item in items)
{
  if (item.Rules == null)
    continue;

  foreach (var rule in item.Rules.FilterCharacterRules)
  {
    ....
  }
}

Анализатор не будет выдавать предупреждение на такой код.

PVS-Studio ищет и другие ситуации в коде, при которых может возникнуть исключение NullReferenceException:

  • V3080. Possible null dereference.
  • V3083. Unsafe invocation of event, NullReferenceException is possible.
  • V3095. The object was used before it was verified against null.
  • и т. д.

Как установить и запустить PVS-Studio?
open icon

Присылаем лучшие статьи раз в месяц

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

TOC

Time for another post in the series Debugging common .NET exceptions. Today’s exception is, without a doubt, the error most people have experienced: System.NullReferenceException. The exception happens when you try to invoke a reference that you were expecting to point to an object but in fact, points to null. Let’s get started!

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

Handling the error

There are some clever ways to avoid a NullReferenceException, but before we start looking into those, let us see how the exception can be caught. Being a plain old C# exception, NullReferenceException can be caught using a try/catch:

try
{
    string s = null;
    s.ToString();
}
catch (NullReferenceException e)
{
    // Do something with e, please.
}

Running the code above will produce the following error:

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

Debugging the error

We already know why the exception is happening. Something is null. When looking at the code above, it’s clear that s is null and the stack trace even tells us that:

stacktrace_1571989670

Sometimes spotting what is null can be hard. Take a look at the following example:

var street = service.GetUser().Address.Street;

If the code above throws a NullReferenceException, what is null? service? The result of GetUser()? Address? At first glance, Visual Studio isn’t exactly helpful either:

NullReferenceException in Visual Studio

There is a range of different ways to find out what is going on. Let’s look at the most commonly used ones.

Splitting chained method-calls to multiple lines

Spotting which call that caused an error is a lot easier if the calls are split into multiple lines:

var service = new Service();
var user = service.GetUser();
var address = user.Address;
var street = address.Street;

Running the code reveals the actual call causing the exception:

NullReferenceException in Visual Studio 2

In the example above user.Address returns null, why address.Street causes the NullReferenceException.

While splitting code into atoms like this can help debug what is going wrong, it’s not preferable in terms of readability (IMO).

Using Null Reference Analysis in Visual Studio

If you are on Visual Studio 2017 or newer (if not, now is the time to upgrade), you will have the Null Reference Analysis feature available. With this in place, Visual Studio can show you exactly what is null. Let’s change the example back to method-chaining:

var street = service.GetUser().Address.Street;

To enable the analysis go to Debug | Windows | Exception Settings. Check Common Language Runtime Exceptions (if not already checked) or extend the node and check the exceptions you are interested in. In this case, you can check System.NullReferenceException. When running the code, the debugger breaks on the NullReferenceException and you now see the Exception Thrown window:

Exception Thrown Window

Voila! The window says «ConsoleApp18.User.Address.get returned null». Exactly what we wanted to see. This will require you to run the code locally, though. If you are experiencing the exception on your production website, the Null Reference Analysis will not be available, since this is a feature belonging to Visual Studio (unfortunately). With that said, you can attach a debugger to a remote site running on Azure as explained here: Introduction to Remote Debugging on Azure Web Sites.

Fixing the error

There are various ways to fix NullReferenceException. We’ll start with the simple (but dirty) approach.

Using null checks

If null is an allowed value of an object, you will need to check for it. The most simple solution is to include a bunch of if-statements.

if (service != null)
{
    var user = service.GetUser();
    if (user != null)
    {
        var address = user.Address;
        if (address != null)
        {
            var street = address.Street;
        }
    }
}

The previous code will only reach address.Street if everything else is not null. We can probably agree that the code isn’t exactly pretty. Having multiple nested steps is harder to read. We can reverse the if-statements:

if (service == null) return;
var user = service.GetUser();
if (user == null) return;
var address = user.Address;
if (address == null) return;
var street = address.Street;

Simpler, but still a lot of code to get a street name.

Using null-conditional operator

C# 6 introduced a piece of syntactic sugar to check for null: null-conditional operator. Let’s change the method-chain example from before to use the «new» operator:

var user = service?.GetUser()?.Address?.Street;

The ? to the right of each variable, corresponds the nested if-statements from previously. But with much less code.

Use Debug.Assert during development

When getting a NullReferenceException it can be hard to spot the intent with the code from the original developer. Rather than including if-statements, it can be clearer for future authors of your code to use the Debug.Assert-method. Much like in a xUnit or NUnit test, you use Assert to verify the desired state on your objects. In the example from above, the service object could have come through a parameter or a constructor injected dependency:

class MyClass
{
    Service service;
    
    public MyClass(Service service)
    {
        this.service = service;
    }
    
    public string UserStreet()
    {
        return service.GetUser().Address.Street;
    }
}

To make a statement in your code that service should never be allowed to have the value of null, extend the constructor:

public MyClass(Service service)
{
    Debug.Assert(service != null);
    this.service = service;
}

In the case MyClass is constructed with null, the following error is shown when running locally:

Assert error

Use nullable reference types in C# 8.0

When designing code you often end up expecting parameters to be not null but end up checking for null to avoid a NullReferenceException. As you already know, all reference types in C# can take the value of null. Value types like int and boolean cannot take a value of null unless explicitely specified using the nullable value type (int? or Nullable<int>). Maybe it should have been the other way around with reference types all along?

C# 8 can fix this with nullable reference types (maybe NOT nullable reference types is a better name). Since this is a breaking change, it is launched as an opt-in feature. Nullable reference types are a great way to avoid NullReferenceExceptions, since you are very explicit about where you expect null and where not.

To enable not nullable reference types, create a new .NET Core 3 project and add the following to the csproj file:

<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>

You should be on C# 8 already, but to make it explicit, I’ve added the LangVersion element. The Nullable element enables nullable reference types. Out of the box, C# 8 creates a warning if it identifies the use of null where a value is expected. Let’s see how that looks:

class Program
{
    static void Main()
    {
        new Program().SayHello(null);
    }

    public void SayHello(string msg)
    {
        Console.WriteLine(msg);
    }
}

When compiling we see the following warning:

Program.cs(9,36): warning CS8625: Cannot convert null literal to non-nullable reference type. [C:projectscore3core3.csproj]

I know you are not one of them, but some people developed a warning-resistance which means that warnings are simply ignored. To overcome this, make sure that errors like this causes build errors over warnings by adding the following to csproj:

<WarningsAsErrors>CS8602,CS8603,CS8618,CS8625</WarningsAsErrors>

This will tell the C# compiler to treat these four nullable reference type warnings as errors instead.

Just to make it clear, allowing null in the msg parameter, you use the ? characters as with value types:

public void SayHello(string? msg)
{
    ...
}

Logging and monitoring

Logging and monitoring for null reference exceptions are a must. While some developers tempt to create control flow from exceptions (no-one should), null reference exceptions should never happen. This means that a System.NullReferenceException is a type of exception that should always be logged and fixed. A null check through either an if statement or the null-conditional operator is always the preferred way of handling potential null values. But make sure to implement a logging strategy that logs all uncaught exceptions, including the System.NullReferenceException.

When logging a System.NullReferenceException in a log file, database, elmah.io, or similar, it can be hard to spot what is null. You typically only see the method-name that causes the NullReferenceException and the Null Reference Analysis feature is only available while debugging inside Visual Studio. I will recommend you to always Include filename and line number in stack traces. This will pinpoint the exact line where the error happens.

elmah.io: Error logging and Uptime Monitoring for your web apps

This blog post is brought to you by elmah.io. elmah.io is error logging, uptime monitoring, deployment tracking, and service heartbeats for your .NET and JavaScript applications. Stop relying on your users to notify you when something is wrong or dig through hundreds of megabytes of log files spread across servers. With elmah.io, we store all of your log messages, notify you through popular channels like email, Slack, and Microsoft Teams, and help you fix errors fast.

elmah.io app banner

See how we can help you monitor your website for crashes
Monitor your website

EDIT: This issue is resolved, good thanks to Reniuz for his 5 hours of work and research of this issue, thank’s everyone.

NullReferenceException: Object reference not set to an instance of an object. on the following code and I’ve searched and searched and pulled my hair out for over 7-8 hours now trying to fix it.

private void buttonAddEffect_Click_1(object sender, EventArgs e)
{
    EffectSelectorForm effectSelectorForm = new EffectSelectorForm(Effects);
    if (effectSelectorForm.ShowDialog(this) == DialogResult.OK)
    {
        // create a new instance of the selected effect as we may want multiple copies of one effect
        Effect effect = (Effect)Activator.CreateInstance(effectSelectorForm.SelectedEffect.GetType());
        audioGraph.AddEffect(effect);
        int index = checkedListBox1.Items.Add(effect, true);
        checkedListBox1.SelectedIndex = index;
    }
    //MessageBox.Show(String.Format("I have {0} effects", Effects.Count));
}

Error is on the line:
EffectSelectorForm effectSelectorForm = new EffectSelectorForm(Effects);

the class:

namespace WindowsFormsApplication13
{
    public partial class EffectSelectorForm : Form
    {
        public EffectSelectorForm(ICollection<Effect> effects)
        {
            InitializeComponent();
            listBoxEffects.DisplayMember = "Name";
            listBoxEffects.DataSource = effects;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        public Effect SelectedEffect
        {
            get
            {
                return (Effect)listBoxEffects.SelectedItem;
            }
        }

        private void listBoxEffects_DoubleClick(object sender, EventArgs e)
        {
            buttonOK_Click(sender, e);
        }

What this is supposed to do is when the effectSelectorForm loads it should make a list of all the voice changer options but it does not do that…. it loads nothing on it, I got this code from another form for changing voice in Skype and re-wrote about 400 lines of code for it to work in my app but now I have this issue and I’m not giving up with all the effort I’ve put in so far. If it loaded on the other project why not in this one? I’ve went over code for hours over and over thinking im missing something but no.

Any help would be fantastic.

Stack

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at WindowsFormsApplication13.pwn4g3.buttonAddEffect_Click_1(Object sender, EventArgs e) in F:UsersTomDesktopNew folderNew folder (2)TestApppwn4g3PWN4G3MainForm2.cs:line 1758
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Effect.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JSNet
{    

    public abstract class Effect
    {
        private List<Slider> sliders;
        public float SampleRate { get; set; }
        public float Tempo { get; set; }
        public bool Enabled { get; set; }

        public Effect()
        {
            sliders = new List<Slider>();
            Enabled = true;
            Tempo = 120;
            SampleRate = 44100;
        }

        public IList<Slider> Sliders { get { return sliders; } }

        public Slider AddSlider(float defaultValue, float minimum, float maximum, float increment, string description)
        {
            Slider slider = new Slider(defaultValue, minimum, maximum, increment, description);
            sliders.Add(slider);
            return slider;
        }

        public abstract string Name { get; }

        // helper base methods
        // these are primarily to enable derived classes to use a similar
        // syntax to JS effects
        protected float slider1 { get { return sliders[0].Value; } }
        protected float slider2 { get { return sliders[1].Value; } }
        protected float slider3 { get { return sliders[2].Value; } }
        protected float slider4 { get { return sliders[3].Value; } }
        protected float slider5 { get { return sliders[4].Value; } }
        protected float slider6 { get { return sliders[5].Value; } }
        protected float slider7 { get { return sliders[6].Value; } }
        protected float slider8 { get { return sliders[7].Value; } }
        protected float min(float a, float b) { return Math.Min(a, b); }
        protected float max(float a, float b) { return Math.Max(a, b); }
        protected float abs(float a) { return Math.Abs(a); }
        protected float exp(float a) { return (float)Math.Exp(a); }
        protected float sqrt(float a) { return (float)Math.Sqrt(a); }
        protected float sin(float a) { return (float)Math.Sin(a); }
        protected float tan(float a) { return (float)Math.Tan(a); }
        protected float cos(float a) { return (float)Math.Cos(a); }
        protected float pow(float a, float b) { return (float)Math.Pow(a, b); }
        protected float sign(float a) { return Math.Sign(a); }
        protected float log(float a) { return (float)Math.Log(a); }
        protected float PI { get { return (float)Math.PI; } }

        protected void convolve_c(float[] buffer1, int offset1, float[] buffer2, int offset2, int count)
        {
            for (int i = 0; i < count * 2; i += 2)
            {
                float r = buffer1[offset1 + i];
                float im = buffer1[offset1 + i + 1];
                float cr = buffer2[offset2 + i];
                float ci = buffer2[offset2 + i + 1];
                buffer1[offset1 + i] = r * cr - im * ci;
                buffer1[offset1 + i + 1] = r * ci + im * cr;
            }
        }


        public virtual void Init()
        {
        }


        public abstract void Slider();


        public virtual void Block(int samplesblock)
        { 
        }


        public abstract void Sample(ref float spl0, ref float spl1);

        public override string ToString()
        {
            return Name;
        }
    }
}

In this article,we will learn: what is NullReferenceException and How to avoid it?

Null Reference Exception

  NullReferenceException or ‘Object Reference not set to an Instance of an Object’ is a very common exception. That indicates, you are trying to access member fields, or function types, on an object reference that points to null. This means the reference is null, and you cannot access members through a null reference.

Example:

using System;

class Program
{
    static void Main()
    {
     string value = null;
       if (value.Length == 0) // <-- Causes exception
        {
     Console.WriteLine(value); // <-- Never reached
  }
    }
}

 

Output:

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at Program.Main() in C:Users…
 

Common Scenarios:

1. Array:

int[] numbers = null;
int n = numbers[0]; // numbers is null. There is no array to index.

2. Array Elements:

Employee[] emp= new Employee[5];
emp[0].Age = 28 // emp[0] is null. The array was allocated but not
                   // initialized. There is no Employee to set the Age for.

 

3. Jagged Arrays:

long[][] array = new long[1][];
array[0][0] = 3; // is null because only the first dimension is yet initialized.
                 // Use array[0] = new long[2]; first.

 

4. Collection/List/Dictionary:

Dictionary<string, int> dicnames= null;
int age = dicnames["Tina"]; // dicnames is null.
                               // There is no Dictionary to perform the lookup.

 

5. Range Variable (Indirect/Deferred):

public class Person {
    public string Name { get; set; }
}
var people = new List<Person>();
people.Add(null);
var names = from p in people select p.Name;
string firstName = names.First(); // Exception is thrown here, but actually occurs
                                  // on the line above.  "p" is null because the
                                  // first element we added to the list is null.

 

6. Events:

public class Demo
{
    public event EventHandler StateChanged;

    protected virtual void OnStateChanged(EventArgs e)
    {        
        StateChanged(this, e); // Exception is thrown here 
                               // if no event handlers have been attached
                               // to StateChanged event
    }
}

 

7. Incorrect use of as operator:

class Book {
    public string Name { get; set; }
}
class Car { }
Car mycar = new Car();
Book mybook = mycar as Book;   // Incompatible conversion --> mybook = null
Console.WriteLine(mybook.Name);   // NullReferenceException

 

Different ways to avoid NullReferenceException?

1. Explicitly check for Null and ignore Null Values:

If you expect the reference sometimes to be null, you can check for it being null before accessing instance members.

void PrintAge(Employee emp) {
    if (emp != null) {
        Console.WriteLine(emp.Age);
    }
}

 

2. Explicitly check for Null and provide a default Value:

If you expect the reference sometimes to be null, you can check for it being null before accessing instance members and return a default value.

string GetName(Employee emp) {
    if (emp == null)
        return "Unknown";
    return emp.Name;
}

 

3. Explicitly check for Null and throw a Custom Exception:

string GetName(Employee emp) {
    if (emp == null)
        throw new EmployeeNotFoundException(emp);  
        return emp.Name;
}

 

4. Use GetValueOrDefault() for nullable value types to provide a default value when they are null:

DateTime? appointment = null;
Console.WriteLine(appointment.GetValueOrDefault(DateTime.Now));
// Will display the default value provided (DateTime.Now), because appointment is null.

appointment = new DateTime(2022, 10, 20);
Console.WriteLine(appointment.GetValueOrDefault(DateTime.Now));
// Will display the appointment date, not the default

 

5. Use the null coalescing operator: ??

The null coalescing operator “??” uses two question marks. With it you can use a custom value for a null reference variable.

class Program
{
    static string _name;
    static string Name
    {
  get
  {
      return _name ?? "Default";
  }
  set
  {
      _name = value;
  }
    }

    static void Main()
    {
  Console.WriteLine(Name);
  Name = "csharp";
  Console.WriteLine(Name);
  Name = null;
  Console.WriteLine(Name);
    }
}

 

6. Use the null conditional operator: ?.

Similar to the coalescing operator, the null conditional operator tests for null before accessing a member of an instance.

 class Program
    {
        static void Main(string[] args)
        {
            Employee Emp = new Employee();
            if (Emp.Name == String.Empty)
            {
                Emp = null;
            }
 
            WriteLine(Emp?.Name ?? "Field is null.");
 
            ReadLine();
        }
    }
 
    public class Employee
    {
        public string Name { get; set; } = "";
    }

 

7. Using Extension Method:

you can Combine NotNull with an extension method as below example.

[System.Diagnostics.DebuggerNonUserCode]
public static class NotNullExtension
{
    public static T NotNull<T>(this T @this) where T : class
    {
        if (@this == null)
        {
            throw new Exception("null value not allowed");
        }

        return @this;
    }
}
//how to use it?
var person = GetPerson().NotNull();

  Thanks for visiting !!

© 2016, Csharp Star. All rights reserved.

Причина

Вкратце

Вы пытаетесь воспользоваться чем-то, что равно null (или Nothing в VB.NET). Это означает, что либо вы присвоили это значение, либо вы ничего не присваивали.

Как и любое другое значение, null может передаваться от объекта к объекту, от метода к методу. Если нечто равно null в методе «А», вполне может быть, что метод «В» передал это значение в метод «А».

Остальная часть статьи описывает происходящее в деталях и перечисляет распространённые ошибки, которые могут привести к исключению NullReferenceException.

Более подробно

Если среда выполнения выбрасывает исключение NullReferenceException, это всегда означает одно: вы пытаетесь воспользоваться ссылкой. И это ссылка не инициализирована (или была инициализирована, но уже не инициализирована).

Это означает, что ссылка равна null, а вы не сможете вызвать методы через ссылку, равную null. В простейшем случае:

string foo = null;
foo.ToUpper();

Этот код выбросит исключение NullReferenceException на второй строке, потому что вы не можете вызвать метод ToUpper() у ссылки на string, равной null.

Отладка

Как определить источник ошибки? Кроме изучения собственно исключения, которое будет выброшено именно там, где оно произошло, вы можете воспользоваться общими рекомендациями по отладке в Visual Studio: поставьте точки останова в ключевых точках, изучите значения переменных, либо расположив курсор мыши над переменной, либо открыв панели для отладки: Watch, Locals, Autos.

Если вы хотите определить место, где значение ссылки устанавливается или не устанавливается, нажмите правой кнопкой на её имени и выберите «Find All References». Затем вы можете поставить точки останова на каждой найденной строке и запустить приложение в режиме отладки. Каждый раз, когда отладкик остановится на точке останова, вы можете удостовериться, что значение верное.

Следя за ходом выполнения программы, вы придёте к месту, где значение ссылки не должно быть null, и определите, почему не присвоено верное значение.

Примеры

Несколько общих примеров, в которых возникает исключение.

Цепочка

ref1.ref2.ref3.member

Если ref1, ref2 или ref3 равно null, вы получите NullReferenceException. Для решения проблемы и определения, что именно равно null, вы можете переписать выражение более простым способом:

var r1 = ref1;
var r2 = r1.ref2;
var r3 = r2.ref3;
r3.member

Например, в цепочке HttpContext.Current.User.Identity.Name, значение может отсутствовать и у HttpContext.Current, и у User, и у Identity.

Неявно

public class Person {
    public int Age { get; set; }
}
public class Book {
    public Person Author { get; set; }
}
public class Example {
    public void Foo() {
        Book b1 = new Book();
        int authorAge = b1.Author.Age; // Свойство Author не было инициализировано
                                       // нет Person, у которого можно вычислить Age.
    }
}

То же верно для вложенных инициализаторов:

Book b1 = new Book { Author = { Age = 45 } };

Несмотря на использование ключевого слова new, создаётся только экземпляр класса Book, но экземпляр Person не создаётся, поэтому свойство Author остаётся null.

Массив

int[] numbers = null;
int n = numbers[0]; // numbers = null. Нет массива, чтобы получить элемент по индексу

Элементы массива

Person[] people = new Person[5];
people[0].Age = 20; // people[0] = null. Массив создаётся, но не
                    // инициализируется. Нет Person, у которого можно задать Age.

Массив массивов

long[][] array = new long[1][];
array[0][0] = 3; // = null, потому что инициализировано только первое измерение.
                 // Сначала выполните array[0] = new long[2].

Collection/List/Dictionary

Dictionary<string, int> agesForNames = null;
int age = agesForNames["Bob"]; // agesForNames = null.
                               // Экземпляр словаря не создан.

LINQ

public class Person {
    public string Name { get; set; }
}
var people = new List<Person>();
people.Add(null);
var names = from p in people select p.Name;
string firstName = names.First(); // Исключение бросается здесь, хотя создаётся
                                  // строкой выше. p = null, потому что
                                  // первый добавленный элемент = null.

События

public class Demo
{
    public event EventHandler StateChanged;

    protected virtual void OnStateChanged(EventArgs e)
    {        
        StateChanged(this, e); // Здесь бросится исключение, если на
                               // событие StateChanged никто не подписался
    }
}

Неудачное именование переменных

Если бы в коде ниже у локальных переменных и полей были разные имена, вы бы обнаружили, что поле не было инициализировано:

public class Form1 {
    private Customer customer;

    private void Form1_Load(object sender, EventArgs e) {
        Customer customer = new Customer();
        customer.Name = "John";
    }

    private void Button_Click(object sender, EventArgs e) {
        MessageBox.Show(customer.Name);
    }
}

Можно избежать проблемы, если использовать префикс для полей:

private Customer _customer;

Цикл жизни страницы ASP.NET

public partial class Issues_Edit : System.Web.UI.Page
{
    protected TestIssue myIssue;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Выполняется только на первой загрузке, но не когда нажата кнопка
            myIssue = new TestIssue(); 
        }
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        myIssue.Entry = "NullReferenceException здесь!";
    }
}

Сессии ASP.NET

// Если сессионная переменная "FirstName" ещё не была задана,
// то эта строка бросит NullReferenceException.
string firstName = Session["FirstName"].ToString();

Пустые вью-модели ASP.NET MVC

Если вы возвращаете пустую модель (или свойство модели) в контроллере, то вью бросит исключение при попытке доступа к ней:

// Controller
public class Restaurant:Controller
{
    public ActionResult Search()
    {
         return View();  // Модель не задана.
    }
}

// Razor view 
@foreach (var restaurantSearch in Model.RestaurantSearch)  // Исключение.
{
}

Способы избежать

Явно проверять на null, пропускать код

Если вы ожидаете, что ссылка в некоторых случаях будет равна null, вы можете явно проверить на это значение перед доступом к членам экземпляра:

void PrintName(Person p) {
    if (p != null) {
        Console.WriteLine(p.Name);
    }
}

Явно проверять на null, использовать значение по умолчанию

Методы могут возвращать null, например, если не найден требуемый экземпляр. В этом случае вы можете вернуть значение по умолчанию:

string GetCategory(Book b) {
    if (b == null)
        return "Unknown";
    return b.Category;
}

Явно проверять на null, выбрасывать своё исключение

Вы также можете бросать своё исключение, чтобы позже его поймать:

string GetCategory(string bookTitle) {
    var book = library.FindBook(bookTitle);  // Может вернуть null
    if (book == null)
        throw new BookNotFoundException(bookTitle);  // Ваше исключение
    return book.Category;
}

Использовать Debug.Assert для проверки на null для обнаружения ошибки до бросания исключения

Если во время разработки вы знаете, что метод может, но вообще-то не должен возвращать null, вы можете воспользоваться Debug.Assert для быстрого обнаружения ошибки:

string GetTitle(int knownBookID) {
    // Вы знаете, что метод не должен возвращать null
    var book = library.GetBook(knownBookID);  

    // Исключение будет выброшено сейчас, а не в конце метода.
    Debug.Assert(book != null, "Library didn't return a book for known book ID.");

    // Остальной код...

    return book.Title; // Не выбросит NullReferenceException в режиме отладки.
}

Однако эта проверка не будет работать в релизной сборке, и вы снова получите NullReferenceException, если book == null.

Использовать GetValueOrDefault() для Nullable типов

DateTime? appointment = null;
Console.WriteLine(appointment.GetValueOrDefault(DateTime.Now));
// Отобразит значение по умолчанию, потому что appointment = null.

appointment = new DateTime(2022, 10, 20);
Console.WriteLine(appointment.GetValueOrDefault(DateTime.Now));
// Отобразит дату, а не значение по умолчанию.

Использовать оператор ?? (C#) или If() (VB)

Краткая запись для задания значения по умолчанию:

IService CreateService(ILogger log, Int32? frobPowerLevel)
{
    var serviceImpl = new MyService(log ?? NullLog.Instance);
    serviceImpl.FrobPowerLevel = frobPowerLevel ?? 5;
}

Использовать операторы ?. и ?[ (C# 6+, VB.NET 14+):

Это оператор безопасного доступа к членам, также известный как оператор Элвиса за специфическую форму. Если выражение слева от оператора равно null, то правая часть игнорируется, и результатом считается null. Например:

var title = person.Title.ToUpper();

Если свойство Title равно null, то будет брошено исключение, потому что это попытка вызвать метод ToUpper на значении, равном null. В C# 5 и ниже можно добавить проверку:

var title = person.Title == null ? null : person.Title.ToUpper();

Теперь вместо бросания исключения переменной title будет присвоено null. В C# 6 был добавлен более короткий синтаксис:

var title = person.Title?.ToUpper();

Разумеется, если переменная person может быть равна null, то надо проверять и её. Также можно использовать операторы ?. и ?? вместе, чтобы предоставить значение по умолчанию:

// обычная проверка на null
int titleLength = 0;
if (title != null)
    titleLength = title.Length;

// совмещаем операторы `?.` и `??`
int titleLength = title?.Length ?? 0;

Если любой член в цепочке может быть null, то можно полностью обезопасить себя (хотя, конечно, архитектуру стоит поставить под сомнение):

int firstCustomerOrderCount = customers?[0]?.Orders?.Count() ?? 0;

Quite often people stumble into same problems again and again, and getting a NullReferenceException
is the one that occurs most frequently, and frankly, can be quite annoying. This problem happens when
writing brand-new ASP.NET MVC code, such as controllers or views, but also when modifying existing
code that used to work just fine, but somehow suddenly got broken. Here I want to show you why these
exceptions happen and how to fix them, so you can stop wasting your time and do more of the
programming that you actually enjoy.

Not sure where should those trycatch blocks go in your code? Learn how to deal with various
types of exceptions in your apps: Where should you put “try … catch” statements in your C#
code

What is NullReferenceException and where can it happen?

NullReferenceException is exactly what it says — it is thrown by .NET Runtime
when your code tries to access properties or call methods using empty, or null,
reference. It sounds obvious and trite, but finding a place in your code where
things went haywire may take some time.

NullReferenceException in .cshtml Razor views

Let’s say you have a UserProfile.cshtml page that displays “Industry” selection
drop down list for a logged in user (to put a dropdown on a form see my other
article):

@model UserProfileModel

<div class="form-group">
    @Html.LabelFor(m => m.Industry)
    @Html.EnumDropDownListFor(model => model.Industry,
                              Model.Industries,
                              "- Please select your industry -",
                              new { @class = "form-control" })
</div>

and when you hit this page you see the following exception:

Server Error in ‘/’ Application.


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 27:                     <div class="form-group">
Line 28:                         @Html.LabelFor(m => m.Industry)
Line 29:                         @Html.EnumDropDownListFor(m => m.Industry,
Line 30:                                                   Model.Industries,
Line 31:                                                   "- Please select your industry -",

Source File: c:[…]ViewsProfileUserProfile.cshtml    Line: 29

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
ASP._Page_Views_Profile_UserProfile_cshtml.Execute() in c:...ViewsProfileUserProfile.cshtml:29
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271
[... some lines deleted ...]
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +932
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +188


Believe me or not, this problem trips people over most often. The reason for this exception
is that no model is being passed to the view from the controller’s action:

public ActionResult UserProfile() {
    var userModel = GetLoggedInUser();
    //
    // … some more super-serious business logic in here ...
    //
    return View(); // <-- HERE'S THE PROBLEM!
}

That’s why when the view is being rendered, Model variable inside of .cshtml
points to null, and trying to access null.FirstName obviously throws a brand
spanking new NullReferenceException. To fix this need to pass an instance of
the model to the View() call:

public ActionResult UserProfile() {
    var userModel = GetLoggedInUser();
    //
    // … some more super-serious business logic in here ...
    //
    return View(userModel); // <-- JUST PASS THE MODEL HERE, TOO EASY!
}

NullReferenceException when working with database entities

This is a second most-common problem — when loading entities from a database,
you cannot always be sure whether an object you’re trying to load or one of its
linked entities exists. Say there’s an optional Address object that
UserProfile object can be linked to. If you do the following in your code,
you’ll get a NullReferenceException when Address is missing:

public int GetUserPostCode(int userId) {
    var user = GetUserFromDb(userId);
    return user.Address.PostCode;
}

In which case the following exception will blow your call stack right up:

Server Error in ‘/’ Application.


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 55:         public int GetUserPostCode(int userId) {
Line 56:             var user = GetUserFromDb(userId);
Line 57:             return user.Address.PostCode;
Line 58:         }

Source File: c:…ControllersProfileController.cs    Line: 57

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
App.Controllers.ProfileController.ViewProfile() in c:...ControllersProfileController.cs:57
lambda_method(Closure , ControllerBase , Object[] ) +101
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
... some lines skipped...
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +932
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +188


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34212

To fix this exception, you need to retrieve data in a safe manner — by checking all the reference
types first and avoiding dangerous assumptions that related objects are always going to be present:

public int? GetUserPostCode(int userId) { // Notice that function returns nullable int now
    var user = GetUserFromDb(userId);
    if (user != null && user.Address != null) { // This check will help to avoid NullReferenceExceptions
        return user.Address.PostCode;
    }
    return null;
}

NullReferenceException when calling methods on null

Similarly, calling any functions on a null reference will result in
NullReferenceException. Say you are building an auction site, where users can
add photos to their listings. There’s class called ‘Listing’ that has a
collection of ‘Photos’, and you want users to be able to submit new listings
along with the photos in the following manner:

[HttpPost]
public ActionResult NewListing(ListingModel model) {
    var newListing = new ListingDBObject {
        Price = model.Price,
        Title = model.Title,
        ContactNumber = model.ContactNumber
    }

    foreach (var photo in model.Photos) {
        newUser.Photos.Add(new PhotoDBObject { // <- THIS WILL BLOW UP!
            BinaryData = photo.Data;
        });
    }

    //...here goes code that saves new listing to the database
}

Again, you’ll get a brand new NullReferenceException with the following stack
trace:

Server Error in ‘/’ Application.


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 25:
Line 26:    foreach (var photo in model.Photos) {
Line 27:        newUser.Photos.Add(new PhotoDBObject {
Line 58:            BinaryData = photo.Data;

Source File: c:…ControllersProfileController.cs    Line: 27

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
App.Controllers.ProfileController.NewListing() in c:...ControllersProfileController.cs:27
lambda_method(Closure , ControllerBase , Object[] ) +101
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
... some lines skipped...
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +932
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +188


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34212

To fix this you need to initialise the collection first:

[HttpPost]
public ActionResult NewListing(ListingModel model) {
    var newListing = new ListingDBObject {
        Price = model.Price,
        Title = model.Title,
        ContactNumber = model.ContactNumber
    }

    newUser.Photos = new List<PhotoDBObject>(); // Initialise the collection first so it's not null
    foreach (var photo in model.Photos) {
        newUser.Photos.Add(new PhotoDBObject { // Add objects to the collection
            BinaryData = photo.Data;
        });
    }

    //...here goes code that saves new listing to the database
}

But Wait, There’s More!

I hope this article helped you to solve your problem and saved you a bit of time and frustration!

Don’t miss my next post — subscribe to the mailing list to get handy tips and solutions for ASP.NET
NVC Core. I never spam, and you can unsubscribe at any time.

Subscribe now and get helpful tips on developing .NET apps — never miss a new article.

Понравилась статья? Поделить с друзьями:
  • Произошла ошибка ssl на айфоне что делать
  • Произошла ошибка ssl безопасное подключение к серверу невозможно
  • Произошла ошибка ssl альфа банк
  • Произошла ошибка 500 мтс модем
  • Произошла ошибка 500 behavior framework not found