Ошибка 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

У меня есть код и когда он выполняется, он выдает NullReferenceException, говоря:

Ссылка на объект не установлена ​​в экземпляр объекта.

Что это значит и что я могу сделать, чтобы исправить эту ошибку?

4b9b3361

Ответ 1

В чем причина?

Нижняя линия

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

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

null могут иметь разные значения:

  1. Объектные переменные, которые неинициализируются и, следовательно, не указывают на ничего. В этом случае, если вы NullReferenceException к свойствам или методам таких объектов, это вызывает NullReferenceException.
  2. Разработчик использует null намеренно, чтобы указать, что нет значимого значения. Обратите внимание, что С# имеет понятие обнуляемых типов данных для переменных (например, таблицы базы данных могут иметь поля с null значением) — вы можете присвоить им null чтобы указать, что в нем нет значения, например int? a = null; int? a = null; где знак вопроса указывает, что ему разрешено хранить значение null в переменной a. Вы можете проверить это с помощью if (a.HasValue) {...} или с if (a==null) {...}. Необязательные переменные, как и a этом примере, позволяют получить доступ к значению через a.Value явно или как обычно, через a.
    Обратите внимание, что доступ к нему через a.Value вызывает InvalidOperationException вместо NullReferenceException если a имеет значение null — вы должны сделать проверку заранее, то есть, если у вас есть другая переменная с возможностью NULL int b; то вам следует выполнять задания типа if (a.HasValue) { b = a.Value; } if (a.HasValue) { b = a.Value; } или короче, if (a != null) { b = a; } if (a != null) { b = a; }.

В оставшейся части этой статьи более подробно описаны ошибки, которые часто делают многие программисты, которые могут привести к NullReferenceException.

Более конкретно

Время выполнения, которое бросает NullReferenceException всегда означает одно и то же: вы пытаетесь использовать ссылку, а ссылка не инициализируется (или она была однажды инициализирована, но больше не инициализирована).

Это означает, что ссылка имеет значение null, и вы не можете получить доступ к элементам (таким как методы) через null ссылку. Простейший случай:

string foo = null;
foo.ToUpper();

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

отладка

Как вы находите источник NullReferenceException? Помимо рассмотрения самого исключения, которое будет выбрасываться именно в том месте, где оно происходит, применяются общие правила отладки в Visual Studio: размещайте стратегические точки останова и проверяйте свои переменные, наводя указатель мыши на их имена, открывая ( Quick) Смотреть окно или использовать различные панели отладки, такие как Locals and Autos.

Если вы хотите узнать, где ссылка или не установлена, щелкните ее имя правой кнопкой мыши и выберите «Найти все ссылки». Затем вы можете разместить точку останова в каждом найденном месте и запустить свою программу с прикрепленным отладчиком. Каждый раз, когда отладчик разбивается на такую точку останова, вам нужно определить, хотите ли вы, чтобы ссылка была не нулевой, проверите переменную и убедитесь, что она указывает на экземпляр, когда вы ожидаете этого.

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

Примеры

Некоторые распространенные сценарии, в которых может быть выбрано исключение:

общий

ref1.ref2.ref3.member

Если ref1 или ref2 или ref3 равно null, вы получите NullReferenceException. Если вы хотите решить проблему, то выясните, какой из них является нулевым, переписывая выражение в его более простой эквивалент:

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

В частности, в HttpContext.Current.User.Identity.Name, HttpContext.Current может быть null или свойство User может быть null или свойство Identity может быть null.

непрямой

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; // You never initialized the Author property.
                                       // there is no Person to get an Age from.
    }
}

Если вы хотите избежать ссылки на null (Person), вы можете инициализировать его в родительском (Book) объектном конструкторе.

Вложенные инициализаторы объектов

То же самое относится к инициализаторам вложенных объектов:

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

Это переводит

Book b1 = new Book();
b1.Author.Age = 45;

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

Инициализаторы вложенной коллекции

public class Person {
    public ICollection<Book> Books { get; set; }
}
public class Book {
    public string Title { get; set; }
}

Инициализаторы вложенных коллекций ведут себя одинаково:

Person p1 = new Person {
    Books = {
        new Book { Title = "Title1" },
        new Book { Title = "Title2" },
    }
};

Это переводит

Person p1 = new Person();
p1.Books.Add(new Book { Title = "Title1" });
p1.Books.Add(new Book { Title = "Title2" });

new Person создает только экземпляр Person, но коллекция Books по-прежнему равна null. Синтаксис инициализатора коллекции не создает коллекцию для p1.Books, она переводит только в p1.Books.Add(...).

массив

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

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

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

Жесткие массивы

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.

Коллекция/Список/Словарь

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

Переменная диапазона (косвенная/отложенная)

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.

Мероприятия

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
    }
}

Плохие соглашения об именах:

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

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)
        {
            // Only called on first load, not when button clicked
            myIssue = new TestIssue(); 
        }
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        myIssue.Entry = "NullReferenceException here!";
    }
}

Значения сеанса ASP.NET

// if the "FirstName" session value has not yet been set,
// then this line will throw a NullReferenceException
string firstName = Session["FirstName"].ToString();

Пустые модели просмотра ASP.NET MVC

Если исключение возникает при ссылке на свойство @Model в представлении ASP.NET MVC, вам нужно понять, что Model получает значение в вашем методе действий при return представления. Когда вы возвращаете пустую модель (или свойство модели) с вашего контроллера, исключение возникает, когда представления обращаются к нему:

// Controller
public class Restaurant:Controller
{
    public ActionResult Search()
    {
         return View();  // Forgot the provide a Model here.
    }
}

// Razor view 
@foreach (var restaurantSearch in Model.RestaurantSearch)  // Throws.
{
}

<p>@Model.somePropertyName</p> <!-- Also throws -->

Порядок создания WPF-контроля и события

Элементы управления WPF создаются во время вызова InitializeComponent в том порядке, в котором они отображаются в визуальном дереве. NullReferenceException будет поднято в случае ранних созданных элементов управления с обработчиками событий и т.д., NullReferenceException срабатывают во время InitializeComponent которые ссылаются на поздние созданные элементы управления.

Например:

<Grid>
    <!-- Combobox declared first -->
    <ComboBox Name="comboBox1" 
              Margin="10"
              SelectedIndex="0" 
              SelectionChanged="comboBox1_SelectionChanged">
        <ComboBoxItem Content="Item 1" />
        <ComboBoxItem Content="Item 2" />
        <ComboBoxItem Content="Item 3" />
    </ComboBox>

    <!-- Label declared later -->
    <Label Name="label1" 
           Content="Label"
           Margin="10" />
</Grid>

Здесь comboBox1 создается до label1. Если comboBox1_SelectionChanged пытается ссылаться на ‘label1, он еще не создан.

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    label1.Content = comboBox1.SelectedIndex.ToString(); // NullReference here!!
}

Изменение порядка объявлений в XAML (т. label1 comboBox1 label1 перед comboBox1, игнорируя проблемы философии дизайна, по крайней мере разрешит здесь NullReferenceException.

В ролях с, as

var myThing = someObject as Thing;

Это не вызывает InvalidCastException, но возвращает null при неудачном запуске (и когда someObject сам является нулевым). Поэтому имейте это в виду.

LINQ FirstOrDefault() и SingleOrDefault()

Простые версии First() и Single() исключают исключения, когда ничего нет. В этом случае версии «OrDefault» возвращают null. Поэтому имейте это в виду.

для каждого

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

 List<int> list = null;    
 foreach(var v in list) { } // exception

Более реалистичный пример — выбор узлов из XML-документа. Будут выбрасываться, если узлы не найдены, но первоначальная отладка показывает, что все свойства действительны:

 foreach (var node in myData.MyXml.DocumentNode.SelectNodes("//Data"))

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

Явно проверяю значение null и игнорирую 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 на null метода и выставляю собственное исключение.

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

string GetCategory(string bookTitle) {
    var book = library.FindBook(bookTitle);  // This may return null
    if (book == null)
        throw new BookNotFoundException(bookTitle);  // Your custom exception
    return book.Category;
}

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

Когда вы знаете во время разработки, что метод, возможно, может, но никогда не должен возвращать null, вы можете использовать Debug.Assert() чтобы как можно скорее сломаться, когда это произойдет:

string GetTitle(int knownBookID) {
    // You know this should never return null.
    var book = library.GetBook(knownBookID);  

    // Exception will occur on the next line instead of at the end of this method.
    Debug.Assert(book != null, "Library didn't return a book for known book ID.");

    // Some other code

    return book.Title; // Will never throw NullReferenceException in Debug mode.
}

Хотя эта проверка не закончится в вашей сборке релизов, заставив ее снова сбросить NullReferenceException когда book == null во время выполнения в режиме деблокирования.

Используйте GetValueOrDefault() для типов значений с null значением, чтобы предоставить значение по умолчанию, когда оно равно 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

Использовать нулевой коалесцирующий оператор: ?? [С#] или If() [VB].

Сокращение до значения по умолчанию, когда встречается null:

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

    // Note that the above "GetValueOrDefault()" can also be rewritten to use
    // the coalesce operator:
    serviceImpl.FrobPowerLevel = frobPowerLevel ?? 5;
}

Используйте оператор нулевого условия: ?. или ?[x] для массивов (доступно на С# 6 и VB.NET 14):

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

var title = person.Title.ToUpper();

Если у человека нет названия, это вызовет исключение, поскольку оно пытается вызвать ToUpper для свойства с нулевым значением.

В С# 5 и ниже это можно защитить:

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

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

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

Это приведет к названию переменной бытия null, и призыв к ToUpper не выполняется, если person.Title является null.

Конечно, вам все равно нужно проверить title для null или использовать оператор null condition вместе с нулевым коалесцирующим оператором (??), чтобы указать значение по умолчанию:

// regular null check
int titleLength = 0;
if (title != null)
    titleLength = title.Length; // If title is null, this would throw NullReferenceException

// combining the '?' and the '??' operator
int titleLength = title?.Length ?? 0;

Аналогично, для массивов вы можете использовать ?[i] следующим образом:

int[] myIntArray=null;
var i=5;
int? elem = myIntArray?[i];
if (!elem.HasValue) Console.WriteLine("No value");

Это сделает следующее: Если myIntArray имеет значение null, выражение возвращает null, и вы можете спокойно его проверить. Если он содержит массив, он будет делать то же самое, что: elem = myIntArray[i]; и возвращает i- й элемент.

Специальные методы для отладки и исправления нулевых дереф в итераторах

С# поддерживает «блоки итератора» (называемые «генераторами» на некоторых других популярных языках). Исключения исключений в виде исключений могут быть особенно сложными для отладки в блоках итератора из-за отложенного выполнения:

public IEnumerable<Frob> GetFrobs(FrobFactory f, int count)
{
    for (int i = 0; i < count; ++i)
      yield return f.MakeFrob();
}
...
FrobFactory factory = whatever;
IEnumerable<Frobs> frobs = GetFrobs();
...
foreach(Frob frob in frobs) { ... }

Если whatever, null MakeFrob whatever результаты в null, то MakeFrob бросит. Теперь вы можете подумать, что правильно это сделать:

// DON'T DO THIS
public IEnumerable<Frob> GetFrobs(FrobFactory f, int count)
{
    if (f == null) 
      throw new ArgumentNullException("f", "factory must not be null");
    for (int i = 0; i < count; ++i)
      yield return f.MakeFrob();
}

Почему это неправильно? Поскольку блок итератора фактически не запускается до тех пор, пока не будет foreach ! Вызов GetFrobs просто возвращает объект, который при итерации будет запускать блок итератора.

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

Правильное исправление:

// DO THIS
public IEnumerable<Frob> GetFrobs(FrobFactory f, int count)
{
    // No yields in a public method that throws!
    if (f == null) 
      throw new ArgumentNullException("f", "factory must not be null");
    return GetFrobsForReal(f, count);
}
private IEnumerable<Frob> GetFrobsForReal(FrobFactory f, int count)
{
    // Yields in a private method
    Debug.Assert(f != null);
    for (int i = 0; i < count; ++i)
      yield return f.MakeFrob();
}

То есть, создайте частный вспомогательный метод, который имеет логику блока итератора, и метод открытой поверхности, который выполняет нулевую проверку и возвращает итератор. Теперь, когда GetFrobs, нулевая проверка происходит немедленно, а затем GetFrobsForReal выполняется, когда последовательность повторяется.

Если вы изучите источник ссылок для LINQ to Objects, вы увидите, что этот метод используется повсюду. Это немного сложнее писать, но значительно облегчает отладку ошибок с ошибками. Оптимизируйте свой код для удобства вызывающего абонента, а не для удобства автора.

Замечание о нулевых различиях в небезопасном коде

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

В небезопасном режиме вы должны знать два важных факта:

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

Чтобы понять, почему это так, это помогает понять, как.NET в первую очередь создает исключения нулевой разницы. (Эти сведения относятся к.NET, работающему в Windows, другие операционные системы используют аналогичные механизмы.)

Память виртуализирована в Windows; каждый процесс получает пространство виртуальной памяти из многих «страниц» памяти, которые отслеживаются операционной системой. На каждой странице памяти установлены флаги, определяющие способ их использования: чтение, запись, выполнение и т.д. Самая низкая страница отмечена как «выдавать ошибку, если она когда-либо используется».

И нулевой указатель, и нулевая ссылка в С# внутренне представлены как числовое значение нуль, и поэтому любая попытка разыменовать его в соответствующем хранилище памяти приводит к тому, что операционная система создает ошибку. Затем среда выполнения.NET обнаруживает эту ошибку и превращает ее в исключение нулевого исключения.

То, что разыменовывание как нулевого указателя, так и нулевой ссылки создает одно и то же исключение.

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

Почему это имеет смысл? Ну, предположим, что у нас есть структура, содержащая два ints, а неуправляемый указатель равен нулю. Если мы попытаемся разыменовать второй int в структуре, CLR не будет пытаться получить доступ к хранилищу при нулевом местоположении; он получит доступ к хранилищу в четвертом месте. Но логически это нулевое разыменование, потому что мы получаем этот адрес через нуль.

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

Ответ 2

Исключение NullReference — Visual Basic

NullReference Exception для Visual Basic ничем не отличается от NullReference Exception в С#. В конце концов, они оба сообщают об одном и том же исключении, определенном в.NET Framework, которое они оба используют. Причины, уникальные для Visual Basic, встречаются редко (возможно, только один).

В этом ответе будут использоваться термины, синтаксис и контекст Visual Basic. Используемые примеры исходят из большого количества прошлых вопросов о переполнении стека. Это делается для того, чтобы максимизировать актуальность, используя ситуации, часто встречающиеся в сообщениях. Немного больше объяснений также предоставляется тем, кому это может понадобиться. Пример, похожий на ваш, очень вероятно указан здесь.

Заметка:

  1. Это основано на концепции: в вашем проекте нет кода для вставки. Он призван помочь вам понять, что вызывает NullReferenceException (NRE), как его найти, как его исправить и как его избежать. NRE может быть вызвано многими способами, поэтому это вряд ли будет вашей единственной встречей.
  2. Примеры (из Qaru posts) не всегда показывают лучший способ сделать что-то в первую очередь.
  3. Как правило, используется самое простое средство.

Основное значение

Сообщение «Объект, не установленный для экземпляра объекта» означает, что вы пытаетесь использовать объект, который не был инициализирован. Это сводится к одному из следующих:

  • Ваш код объявлял объектную переменную, но не инициализировал ее (создайте экземпляр или «создайте экземпляр»)
  • Что-то, что предполагал ваш код, инициализирует объект, не
  • Возможно, другой код преждевременно аннулировал объект, который все еще используется

Поиск причины

Поскольку проблема — это ссылка на объект, которая является » Nothing, ответ заключается в том, чтобы изучить их, чтобы узнать, какой из них. Затем определите, почему он не инициализирован. Держите мышь над различными переменными, и Visual Studio (VS) покажет их значения — виновником будет Nothing.

IDE debug display

Вы также должны удалить блоки Try/Catch из соответствующего кода, особенно те, где в блоке Catch ничего нет. Это приведет к сбою вашего кода при попытке использовать объект Nothing. Это то, что вы хотите, потому что оно идентифицирует точное местоположение проблемы и позволяет идентифицировать вызывающий объект.

MsgBox в Catch, который отображает Error while..., мало поможет. Этот метод также приводит к очень плохим вопросам, потому что вы не можете описать фактическое исключение, объект или даже строку кода, где это происходит.

Вы также можете использовать Locals Window (Debug → Windows → Locals) для проверки ваших объектов.

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

Смотрите также:

  • Контрольные точки
  • MSDN: Как использовать блок Try/Catch для выхвата исключений
  • MSDN: рекомендации по исключениям

Примеры и средства защиты

Объекты класса/Создание экземпляра

Dim reg As CashRegister
...
TextBox1.Text = reg.Amount         ' NRE

Проблема заключается в том, что Dim не создает объект CashRegister; он объявляет только переменную с именем reg этого типа. Объявление переменной объекта и создание экземпляра — это две разные вещи.

средство

Оператор New может часто использоваться для создания экземпляра при его объявлении:

Dim reg As New CashRegister        ' [New] creates instance, invokes the constructor

' Longer, more explicit form:
Dim reg As CashRegister = New CashRegister

Когда целесообразно создать экземпляр позже:

Private reg As CashRegister         ' Declare
  ...
reg = New CashRegister()            ' Create instance

Примечание. Не используйте Dim снова в процедуре, включая конструктор (Sub New):

Private reg As CashRegister
'...

Public Sub New()
   '...
   Dim reg As New CashRegister
End Sub

Это создаст локальную переменную reg, которая существует только в этом контексте (sub). Переменная reg с уровнем модуля Scope который вы будете использовать везде, останется Nothing.

Отсутствие оператора New является причиной № 1 NullReference Exceptions рассмотренной в рассмотренных вопросах.

Visual Basic пытается многократно очистить процесс, используя New: Использование New Operator создает новый объект и вызывает Sub New — конструктор — где ваш объект может выполнять любую другую инициализацию.

Чтобы быть ясным, Dim (или Private) объявляет только переменную и ее Type. Область действия переменной — независимо от того, существует ли она для всего модуля/класса или является локальной процедурой — определяется тем, где она объявлена. Private | Friend | Public Private | Friend | Public определяет уровень доступа, а не Scope.

Для получения дополнительной информации см.

  • Новый оператор
  • Область применения Visual Basic
  • Уровни доступа в Visual Basic
  • Типы значений и ссылочные типы

Массивы

Массивы также должны быть созданы:

Private arr as String()

Этот массив только объявлен, а не создан. Существует несколько способов инициализации массива:

Private arr as String() = New String(10){}
' or
Private arr() As String = New String(10){}

' For a local array (in a procedure) and using 'Option Infer':
Dim arr = New String(10) {}

Примечание. Начиная с VS 2010, при инициализации локального массива с использованием литерала и Option Infer, Option Infer As <Type> и New являются необязательными:

Dim myDbl As Double() = {1.5, 2, 9.9, 18, 3.14}
Dim myDbl = New Double() {1.5, 2, 9.9, 18, 3.14}
Dim myDbl() = {1.5, 2, 9.9, 18, 3.14}

Тип данных и размер массива выводятся из назначаемых данных. Объявление уровня класса/модуля по-прежнему требует использования As <Type> с Option Strict:

Private myDoubles As Double() = {1.5, 2, 9.9, 18, 3.14}

Пример: массив объектов класса

Dim arrFoo(5) As Foo

For i As Integer = 0 To arrFoo.Count - 1
   arrFoo(i).Bar = i * 10       ' Exception
Next

Массив создан, но объекты Foo в нем нет.

средство

For i As Integer = 0 To arrFoo.Count - 1
    arrFoo(i) = New Foo()         ' Create Foo instance
    arrFoo(i).Bar = i * 10
Next

Использование List(Of T) затруднит наличие элемента без действительного объекта:

Dim FooList As New List(Of Foo)     ' List created, but it is empty
Dim f As Foo                        ' Temporary variable for the loop

For i As Integer = 0 To 5
    f = New Foo()                    ' Foo instance created
    f.Bar =  i * 10
    FooList.Add(f)                   ' Foo object added to list
Next

Для получения дополнительной информации см.

  • Вывод опциона Infer
  • Область применения Visual Basic
  • Массивы в Visual Basic

Списки и коллекции

Коллекции.NET (из которых есть много разновидностей — Списки, Словарь и т.д.) Также должны быть созданы или созданы.

Private myList As List(Of String)
..
myList.Add("ziggy")           ' NullReference

Вы получаете одно и то же исключение по той же причине — myList был объявлен, но экземпляр не создан. Средство защиты одно и то же:

myList = New List(Of String)

' Or create an instance when declared:
Private myList As New List(Of String)

Общий надзор — это класс, в котором используется коллекция Type:

Public Class Foo
    Private barList As List(Of Bar)

    Friend Function BarCount As Integer
        Return barList.Count
    End Function

    Friend Sub AddItem(newBar As Bar)
        If barList.Contains(newBar) = False Then
            barList.Add(newBar)
        End If
    End Function

Любая процедура приведет к NRE, потому что barList объявляется, а не создается. Создание экземпляра Foo также не создаст экземпляр внутреннего barList. Возможно, это было намерение сделать это в конструкторе:

Public Sub New         ' Constructor
    ' Stuff to do when a new Foo is created...
    barList = New List(Of Bar)
End Sub

Как и раньше, это неверно:

Public Sub New()
    ' Creates another barList local to this procedure
     Dim barList As New List(Of Bar)
End Sub

Дополнительные сведения см. В разделе List(Of T).


Объекты поставщика данных

Работа с базами данных предоставляет много возможностей для NullReference, потому что одновременно может быть много объектов (Command, Connection, Transaction, Dataset, DataTable, DataRows….). Примечание. Неважно, какой поставщик данных вы используете — MySQL, SQL Server, OleDB и т.д. — концепции одинаковы.

Пример 1

Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim MaxRows As Integer

con.Open()
Dim sql = "SELECT * FROM tblfoobar_List"
da = New OleDbDataAdapter(sql, con)
da.Fill(ds, "foobar")
con.Close()

MaxRows = ds.Tables("foobar").Rows.Count      ' Error

Как и ранее, объект ds Dataset был объявлен, но экземпляр никогда не создавался. DataAdapter заполнит существующий DataSet, а не создаст его. В этом случае, поскольку ds является локальной переменной, IDE предупреждает вас, что это может произойти:

img

Когда объявляется как переменная уровня модуля/класса, как представляется, в случае с con, компилятор не может знать, был ли объект создан процедурой восходящего потока. Не игнорируйте предупреждения.

средство

Dim ds As New DataSet

Пример 2.

ds = New DataSet
da = New OleDBDataAdapter(sql, con)
da.Fill(ds, "Employees")

txtID.Text = ds.Tables("Employee").Rows(0).Item(1)
txtID.Name = ds.Tables("Employee").Rows(0).Item(2)

Опечатка является проблемой здесь: Employees против Employee. Не было создано DataTable именем «Employee», поэтому результаты NullReferenceException пытаются получить к нему доступ. Другая потенциальная проблема заключается в предположении, что будут Items которые могут быть не такими, когда SQL включает предложение WHERE.

средство

Поскольку это использует одну таблицу, использование Tables(0) позволит избежать орфографических ошибок. Изучение Rows.Count также может помочь:

If ds.Tables(0).Rows.Count > 0 Then
    txtID.Text = ds.Tables(0).Rows(0).Item(1)
    txtID.Name = ds.Tables(0).Rows(0).Item(2)
End If

Fill — это функция, возвращающая количество затронутых Rows которые также могут быть протестированы:

If da.Fill(ds, "Employees") > 0 Then...

Пример 3.

Dim da As New OleDb.OleDbDataAdapter("SELECT TICKET.TICKET_NO,
        TICKET.CUSTOMER_ID, ... FROM TICKET_RESERVATION AS TICKET INNER JOIN
        FLIGHT_DETAILS AS FLIGHT ... WHERE [TICKET.TICKET_NO]= ...", con)
Dim ds As New DataSet
da.Fill(ds)

If ds.Tables("TICKET_RESERVATION").Rows.Count > 0 Then

DataAdapter обеспечит TableNames, как показано в предыдущем примере, но не разобрать имена из SQL или базы данных таблицы. В результате ds.Tables("TICKET_RESERVATION") ссылается на несуществующую таблицу.

Средство устранения одинаково, ссылайтесь на таблицу по индексу:

If ds.Tables(0).Rows.Count > 0 Then

См. Также Класс DataTable.


Пути объектов/Вложенные

If myFoo.Bar.Items IsNot Nothing Then
   ...

Код только тестирует Items то время как myFoo и Bar также могут быть Nothing. Средство состоит в том, чтобы проверить всю цепочку или путь объектов по одному:

If (myFoo IsNot Nothing) AndAlso
    (myFoo.Bar IsNot Nothing) AndAlso
    (myFoo.Bar.Items IsNot Nothing) Then
    ....

AndAlso важно. Последующие тесты не будут выполняться после первого состояния False. Это позволяет коду безопасно «сверлить» на объект один «уровень» за раз, оценивая myFoo.Bar только после (и если) myFoo определяется как действительный. Цепочки объектов или пути могут быть довольно длинными при кодировании сложных объектов:

myBase.myNodes(3).Layer.SubLayer.Foo.Files.Add("somefilename")

Невозможно ссылаться на «нисходящий поток» null объекта. Это также относится к элементам управления:

myWebBrowser.Document.GetElementById("formfld1").InnerText = "some value"

Здесь myWebBrowser или Document может быть Nothing или элемент formfld1 может не существовать.


Элементы управления пользовательским интерфейсом

Dim cmd5 As New SqlCommand("select Cartons, Pieces, Foobar " _
     & "FROM Invoice where invoice_no = '" & _
     Me.ComboBox5.SelectedItem.ToString.Trim & "' And category = '" & _
     Me.ListBox1.SelectedItem.ToString.Trim & "' And item_name = '" & _
     Me.ComboBox2.SelectedValue.ToString.Trim & "' And expiry_date = '" & _
     Me.expiry.Text & "'", con)

Помимо всего прочего, этот код не предполагает, что пользователь может не выбрать что-либо в одном или нескольких элементах управления пользовательским интерфейсом. ListBox1.SelectedItem вполне может быть Nothing, поэтому ListBox1.SelectedItem.ToString приведет к NRE.

средство

Проверяйте данные перед их использованием (также используйте параметры Option Strict и SQL):

Dim expiry As DateTime         ' for text date validation
If (ComboBox5.SelectedItems.Count > 0) AndAlso
    (ListBox1.SelectedItems.Count > 0) AndAlso
    (ComboBox2.SelectedItems.Count > 0) AndAlso
    (DateTime.TryParse(expiry.Text, expiry) Then

    '... do stuff
Else
    MessageBox.Show(...error message...)
End If

Кроме того, вы можете использовать (ComboBox5.SelectedItem IsNot Nothing) AndAlso...


Визуальные базовые формы

Public Class Form1

    Private NameBoxes = New TextBox(5) {Controls("TextBox1"), _
                   Controls("TextBox2"), Controls("TextBox3"), _
                   Controls("TextBox4"), Controls("TextBox5"), _
                   Controls("TextBox6")}

    ' same thing in a different format:
    Private boxList As New List(Of TextBox) From {TextBox1, TextBox2, TextBox3 ...}

    ' Immediate NRE:
    Private somevar As String = Me.Controls("TextBox1").Text

Это довольно распространенный способ получить NRE. В С#, в зависимости от того, как он кодируется, среда IDE сообщит, что Controls не существуют в текущем контексте, или «не может ссылаться на нестатический член». Итак, в какой-то степени это ситуация только с VB. Это также сложно, потому что это может привести к отказу каскада.

Таким образом, массивы и коллекции не могут быть инициализированы. Этот код инициализации будет выполняться до того, как конструктор создаст Form или Controls. В результате:

  • Списки и коллекция будут просто пустыми
  • Массив будет содержать пять элементов Nothing
  • Назначение somevar приведет к немедленному NRE, потому что Nothing не имеет свойства .Text

После этого ссылки на элементы массива приведут к NRE. Если вы делаете это в Form_Load, из-за нечетной ошибки, среда IDE может не сообщать об исключении, когда это происходит. Исключение появится позже, когда ваш код попытается использовать массив. Это «молчаливое исключение» подробно описано в этом сообщении. Для наших целей ключ заключается в том, что, когда что-то катастрофическое происходит при создании формы (Sub New или Form Load event), исключения могут не сообщаться, код выходит из процедуры и просто отображает форму.

Поскольку никакой другой код в вашем событии Sub New или Form Load будет запущен после NRE, многие другие вещи могут быть оставлены неинициализированными.

Sub Form_Load(..._
   '...
   Dim name As String = NameBoxes(2).Text        ' NRE
   ' ...
   ' More code (which will likely not be executed)
   ' ...
End Sub

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

Public Class Form1

    Private myFiles() As String = Me.OpenFileDialog1.FileName & ...
    Private dbcon As String = OpenFileDialog1.FileName & ";Jet Oledb..."
    Private studentName As String = TextBox13.Text

Частичное средство

Любопытно, что VB не дает предупреждение, а средство объявить контейнеры на уровне формы, но инициализировать их в обработчик события загрузки формы, когда элементы управления существует. Это можно сделать в Sub New если ваш код после вызова InitializeComponent:

' Module level declaration
Private NameBoxes as TextBox()
Private studentName As String

' Form Load, Form Shown or Sub New:
'
' Using the OP approach (illegal using OPTION STRICT)
NameBoxes = New TextBox() {Me.Controls("TextBox1"), Me.Controls("TestBox2"), ...)
studentName = TextBox32.Text           ' For simple control references

Код массива, возможно, еще не вышел из леса. Любые элементы управления, которые находятся в контроле контейнера (например, GroupBox или Panel), не будут найдены в Me.Controls; они будут находиться в коллекции Controls этой Panel или GroupBox. Также не будет возвращен элемент управления, если контрольное имя будет написано неправильно ("TeStBox2"). В таких случаях в этих элементах массива Nothing не будет сохранено, и при попытке ссылаться на него будет возникать NRE.

Теперь вам будет легко найти, что вы знаете, что ищете: VS shows you the error of your ways

«Button2» находится на Panel

средство

Вместо косвенных ссылок по имени с использованием коллекции Controls формы используйте ссылку на управление:

' Declaration
Private NameBoxes As TextBox()

' Initialization -  simple and easy to read, hard to botch:
NameBoxes = New TextBox() {TextBox1, TextBox2, ...)

' Initialize a List
NamesList = New List(Of TextBox)({TextBox1, TextBox2, TextBox3...})
' or
NamesList = New List(Of TextBox)
NamesList.AddRange({TextBox1, TextBox2, TextBox3...})

Функция, возвращающая ничего

Private bars As New List(Of Bars)        ' Declared and created

Public Function BarList() As List(Of Bars)
    bars.Clear
    If someCondition Then
        For n As Integer = 0 to someValue
            bars.Add(GetBar(n))
        Next n
    Else
        Exit Function
    End If

    Return bars
End Function

Это случай, когда IDE предупредит вас, что «не все пути возвращают значение и может возникнуть NullReferenceException «. Вы можете подавить предупреждение, заменив Exit Function на Return Nothing, но это не решит проблему. Все, что пытается использовать возврат, когда someCondition = False приведет к NRE:

bList = myFoo.BarList()
For Each b As Bar in bList      ' EXCEPTION
      ...

средство

Замените Exit Function в функции с помощью функции Return bList. Возврат пустого List не совпадает с возвратом Nothing. Если есть вероятность того, что возвращаемый объект может быть Nothing, перед его использованием проверьте:

 bList = myFoo.BarList()
 If bList IsNot Nothing Then...

Плохо реализовано Try/Catch

Плохо реализованный Try/Catch может скрыть, где проблема, и привести к новым:

Dim dr As SqlDataReader
Try
    Dim lnk As LinkButton = TryCast(sender, LinkButton)
    Dim gr As GridViewRow = DirectCast(lnk.NamingContainer, GridViewRow)
    Dim eid As String = GridView1.DataKeys(gr.RowIndex).Value.ToString()
    ViewState("username") = eid
    sqlQry = "select FirstName, Surname, DepartmentName, ExtensionName, jobTitle,
             Pager, mailaddress, from employees1 where username='" & eid & "'"
    If connection.State <> ConnectionState.Open Then
        connection.Open()
    End If
    command = New SqlCommand(sqlQry, connection)

    'More code fooing and barring

    dr = command.ExecuteReader()
    If dr.Read() Then
        lblFirstName.Text = Convert.ToString(dr("FirstName"))
        ...
    End If
    mpe.Show()
Catch

Finally
    command.Dispose()
    dr.Close()             ' <-- NRE
    connection.Close()
End Try

Это случай, когда объект создается не так, как ожидалось, но также демонстрирует полезность счетчика пустого Catch.

В SQL имеется дополнительная запятая (после «mailaddress»), которая приводит к исключению в .ExecuteReader. После того, как Catch ничего не делает, в Finally пытается выполнить очистку, но поскольку вы не можете Close нулевой объект DataReader, получается новое значение NullReferenceException.

Пустой блок Catch — площадка для дьявола. Это О.П. был озадачен, почему он был получать NRE в Finally блоке. В других ситуациях, пустой Catch может привести к чему-то еще намного дальше вниз по течению, идущему в сторону haywire, и заставить вас тратить время на то, чтобы не ошибиться в неполадке проблемы. («Тихая исключение», описанное выше, дает такое же развлекательное значение.)

средство

Не используйте пустые блоки Try/Catch — пусть сбой кода, чтобы вы могли: a) определить причину b) определить местоположение и c) применить надлежащее средство. Блоки Try/Catch не предназначены для того, чтобы скрывать исключения от лица, обладающего уникальной квалификацией для их исправления — разработчика.


DBNull — это не то же самое, что и ничего

For Each row As DataGridViewRow In dgvPlanning.Rows
    If Not IsDBNull(row.Cells(0).Value) Then
        ...

Функция IsDBNull используется для тестирования, если значение равно System.DBNull: From MSDN:

Значение System.DBNull указывает, что объект представляет отсутствующие или несуществующие данные. DBNull — это не то же самое, что и Nothing, что указывает на то, что переменная еще не была инициализирована.

средство

If row.Cells(0) IsNot Nothing Then ...

Как и прежде, вы можете протестировать Nothing, а затем для определенного значения:

If (row.Cells(0) IsNot Nothing) AndAlso (IsDBNull(row.Cells(0).Value) = False) Then

Пример 2.

Dim getFoo = (From f In dbContext.FooBars
               Where f.something = something
               Select f).FirstOrDefault

If Not IsDBNull(getFoo) Then
    If IsDBNull(getFoo.user_id) Then
        txtFirst.Text = getFoo.first_name
    Else
       ...

FirstOrDefault возвращает первый элемент или значение по умолчанию, которое не является Nothing для ссылочных типов и никогда DBNull:

If getFoo IsNot Nothing Then...

управления

Dim chk As CheckBox

chk = CType(Me.Controls(chkName), CheckBox)
If chk.Checked Then
    Return chk
End If

Если CheckBox с chkName не может быть найден (или существует в GroupBox), тогда chk будет Nothing и будет пытаться ссылаться на любое свойство, приведет к исключению.

средство

If (chk IsNot Nothing) AndAlso (chk.Checked) Then ...

DataGridView

Периодически DGV имеет несколько причуд:

dgvBooks.DataSource = loan.Books
dgvBooks.Columns("ISBN").Visible = True       ' NullReferenceException
dgvBooks.Columns("Title").DefaultCellStyle.Format = "C"
dgvBooks.Columns("Author").DefaultCellStyle.Format = "C"
dgvBooks.Columns("Price").DefaultCellStyle.Format = "C"

Если dgvBooks имеет AutoGenerateColumns = True, он будет создавать столбцы, но он их не AutoGenerateColumns = True, поэтому приведенный выше код завершится неудачей, когда он ссылается на них по имени.

средство

Назовите столбцы вручную или ссылку по индексу:

dgvBooks.Columns(0).Visible = True

Пример 2 — Остерегайтесь NewRow

xlWorkSheet = xlWorkBook.Sheets("sheet1")

For i = 0 To myDGV.RowCount - 1
    For j = 0 To myDGV.ColumnCount - 1
        For k As Integer = 1 To myDGV.Columns.Count
            xlWorkSheet.Cells(1, k) = myDGV.Columns(k - 1).HeaderText
            xlWorkSheet.Cells(i + 2, j + 1) = myDGV(j, i).Value.ToString()
        Next
    Next
Next

Когда ваш DataGridView имеет AllowUserToAddRows как True (по умолчанию), Cells в пустой/новой строке внизу будут содержать Nothing. Большинство попыток использования содержимого (например, ToString) приведет к NRE.

средство

Используйте цикл For/Each и проверьте свойство IsNewRow чтобы определить, является ли это последней строкой. Это работает, верно ли AllowUserToAddRows или нет:

For Each r As DataGridViewRow in myDGV.Rows
    If r.IsNewRow = False Then
         ' ok to use this row

Если вы используете цикл For n, измените количество строк или используйте Exit For когда IsNewRow истинно.


My.Settings(StringCollection)

При определенных обстоятельствах попытка использовать элемент из My.Settings который является StringCollection может привести к NullReference при первом использовании. Решение одно и то же, но не так очевидно. Рассматривать:

My.Settings.FooBars.Add("ziggy")         ' foobars is a string collection

Поскольку VB управляет настройками для вас, разумно ожидать, что он инициализирует коллекцию. Он будет, но только если вы ранее добавили начальную запись в коллекцию (в редакторе настроек). Поскольку коллекция (по-видимому) инициализируется при добавлении элемента, она остается » Nothing когда в редакторе настроек не добавляются элементы.

средство

Инициализировать коллекцию настроек в форме Load event handler, если/когда необходимо:

If My.Settings.FooBars Is Nothing Then
    My.Settings.FooBars = New System.Collections.Specialized.StringCollection
End If

Как правило, Settings должна быть инициализирована только при первом запуске приложения. Альтернативным средством является добавление начального значения в вашу коллекцию в Project → Settings | FooBars, сохраните проект, а затем удалите поддельное значение.


Ключевые моменты

Вероятно, вы забыли оператора » New.

или

То, что вы предположили, безупречно выполнило бы, чтобы вернуть инициализированный объект в ваш код, не сделал этого.

Не игнорируйте предупреждения компилятора (когда-либо) и используйте параметр Option Strict On (всегда).


Исключение NullReference MSDN

Ответ 3

Другой сценарий — когда вы приводите нулевой объект в тип значения. Например, код ниже:

object o = null;
DateTime d = (DateTime)o;

Это бросит NullReferenceException на приведение. Это кажется вполне очевидным в приведенном выше примере, но это может произойти в более сложных запоздалых сценариях, когда нулевой объект был возвращен из некоторого кода, который вам не принадлежит, и приведение, например, генерируется некоторой автоматической системой.

Одним из примеров этого является этот простой фрагмент привязки ASP.NET с элементом управления Calendar:

<asp:Calendar runat="server" SelectedDate="<%#Bind("Something")%>" />

Здесь SelectedDate на самом деле является свойством — типа DateTime — типа веб-элемента управления Calendar, и привязка может совершенно вернуть что-то нулевое. Неявный ASP.NET Generator создаст кусок кода, который будет эквивалентен приведенному выше коду. И это вызовет NullReferenceException, которое довольно трудно обнаружить, поскольку оно находится в сгенерированном ASP.NET коде, который прекрасно компилируется…

Ответ 4

Это означает, что рассматриваемая переменная ничем не указана. Я мог бы создать это так:

SqlConnection connection = null;
connection.Open();

Это вызовет ошибку, потому что, пока я объявил переменную «connection«, она ничего не указала. Когда я пытаюсь вызвать член «Open«, там нет ссылки для его разрешения, и он выдает ошибку.

Чтобы избежать этой ошибки:

  • Всегда инициализируйте свои объекты, прежде чем пытаться что-либо сделать с ними.
  • Если вы не уверены, является ли объект нулевым, проверьте его с помощью object == null.

Инструмент Resharper JetBrains определит каждое место в вашем коде, которое имеет возможность ошибки нулевой ссылки, позволяя вам установить нулевую проверку. Эта ошибка является источником ошибок номер один, IMHO.

Ответ 5

Это означает, что ваш код использовал ссылочную переменную объекта, которая была установлена ​​в значение null (т.е. она не ссылалась на экземпляр фактического объекта).

Чтобы предотвратить ошибку, объекты, которые могут быть пустыми, должны быть проверены на значение null перед использованием.

if (myvar != null)
{
    // Go ahead and use myvar
    myvar.property = ...
}
else
{
    // Whoops! myvar is null and cannot be used without first
    // assigning it to an instance reference
    // Attempting to use myvar here will result in NullReferenceException
}

Ответ 6

Помните, что независимо от сценария причина всегда одинакова в .NET:

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

Вы либо никогда не присваивали какую-либо переменную, никогда не создавали экземпляр значения, присвоенного переменной, или вы устанавливали переменную равную Nothing/null вручную, или вы вызывали функцию, которая устанавливает переменную в Nothing/null для вас.

Ответ 7

Примером этого исключения является: Когда вы пытаетесь проверить что-то, это null.

Например:

string testString = null; //Because it doesn't have a value (i.e. it null; "Length" cannot do what it needs to do)

if (testString.Length == 0) // Throws a nullreferenceexception
{
    //Do something
} 

Среда выполнения.NET будет генерировать исключение NullReferenceException при попытке выполнить действие над чем-то, что не было создано, т.е. приведенным выше кодом.

По сравнению с ArgumentNullException, которое обычно выбрасывается как защитная мера, если метод ожидает, что переданное ему значение не является нулевым.

Дополнительная информация содержится в С# NullReferenceException и Null Parameter.

Ответ 8

Обновление С# 8.0, 2019: Обнуляемые ссылочные типы

С# 8.0 вводит пустые ссылочные типы и ненулевые ссылочные типы. Поэтому необходимо проверять только обнуляемые ссылочные типы, чтобы избежать исключения NullReferenceException.


Если вы не инициализировали ссылочный тип и хотите установить или прочитать одно из его свойств, оно выдаст исключение NullReferenceException.

Пример:

Person p = null;
p.Name = "Harry"; // NullReferenceException occurs here.

Вы можете просто избежать этого, проверив, не является ли переменная нулевой:

Person p = null;
if (p!=null)
{
    p.Name = "Harry"; // Not going to run to this point
}

Чтобы полностью понять, почему выбрасывается исключение NullReferenceException, важно знать разницу между типами значений и [ссылочными типами] [3].

Таким образом, если вы имеете дело с типами значений, NullReferenceExceptions не может возникнуть. Хотя при работе с ссылочными типами нужно соблюдать бдительность!

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

Типы ссылок (эти должны быть проверены):

  • динамический
  • объект
  • строка

Типы значений (вы можете просто игнорировать эти):

  • Числовые типы
  • Интегральные типы
  • Типы с плавающей точкой
  • десятичный
  • BOOL
  • Пользовательские структуры

Ответ 9

Другим случаем, когда NullReferenceExceptions может случиться, является (неправильное) использование оператора as:

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

Здесь Book и Car являются несовместимыми типами; Car не может быть преобразован/брошен в Book. Когда этот бросок терпит неудачу, as возвращает null. Использование mybook после этого вызывает NullReferenceException.

В общем, вы должны использовать бросок или as, например:

Если вы ожидаете, что преобразование типа всегда будет успешным (т.е. Вы знаете, что объект должен быть впереди времени), тогда вы должны использовать бросок:

ComicBook cb = (ComicBook)specificBook;

Если вы не уверены в типе, но хотите попытаться использовать его как определенный тип, используйте его as:

ComicBook cb = specificBook as ComicBook;
if (cb != null) {
   // ...
}

Ответ 10

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

Пример:

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

Ошибка исключения:

Необработанное исключение:

System.NullReferenceException: ссылка на объект не установлена ​​в экземпляр объекта. в Program.Main()

Ответ 11

В то время как то, что вызывает NullReferenceExceptions и подходы к исключению/исправлению такого исключения, было рассмотрено в других ответах, что многие программисты еще не изучили, как самостоятельно отлаживать такие исключения во время разработки.

В Visual Studio это обычно легко благодаря Visual Studio Debugger.


Во-первых, убедитесь, что правильная ошибка будет обнаружена — см. Как разрешить нарушение «Исключение System.NullReferenceException» в VS2010? Примечание 1

Затем либо » Начать с отладки» (F5), либо » Прикрепить» [отладчик VS] для выполнения процесса. Иногда может быть полезно использовать Debugger.Break, который предложит запустить отладчик.

Теперь, когда выбрано NullReferenceException (или необработанное), отладчик остановится (вспомните правило, установленное выше?) В строке, в которой произошло исключение. Иногда ошибка будет легко заметить.

Например, в следующей строке единственным кодом, который может вызвать исключение, является то, что myString значение null. Это можно проверить, посмотрев окно просмотра или выполнив выражения в окне Immediate.

var x = myString.Trim();

В более сложных случаях, таких как следующее, вам нужно будет использовать один из методов выше (Watch или Immediate Windows), чтобы проверить выражения, чтобы определить, было ли str1 пустым или str2 был нулевым.

var x = str1.Trim() + str2.Trim();

Когда место, где было выбрано исключение, было расположено, оно обычно тривиально по отношению к разуму назад, чтобы выяснить, где введено неверное значение —

Найдите время, необходимое для понимания причины исключения. Проверьте нулевые выражения. Проверьте предыдущие выражения, которые могли бы привести к таким нулевым выражениям. Добавьте точки останова и, по мере необходимости, выполните программу. Используйте отладчик.


1 Если Break on Throws является слишком агрессивным, и отладчик останавливается на NPE в библиотеке.NET или сторонних разработчиков, Break on User-Unhandled может использоваться для ограничения исключений. Кроме того, VS2012 представляет Just My Code, который я рекомендую также включить.

Если вы отлаживаетесь с включенным Just My Code, поведение немного отличается. При включенном Just My Code отладчик игнорирует исключения, связанные с привилегиями обычного языка (CLR), которые выходят за пределы My Code и не проходят через My Code

Ответ 12

Симон Моурир привел этот пример:

object o = null;
DateTime d = (DateTime)o;  // NullReferenceException

где преобразование (cast) unboxing из object (или из одного из классов System.ValueType или System.Enum или из типа интерфейса) в тип значения (кроме Nullable<>) само по себе дает NullReferenceException.

В другом направлении преобразование бокса из Nullable<> которое HasValue равно false для ссылочного типа, может дать null ссылку, которая затем может привести к NullReferenceException. Классический пример:

DateTime? d = null;
var s = d.ToString();  // OK, no exception (no boxing), returns ""
var t = d.GetType();   // Bang! d is boxed, NullReferenceException

Иногда бокс происходит по-другому. Например, с помощью этого не общего метода расширения:

public static void MyExtension(this object x)
{
  x.ToString();
}

следующий код будет проблематичным:

DateTime? d = null;
d.MyExtension();  // Leads to boxing, NullReferenceException occurs inside the body of the called method, not here.

Эти случаи возникают из-за специальных правил, которые использует среда выполнения при боксировании Nullable<> экземпляров.

Ответ 13

Добавление случая, когда имя класса для сущности, используемой в структуре сущности, такое же, как имя класса для файла с кодировкой веб-формы.

Предположим, что у вас есть веб-форма Contact.aspx, чей класс codebehind имеет контакт, и у вас есть контакт с именем организации.

Затем следующий код будет вызывать исключение NullReferenceException при вызове context.SaveChanges()

Contact contact = new Contact { Name = "Abhinav"};
var context = new DataContext();
context.Contacts.Add(contact);
context.SaveChanges(); // NullReferenceException at this line

Для полноты класса DataContext

public class DataContext : DbContext 
{
    public DbSet<Contact> Contacts {get; set;}
}

и класс контактного объекта. Иногда классы сущностей являются частичными классами, поэтому вы можете распространять их и в других файлах.

public partial class Contact 
{
    public string Name {get; set;}
}

Ошибка возникает, когда и класс entity, и codebehind находятся в одном пространстве имен.
Чтобы исправить это, переименуйте класс сущности или класс codebehind для Contact.aspx.

Причина
Я все еще не уверен в причине. Но всякий раз, когда какой-либо из классов сущностей расширяет System.Web.UI.Page, эта ошибка возникает.

Для обсуждения рассмотрим NullReferenceException в DbContext.saveChanges()

Ответ 14

Другой общий случай, когда можно получить это исключение, включает в себя насмешливые классы во время модульного тестирования. Независимо от используемой насмешливой структуры, вы должны убедиться, что все соответствующие уровни иерархии классов должным образом высмеиваются. В частности, все свойства HttpContext, на которые ссылается тестируемый код, должны быть изделены.

См. «Исключение NullReferenceException при проверке пользовательского атрибута AuthorizationAttribute» для несколько подробного примера.

Ответ 15

У меня есть другая перспектива ответить на это. Такие ответы «что еще можно сделать, чтобы избежать этого?»

При работе в разных слоях, например, в приложении MVC, контроллеру нужны службы для вызова бизнес-операций. В таких сценариях Контейнер инжекции зависимостей может использоваться для инициализации служб, чтобы избежать NullReferenceException. Это означает, что вам не нужно беспокоиться о проверке нулевого и просто вызвать службы с контроллера, как будто они всегда будут доступны (и инициализированы) как одиночный или прототип.

public class MyController
{
    private ServiceA serviceA;
    private ServiceB serviceB;

    public MyController(ServiceA serviceA, ServiceB serviceB)
    {
        this.serviceA = serviceA;
        this.serviceB = serviceB;
    }

    public void MyMethod()
    {
        // We don't need to check null because the dependency injection container 
        // injects it, provided you took care of bootstrapping it.
        var someObject = serviceA.DoThis();
    }
}

Ответ 16

В отношении «что мне делать с этим» может быть много ответов.

Более «формальный» способ предотвращения таких ошибок при разработке применяется дизайн по контракту в вашем коде. Это означает, что вам необходимо установить инварианты классов и/или даже предпосылки и постусловия функций и постусловий в вашей системе при разработке.

Короче говоря, инварианты класса гарантируют, что в вашем классе будут некоторые ограничения, которые не будут нарушены при нормальном использовании (и, следовательно, класс не попадет в несогласованное состояние). Предпосылки означают, что данные, данные как входные данные для функции/метода, должны соответствовать установленным ограничениям и никогда, а постусловия означает, что вывод функции/метода должен снова следовать установленным ограничениям, не нарушая их.
Условия контракта должны быть никогда нарушены во время выполнения программы без ошибок, поэтому дизайн по контракту проверяется на практике в режиме отладки, будучи отключенным в версиях, чтобы максимизировать развитую производительность системы.

Таким образом, вы можете избежать случаев NullReferenceException, которые являются результатом нарушения установленных ограничений. Например, если вы используете свойство объекта X в классе, а затем попытаетесь вызвать один из его методов, а X имеет нулевое значение, это приведет к NullReferenceException:

public X { get; set; }

public void InvokeX()
{
    X.DoSomething(); // if X value is null, you will get a NullReferenceException
}

Но если вы установите «свойство X никогда не должно иметь нулевого значения» в качестве предварительного условия метода, вы можете предотвратить описанный выше сценарий:

//Using code contracts:
[ContractInvariantMethod]
protected void ObjectInvariant () 
{
    Contract.Invariant ( X != null );
    //...
}

По этой причине существует проект Code Contracts для приложений .NET.

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

ОБНОВЛЕНИЕ: Стоит отметить, что этот термин был придуман Бертраном Мейером в связи с его дизайном языка программирования Эйфеля.

Ответ 17

A NullReferenceException возникает, когда мы пытаемся получить доступ к свойствам нулевого объекта или когда значение строки становится пустым и мы пытаемся получить доступ к строковым методам.

Например:

  • При использовании строкового метода пустой строки:

    string str = string.Empty;
    str.ToLower(); // throw null reference exception
    
  • Когда свойство нулевого объекта доступно:

    Public Class Person {
        public string Name { get; set; }
    }
    Person objPerson;
    objPerson.Name  /// throw Null refernce Exception 
    

Ответ 18

TL; DR: Попробуйте использовать Html.Partial вместо Renderpage


Я получал Object reference not set to an instance of an object, когда я пытался отобразить представление в представлении, отправив ему модель, например:

@{
    MyEntity M = new MyEntity();
}
@RenderPage("_MyOtherView.cshtml", M); // error in _MyOtherView, the Model was Null

Отладка показала, что модель была Null внутри MyOtherView. Пока я не изменил его на:

@{
    MyEntity M = new MyEntity();
}
@Html.Partial("_MyOtherView.cshtml", M);

И это сработало.

Кроме того, причина, по которой у меня не было Html.Partial, заключалась в том, что Visual Studio иногда генерирует строчные строки с ошибками в Html.Partial, если она находится внутри другого построенного цикла foreach, хотя это не ошибка:

@inherits System.Web.Mvc.WebViewPage
@{
    ViewBag.Title = "Entity Index";
    List<MyEntity> MyEntities = new List<MyEntity>();
    MyEntities.Add(new MyEntity());
    MyEntities.Add(new MyEntity());
    MyEntities.Add(new MyEntity());
}
<div>
    @{
        foreach(var M in MyEntities)
        {
            // Squiggly lines below. Hovering says: cannot convert method group 'partial' to non-delegate type Object, did you intend to envoke the Method?
            @Html.Partial("MyOtherView.cshtml");
        }
    }
</div>

Но я смог запустить приложение без проблем с этой «ошибкой». Я смог избавиться от ошибки, изменив структуру цикла foreach, чтобы выглядеть так:

@foreach(var M in MyEntities){
    ...
}

Хотя у меня такое чувство, что Visual Studio неправильно интерпретирует амперсанды и скобки.

Ответ 19

Что вы можете сделать с этим?

Здесь есть много хороших ответов, объясняющих, что такое пустая ссылка и как ее отладить. Но очень мало о том, как предотвратить проблему или, по крайней мере, сделать ее легче поймать.

Проверить аргументы

Например, методы могут проверять разные аргументы, чтобы увидеть, являются ли они нулевыми и выбрасывают ArgumentNullException, исключение, явно созданное для этой цели.

Конструктор для ArgumentNullException даже принимает имя параметра и сообщение в качестве аргументов, чтобы вы могли точно сказать разработчику, в чем проблема.

public void DoSomething(MyObject obj) {
    if(obj == null) 
    {
        throw new ArgumentNullException("obj", "Need a reference to obj.");
    }
}

Использовать инструменты

Есть также несколько библиотек, которые могут помочь. Например, «Resharper» может предоставить вам предупреждения при написании кода, особенно если вы используете их атрибут: NotNullAttribute

Там «Контракты кода Microsoft», где вы используете синтаксис типа Contract.Requires(obj != null), который дает вам проверку выполнения и компиляцию: Представление кодовых контрактов.

Там также «PostSharp», который позволит вам просто использовать такие атрибуты:

public void DoSometing([NotNull] obj)

Сделав это и сделав часть PostSharp вашего процесса сборки obj, будет проверяться на нуль во время выполнения. См.: Ошибка проверки PostSharp

Решение простого кода

Или вы всегда можете использовать свой собственный подход, используя простой старый код. Например, вот структура, которую вы можете использовать, чтобы поймать нулевые ссылки. Он смоделирован по той же концепции, что и Nullable<T>:

[System.Diagnostics.DebuggerNonUserCode]
public struct NotNull<T> where T: class
{
    private T _value;

    public T Value
    {
        get
        {
            if (_value == null)
            {
                throw new Exception("null value not allowed");
            }

            return _value;
        }
        set
        {
            if (value == null)
            {
                throw new Exception("null value not allowed.");
            }

            _value = value;
        }
    }

    public static implicit operator T(NotNull<T> notNullValue)
    {
        return notNullValue.Value;
    }

    public static implicit operator NotNull<T>(T value)
    {
        return new NotNull<T> { Value = value };
    }
}

Вы бы очень похожи на то, как вы использовали бы Nullable<T>, кроме как с целью достижения совершенно противоположного — не разрешать null. Вот несколько примеров:

NotNull<Person> person = null; // throws exception
NotNull<Person> person = new Person(); // OK
NotNull<Person> person = GetPerson(); // throws exception if GetPerson() returns null

NotNull<T> неявно отображается в и из T, поэтому вы можете использовать его в любом месте, где это необходимо. Например, вы можете передать объект Person методу, который принимает NotNull<Person>:

Person person = new Person { Name = "John" };
WriteName(person);

public static void WriteName(NotNull<Person> person)
{
    Console.WriteLine(person.Value.Name);
}

Как вы можете видеть выше, как с помощью nullable, вы получите доступ к базовому значению с помощью свойства Value. Кроме того, вы можете использовать явный или неявный листинг, вы можете увидеть пример с возвращаемым значением ниже:

Person person = GetPerson();

public static NotNull<Person> GetPerson()
{
    return new Person { Name = "John" };
}

Или вы даже можете использовать его, когда метод просто возвращает T (в данном случае Person), выполнив бросок. Например, следующий код будет похож на код выше:

Person person = (NotNull<Person>)GetPerson();

public static Person GetPerson()
{
    return new Person { Name = "John" };
}

Объединить с расширением

Объедините NotNull<T> с методом расширения, и вы можете охватить еще больше ситуаций. Вот пример того, как выглядит метод расширения:

[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;
    }
}

И вот пример того, как он может быть использован:

var person = GetPerson().NotNull();

GitHub

Для справки я сделал код выше, доступный на GitHub, вы можете найти его по адресу:

https://github.com/luisperezphd/NotNull

Функция родственного языка

С# 6.0 представил «нуль-условный оператор», который немного помогает в этом. С помощью этой функции вы можете ссылаться на вложенные объекты, и если какой-либо из них null, все выражение возвращает null.

Это уменьшает количество нулевых проверок, которые вы должны выполнить в некоторых случаях. Синтаксис заключается в том, чтобы поставить вопросительный знак перед каждой точкой. Возьмите следующий код, например:

var address = country?.State?.County?.City;

Предположим, что country является объектом типа country, который имеет свойство, называемое State и т.д. Если country, State, County или City есть null, то address will be null . Therefore you only have to check whether address is null`.

Это отличная функция, но она дает вам меньше информации. Это не делает очевидным, какой из 4 является нулевым.

Встроенный как Nullable?

С# имеет красивое сокращение для Nullable<T>, вы можете сделать что-то нулевое, поставив знак вопроса после такого типа int?.

Было бы неплохо, если бы у С# было что-то вроде структуры NotNull<T> выше и имела аналогичную стенографию, может быть, восклицательный знак (!), чтобы вы могли написать что-то вроде: public void WriteName(Person! person).

Ответ 20

Исключить исключение NullReferenceException можно с помощью Null-условных операторов в С# 6 и написать меньше кода для обработки нулевых проверок.

Он использовал для проверки значения null перед выполнением операции доступа к члену (?.) или индекса (? [).

Пример

  var name = p?.Spouse?.FirstName;

эквивалентно:

    if (p != null)
    {
        if (p.Spouse != null)
        {
            name = p.Spouse.FirstName;
        }
    }

В результате имя будет пустым, если p равно null или p.Spouse — null.

В противном случае имени переменной будет присвоено значение p.Spouse.FirstName.

Для получения дополнительной информации: Нулевые условные операторы

Ответ 21

Строка ошибки «Ссылка на объект не установлена ​​в экземпляр объекта.» заявляет, что вы не присвоили экземпляр объекта объектной ссылке, и все же вы получаете доступ к свойствам/методам этого объекта.

например: допустим, что у вас есть класс с именем myClass, и он содержит одно свойство prop1.

public Class myClass
{
   public int prop1 {get;set;}
}

Теперь вы получаете доступ к этому prop1 в другом классе, как показано ниже:

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref.prop1 = 1;  //This line throws error
     }
}

выше строка выдает ошибку, потому что ссылка класса myClass объявлена, но не создана, или экземпляр объекта не назначен для referecne этого класса.

Чтобы исправить это, вы должны создать экземпляр (присвоить объект ссылке на этот класс).

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref = new myClass();
        ref.prop1 = 1;  
     }
}

Ответ 22

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

Случай с краем # 1: одновременный доступ к словарю

Общие словари в .NET не являются потокобезопасными, и иногда они могут бросать NullReference или даже (чаще) a KeyNotFoundException при попытке получить доступ к ключу из двух параллельных потоков. Исключение в этом случае вводит в заблуждение.

Кромка # 2: небезопасный код

Если a NullReferenceException выбрано кодом unsafe, вы можете посмотреть на переменные указателя и проверить их на IntPtr.Zero или что-то еще. Это одно и то же ( «исключение нулевого указателя» ), но в небезопасном коде переменные часто переводятся в типы значений/массивы и т.д., И вы ударяете головой о стену, задаваясь вопросом, как тип значения может исключение.

(Еще одна причина для небезопасного использования небезопасного кода, если вам это не нужно)

Ответ 23

Ссылка NullReferenceException или Object, не установленная на экземпляр объекта, возникает, когда объект класса, который вы пытаетесь использовать, не создается.
Например:

Предположим, что у вас есть класс с именем Student.

public class Student
{
    private string FirstName;
    private string LastName;
    public string GetFullName()
    {
        return FirstName + LastName;
    }
}

Теперь рассмотрим другой класс, в котором вы пытаетесь получить полное имя студента.

public class StudentInfo
{      
    public string GetStudentName()
    {
        Student s;
        string fullname = s.GetFullName();
        return fullname;
    }        
}

Как видно из приведенного выше кода, утверждение
Студент s — объявляет только переменную типа Student, обратите внимание, что класс Student не создается в этой точке.
Следовательно, когда выполняется инструкция s.GetFullName(), она выкинет исключение NullReferenceException.

Ответ 24

Ну, простыми словами:

Вы пытаетесь получить доступ к объекту, который не создан или в данный момент отсутствует в памяти.

Итак, как справиться с этим:

  1. Отладьте и дайте отладчику сломаться… Он напрямую приведет вас к сломанной переменной… Теперь ваша задача просто исправить это… Используя ключевое слово new в соответствующем месте.

  2. Если это вызвано в некоторых командах базы данных, потому что объект не присутствует, тогда все, что вам нужно сделать, это сделать нулевую проверку и обработать ее:

    if (i == null) {
        // Handle this
    }
    
  3. Сложнее всего… если GC уже собрал объект… Обычно это происходит, если вы пытаетесь найти объект с помощью строк… То есть, находя его по имени объекта, может случиться так, что GC может уже Вычистил это… Это трудно найти и станет большой проблемой… Лучший способ справиться с этим — делать нулевые проверки везде, где это необходимо во время процесса разработки. Это сэкономит вам много времени.

Под поиском по имени я подразумеваю некоторую инфраструктуру, позволяющую вам FIndObjects использовать строки, и код может выглядеть следующим образом: FindObject («ObjectName»);

Ответ 25

Если мы рассмотрим общие сценарии, в которых может быть выбрано это исключение, доступ к свойствам с объектом вверху.

Пример:

string postalcode=Customer.Address.PostalCode; 
//if customer or address is null , this will through exeption

здесь, если адрес равен NULL, тогда вы получите исключение NullReferenceException.

Итак, как практика, мы всегда должны использовать проверку нуля, прежде чем обращаться к свойствам в таких объектах (особенно в родовых)

string postalcode=Customer?.Address?.PostalCode;
//if customer or address is null , this will return null, without through a exception

Ответ 26

Буквально самый простой способ исправить NullReferenceExeption имеет два пути.
Если у вас есть GameObject, например, с прикрепленным script и переменной с именем rb (rigidbody), эта переменная начнет пустую, когда вы начнете игру.
Вот почему вы получаете NullReferenceExeption, потому что на компьютере нет данных, хранящихся в этой переменной.

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

  • Добавьте RigidBody к вашему объекту с помощью AddComponent > Физикa > Rigidbody
    Затем зайдите в свой script и введите rb = GetComponent<Rigidbody>();
    Эта строка кода лучше всего работает под вашими функциями Start() или Awake().
  • Вы можете добавить компонент программно и назначить переменную одновременно с одной строкой кода: rb = AddComponent<RigidBody>();

Дальнейшие примечания. Если вы хотите, чтобы единство добавляло компонент к вашему объекту, и вы, возможно, забыли добавить его, вы можете ввести [RequireComponent(typeof(RigidBody))] над объявлением класса (пробел ниже всех ваших приложений).
Наслаждайтесь и получайте удовольствие от игр!

Ответ 27

Для типов ссылок по умолчанию используется null, чтобы указать, что они не ссылаются на какой-либо объект. Следовательно, если вы попытаетесь получить доступ к объекту, на который ссылаетесь, и его нет, вы получите NullReferenceException.

Для Ex:

SqlConnection connection = null;
connection.Open();

Когда вы запустите этот код, вы получите:

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

Вы можете избежать этой ошибки путем кодирования следующим образом:

if (connection != null){
    connection.Open();
}

Примечание. Чтобы избежать этой ошибки, вы всегда должны инициализировать свои объекты, прежде чем пытаться что-либо сделать с ними.

Ответ 28

Если вы получаете это сообщение во время сохранения или компиляции сборки, просто закройте все файлы, а затем откройте любой файл для компиляции и сохранения.

Для меня причина в том, что я переименовал файл, и старый файл все еще был открыт.

Ответ 29

Чтобы использовать методы и член объекта, вам сначала нужно создать этот объект. Если вы его не создали (переменная, которая должна содержать объект, не инициализируется), но вы пытаетесь использовать его методы или переменные, вы получите эту ошибку.

Иногда вы можете просто забыть инициализировать.

Отредактировано: new не может вернуть значение null, но исключение fire при неудаче. Давно это было на некоторых языках, но не больше. Спасибо @John Saunders за указание на это.

Ответ 30

Это означает, что вы пытаетесь манипулировать тем, что имеет ссылку, но еще не инициализировано

Первое, что нужно сделать, это проверить каждый созданный экземпляр.

Используйте контрольные точки, часы, проверяйте свои значения varibale.

Следить за трассировкой стека и искать точные строки и столбцы, которые создают проблему.

Причина

Вкратце

Вы пытаетесь воспользоваться чем-то, что равно 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;

NullReference Exception — Visual Basic

The NullReference Exception for Visual Basic is no different from the one in C#. After all, they are both reporting the same exception defined in the .NET Framework which they both use. Causes unique to Visual Basic are rare (perhaps only one).

This answer will use Visual Basic terms, syntax, and context. The examples used come from a large number of past Stack  Overflow questions. This is to maximize relevance by using the kinds of situations often seen in posts. A bit more explanation is also provided for those who might need it. An example similar to yours is very likely listed here.

Note:

  1. This is concept-based: there is no code for you to paste into your project. It is intended to help you understand what causes a NullReferenceException (NRE), how to find it, how to fix it, and how to avoid it. An NRE can be caused many ways so this is unlikely to be your sole encounter.
  2. The examples (from Stack  Overflow posts) do not always show the best way to do something in the first place.
  3. Typically, the simplest remedy is used.

Basic Meaning

The message «Object not set to an instance of Object» means you are trying to use an object which has not been initialized. This boils down to one of these:

  • Your code declared an object variable, but it did not initialize it (create an instance or ‘instantiate‘ it)
  • Something which your code assumed would initialize an object, did not
  • Possibly, other code prematurely invalidated an object still in use

Finding The Cause

Since the problem is an object reference which is Nothing, the answer is to examine them to find out which one. Then determine why it is not initialized. Hold the mouse over the various variables and Visual Studio (VS) will show their values — the culprit will be Nothing.

IDE debug display

You should also remove any Try/Catch blocks from the relevant code, especially ones where there is nothing in the Catch block. This will cause your code to crash when it tries to use an object which is Nothing. This is what you want because it will identify the exact location of the problem, and allow you to identify the object causing it.

A MsgBox in the Catch which displays Error while... will be of little help. This method also leads to very bad Stack  Overflow questions, because you can’t describe the actual exception, the object involved or even the line of code where it happens.

You can also use the Locals Window (Debug -> Windows -> Locals) to examine your objects.

Once you know what and where the problem is, it is usually fairly easy to fix and faster than posting a new question.

See also:

  • Breakpoints
  • MSDN: How to: Use the Try/Catch Block to Catch Exceptions
  • MSDN: Best Practices for Exceptions

Examples and Remedies

Class Objects / Creating an Instance

Dim reg As CashRegister
...
TextBox1.Text = reg.Amount         ' NRE

The problem is that Dim does not create a CashRegister object; it only declares a variable named reg of that Type. Declaring an object variable and creating an instance are two different things.

Remedy

The New operator can often be used to create the instance when you declare it:

Dim reg As New CashRegister        ' [New] creates instance, invokes the constructor

' Longer, more explicit form:
Dim reg As CashRegister = New CashRegister

When it is only appropriate to create the instance later:

Private reg As CashRegister         ' Declare
  ...
reg = New CashRegister()            ' Create instance

Note: Do not use Dim again in a procedure, including the constructor (Sub New):

Private reg As CashRegister
'...

Public Sub New()
   '...
   Dim reg As New CashRegister
End Sub

This will create a local variable, reg, which exists only in that context (sub). The reg variable with module level Scope which you will use everywhere else remains Nothing.

Missing the New operator is the #1 cause of NullReference Exceptions seen in the Stack  Overflow questions reviewed.

Visual Basic tries to make the process clear repeatedly using New: Using the New Operator creates a new object and calls Sub New — the constructor — where your object can perform any other initialization.

To be clear, Dim (or Private) only declares a variable and its Type. The Scope of the variable — whether it exists for the entire module/class or is local to a procedure — is determined by where it is declared. Private | Friend | Public defines the access level, not Scope.

For more information, see:

  • New Operator
  • Scope in Visual Basic
  • Access Levels in Visual Basic
  • Value Types and Reference Types

Arrays

Arrays must also be instantiated:

Private arr as String()

This array has only been declared, not created. There are several ways to initialize an array:

Private arr as String() = New String(10){}
' or
Private arr() As String = New String(10){}

' For a local array (in a procedure) and using 'Option Infer':
Dim arr = New String(10) {}

Note: Beginning with VS 2010, when initializing a local array using a literal and Option Infer, the As <Type> and New elements are optional:

Dim myDbl As Double() = {1.5, 2, 9.9, 18, 3.14}
Dim myDbl = New Double() {1.5, 2, 9.9, 18, 3.14}
Dim myDbl() = {1.5, 2, 9.9, 18, 3.14}

The data Type and array size are inferred from the data being assigned. Class/Module level declarations still require As <Type> with Option Strict:

Private myDoubles As Double() = {1.5, 2, 9.9, 18, 3.14}

Example: Array of class objects

Dim arrFoo(5) As Foo

For i As Integer = 0 To arrFoo.Count - 1
   arrFoo(i).Bar = i * 10       ' Exception
Next

The array has been created, but the Foo objects in it have not.

Remedy

For i As Integer = 0 To arrFoo.Count - 1
    arrFoo(i) = New Foo()         ' Create Foo instance
    arrFoo(i).Bar = i * 10
Next

Using a List(Of T) will make it quite difficult to have an element without a valid object:

Dim FooList As New List(Of Foo)     ' List created, but it is empty
Dim f As Foo                        ' Temporary variable for the loop

For i As Integer = 0 To 5
    f = New Foo()                    ' Foo instance created
    f.Bar =  i * 10
    FooList.Add(f)                   ' Foo object added to list
Next

For more information, see:

  • Option Infer Statement
  • Scope in Visual Basic
  • Arrays in Visual Basic

Lists and Collections

.NET collections (of which there are many varieties — Lists, Dictionary, etc.) must also be instantiated or created.

Private myList As List(Of String)
..
myList.Add("ziggy")           ' NullReference

You get the same exception for the same reason — myList was only declared, but no instance created. The remedy is the same:

myList = New List(Of String)

' Or create an instance when declared:
Private myList As New List(Of String)

A common oversight is a class which uses a collection Type:

Public Class Foo
    Private barList As List(Of Bar)

    Friend Function BarCount As Integer
        Return barList.Count
    End Function

    Friend Sub AddItem(newBar As Bar)
        If barList.Contains(newBar) = False Then
            barList.Add(newBar)
        End If
    End Function

Either procedure will result in an NRE, because barList is only declared, not instantiated. Creating an instance of Foo will not also create an instance of the internal barList. It may have been the intent to do this in the constructor:

Public Sub New         ' Constructor
    ' Stuff to do when a new Foo is created...
    barList = New List(Of Bar)
End Sub

As before, this is incorrect:

Public Sub New()
    ' Creates another barList local to this procedure
     Dim barList As New List(Of Bar)
End Sub

For more information, see List(Of T) Class.


Data Provider Objects

Working with databases presents many opportunities for a NullReference because there can be many objects (Command, Connection, Transaction, Dataset, DataTable, DataRows….) in use at once. Note: It does not matter which data provider you are using — MySQL, SQL Server, OleDB, etc. — the concepts are the same.

Example 1

Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim MaxRows As Integer

con.Open()
Dim sql = "SELECT * FROM tblfoobar_List"
da = New OleDbDataAdapter(sql, con)
da.Fill(ds, "foobar")
con.Close()

MaxRows = ds.Tables("foobar").Rows.Count      ' Error

As before, the ds Dataset object was declared, but an instance was never created. The DataAdapter will fill an existing DataSet, not create one. In this case, since ds is a local variable, the IDE warns you that this might happen:

img

When declared as a module/class level variable, as appears to be the case with con, the compiler can’t know if the object was created by an upstream procedure. Do not ignore warnings.

Remedy

Dim ds As New DataSet

Example 2

ds = New DataSet
da = New OleDBDataAdapter(sql, con)
da.Fill(ds, "Employees")

txtID.Text = ds.Tables("Employee").Rows(0).Item(1)
txtID.Name = ds.Tables("Employee").Rows(0).Item(2)

A typo is a problem here: Employees vs Employee. There was no DataTable named «Employee» created, so a NullReferenceException results trying to access it. Another potential problem is assuming there will be Items which may not be so when the SQL includes a WHERE clause.

Remedy

Since this uses one table, using Tables(0) will avoid spelling errors. Examining Rows.Count can also help:

If ds.Tables(0).Rows.Count > 0 Then
    txtID.Text = ds.Tables(0).Rows(0).Item(1)
    txtID.Name = ds.Tables(0).Rows(0).Item(2)
End If

Fill is a function returning the number of Rows affected which can also be tested:

If da.Fill(ds, "Employees") > 0 Then...

Example 3

Dim da As New OleDb.OleDbDataAdapter("SELECT TICKET.TICKET_NO,
        TICKET.CUSTOMER_ID, ... FROM TICKET_RESERVATION AS TICKET INNER JOIN
        FLIGHT_DETAILS AS FLIGHT ... WHERE [TICKET.TICKET_NO]= ...", con)
Dim ds As New DataSet
da.Fill(ds)

If ds.Tables("TICKET_RESERVATION").Rows.Count > 0 Then

The DataAdapter will provide TableNames as shown in the previous example, but it does not parse names from the SQL or database table. As a result, ds.Tables("TICKET_RESERVATION") references a non-existent table.

The Remedy is the same, reference the table by index:

If ds.Tables(0).Rows.Count > 0 Then

See also DataTable Class.


Object Paths / Nested

If myFoo.Bar.Items IsNot Nothing Then
   ...

The code is only testing Items while both myFoo and Bar may also be Nothing. The remedy is to test the entire chain or path of objects one at a time:

If (myFoo IsNot Nothing) AndAlso
    (myFoo.Bar IsNot Nothing) AndAlso
    (myFoo.Bar.Items IsNot Nothing) Then
    ....

AndAlso is important. Subsequent tests will not be performed once the first False condition is encountered. This allows the code to safely ‘drill’ into the object(s) one ‘level’ at a time, evaluating myFoo.Bar only after (and if) myFoo is determined to be valid. Object chains or paths can get quite long when coding complex objects:

myBase.myNodes(3).Layer.SubLayer.Foo.Files.Add("somefilename")

It is not possible to reference anything ‘downstream’ of a null object. This also applies to controls:

myWebBrowser.Document.GetElementById("formfld1").InnerText = "some value"

Here, myWebBrowser or Document could be Nothing or the formfld1 element may not exist.


UI Controls

Dim cmd5 As New SqlCommand("select Cartons, Pieces, Foobar " _
     & "FROM Invoice where invoice_no = '" & _
     Me.ComboBox5.SelectedItem.ToString.Trim & "' And category = '" & _
     Me.ListBox1.SelectedItem.ToString.Trim & "' And item_name = '" & _
     Me.ComboBox2.SelectedValue.ToString.Trim & "' And expiry_date = '" & _
     Me.expiry.Text & "'", con)

Among other things, this code does not anticipate that the user may not have selected something in one or more UI controls. ListBox1.SelectedItem may well be Nothing, so ListBox1.SelectedItem.ToString will result in an NRE.

Remedy

Validate data before using it (also use Option Strict and SQL parameters):

Dim expiry As DateTime         ' for text date validation
If (ComboBox5.SelectedItems.Count > 0) AndAlso
    (ListBox1.SelectedItems.Count > 0) AndAlso
    (ComboBox2.SelectedItems.Count > 0) AndAlso
    (DateTime.TryParse(expiry.Text, expiry) Then

    '... do stuff
Else
    MessageBox.Show(...error message...)
End If

Alternatively, you can use (ComboBox5.SelectedItem IsNot Nothing) AndAlso...


Visual Basic Forms

Public Class Form1

    Private NameBoxes = New TextBox(5) {Controls("TextBox1"), _
                   Controls("TextBox2"), Controls("TextBox3"), _
                   Controls("TextBox4"), Controls("TextBox5"), _
                   Controls("TextBox6")}

    ' same thing in a different format:
    Private boxList As New List(Of TextBox) From {TextBox1, TextBox2, TextBox3 ...}

    ' Immediate NRE:
    Private somevar As String = Me.Controls("TextBox1").Text

This is a fairly common way to get an NRE. In C#, depending on how it is coded, the IDE will report that Controls does not exist in the current context, or «cannot reference non-static member». So, to some extent, this is a VB-only situation. It is also complex because it can result in a failure cascade.

The arrays and collections cannot be initialized this way. This initialization code will run before the constructor creates the Form or the Controls. As a result:

  • Lists and Collection will simply be empty
  • The Array will contain five elements of Nothing
  • The somevar assignment will result in an immediate NRE because Nothing doesn’t have a .Text property

Referencing array elements later will result in an NRE. If you do this in Form_Load, due to an odd bug, the IDE may not report the exception when it happens. The exception will pop up later when your code tries to use the array. This «silent exception» is detailed in this post. For our purposes, the key is that when something catastrophic happens while creating a form (Sub New or Form Load event), exceptions may go unreported, the code exits the procedure and just displays the form.

Since no other code in your Sub New or Form Load event will run after the NRE, a great many other things can be left uninitialized.

Sub Form_Load(..._
   '...
   Dim name As String = NameBoxes(2).Text        ' NRE
   ' ...
   ' More code (which will likely not be executed)
   ' ...
End Sub

Note this applies to any and all control and component references making these illegal where they are:

Public Class Form1

    Private myFiles() As String = Me.OpenFileDialog1.FileName & ...
    Private dbcon As String = OpenFileDialog1.FileName & ";Jet Oledb..."
    Private studentName As String = TextBox13.Text

Partial Remedy

It is curious that VB does not provide a warning, but the remedy is to declare the containers at the form level, but initialize them in form load event handler when the controls do exist. This can be done in Sub New as long as your code is after the InitializeComponent call:

' Module level declaration
Private NameBoxes as TextBox()
Private studentName As String

' Form Load, Form Shown or Sub New:
'
' Using the OP's approach (illegal using OPTION STRICT)
NameBoxes = New TextBox() {Me.Controls("TextBox1"), Me.Controls("TestBox2"), ...)
studentName = TextBox32.Text           ' For simple control references

The array code may not be out of the woods yet. Any controls which are in a container control (like a GroupBox or Panel) will not be found in Me.Controls; they will be in the Controls collection of that Panel or GroupBox. Nor will a control be returned when the control name is misspelled ("TeStBox2"). In such cases, Nothing will again be stored in those array elements and an NRE will result when you attempt to reference it.

These should be easy to find now that you know what you are looking for:
VS shows you the error of your ways

«Button2» resides on a Panel

Remedy

Rather than indirect references by name using the form’s Controls collection, use the control reference:

' Declaration
Private NameBoxes As TextBox()

' Initialization -  simple and easy to read, hard to botch:
NameBoxes = New TextBox() {TextBox1, TextBox2, ...)

' Initialize a List
NamesList = New List(Of TextBox)({TextBox1, TextBox2, TextBox3...})
' or
NamesList = New List(Of TextBox)
NamesList.AddRange({TextBox1, TextBox2, TextBox3...})

Function Returning Nothing

Private bars As New List(Of Bars)        ' Declared and created

Public Function BarList() As List(Of Bars)
    bars.Clear
    If someCondition Then
        For n As Integer = 0 to someValue
            bars.Add(GetBar(n))
        Next n
    Else
        Exit Function
    End If

    Return bars
End Function

This is a case where the IDE will warn you that ‘not all paths return a value and a NullReferenceException may result‘. You can suppress the warning, by replacing Exit Function with Return Nothing, but that does not solve the problem. Anything which tries to use the return when someCondition = False will result in an NRE:

bList = myFoo.BarList()
For Each b As Bar in bList      ' EXCEPTION
      ...

Remedy

Replace Exit Function in the function with Return bList. Returning an empty List is not the same as returning Nothing. If there is a chance that a returned object can be Nothing, test before using it:

 bList = myFoo.BarList()
 If bList IsNot Nothing Then...

Poorly Implemented Try/Catch

A badly implemented Try/Catch can hide where the problem is and result in new ones:

Dim dr As SqlDataReader
Try
    Dim lnk As LinkButton = TryCast(sender, LinkButton)
    Dim gr As GridViewRow = DirectCast(lnk.NamingContainer, GridViewRow)
    Dim eid As String = GridView1.DataKeys(gr.RowIndex).Value.ToString()
    ViewState("username") = eid
    sqlQry = "select FirstName, Surname, DepartmentName, ExtensionName, jobTitle,
             Pager, mailaddress, from employees1 where username='" & eid & "'"
    If connection.State <> ConnectionState.Open Then
        connection.Open()
    End If
    command = New SqlCommand(sqlQry, connection)

    'More code fooing and barring

    dr = command.ExecuteReader()
    If dr.Read() Then
        lblFirstName.Text = Convert.ToString(dr("FirstName"))
        ...
    End If
    mpe.Show()
Catch

Finally
    command.Dispose()
    dr.Close()             ' <-- NRE
    connection.Close()
End Try

This is a case of an object not being created as expected, but also demonstrates the counter usefulness of an empty Catch.

There is an extra comma in the SQL (after ‘mailaddress’) which results in an exception at .ExecuteReader. After the Catch does nothing, Finally tries to perform clean up, but since you cannot Close a null DataReader object, a brand new NullReferenceException results.

An empty Catch block is the devil’s playground. This OP was baffled why he was getting an NRE in the Finally block. In other situations, an empty Catch may result in something else much further downstream going haywire and cause you to spend time looking at the wrong things in the wrong place for the problem. (The «silent exception» described above provides the same entertainment value.)

Remedy

Don’t use empty Try/Catch blocks — let the code crash so you can a) identify the cause b) identify the location and c) apply a proper remedy. Try/Catch blocks are not intended to hide exceptions from the person uniquely qualified to fix them — the developer.


DBNull is not the same as Nothing

For Each row As DataGridViewRow In dgvPlanning.Rows
    If Not IsDBNull(row.Cells(0).Value) Then
        ...

The IsDBNull function is used to test if a value equals System.DBNull: From MSDN:

The System.DBNull value indicates that the Object represents missing or non-existent data. DBNull is not the same as Nothing, which indicates that a variable has not yet been initialized.

Remedy

If row.Cells(0) IsNot Nothing Then ...

As before, you can test for Nothing, then for a specific value:

If (row.Cells(0) IsNot Nothing) AndAlso (IsDBNull(row.Cells(0).Value) = False) Then

Example 2

Dim getFoo = (From f In dbContext.FooBars
               Where f.something = something
               Select f).FirstOrDefault

If Not IsDBNull(getFoo) Then
    If IsDBNull(getFoo.user_id) Then
        txtFirst.Text = getFoo.first_name
    Else
       ...

FirstOrDefault returns the first item or the default value, which is Nothing for reference types and never DBNull:

If getFoo IsNot Nothing Then...

Controls

Dim chk As CheckBox

chk = CType(Me.Controls(chkName), CheckBox)
If chk.Checked Then
    Return chk
End If

If a CheckBox with chkName can’t be found (or exists in a GroupBox), then chk will be Nothing and be attempting to reference any property will result in an exception.

Remedy

If (chk IsNot Nothing) AndAlso (chk.Checked) Then ...

The DataGridView

The DGV has a few quirks seen periodically:

dgvBooks.DataSource = loan.Books
dgvBooks.Columns("ISBN").Visible = True       ' NullReferenceException
dgvBooks.Columns("Title").DefaultCellStyle.Format = "C"
dgvBooks.Columns("Author").DefaultCellStyle.Format = "C"
dgvBooks.Columns("Price").DefaultCellStyle.Format = "C"

If dgvBooks has AutoGenerateColumns = True, it will create the columns, but it does not name them, so the above code fails when it references them by name.

Remedy

Name the columns manually, or reference by index:

dgvBooks.Columns(0).Visible = True

Example 2 — Beware of the NewRow

xlWorkSheet = xlWorkBook.Sheets("sheet1")

For i = 0 To myDGV.RowCount - 1
    For j = 0 To myDGV.ColumnCount - 1
        For k As Integer = 1 To myDGV.Columns.Count
            xlWorkSheet.Cells(1, k) = myDGV.Columns(k - 1).HeaderText
            xlWorkSheet.Cells(i + 2, j + 1) = myDGV(j, i).Value.ToString()
        Next
    Next
Next

When your DataGridView has AllowUserToAddRows as True (the default), the Cells in the blank/new row at the bottom will all contain Nothing. Most attempts to use the contents (for example, ToString) will result in an NRE.

Remedy

Use a For/Each loop and test the IsNewRow property to determine if it is that last row. This works whether AllowUserToAddRows is true or not:

For Each r As DataGridViewRow in myDGV.Rows
    If r.IsNewRow = False Then
         ' ok to use this row

If you do use a For n loop, modify the row count or use Exit For when IsNewRow is true.


My.Settings (StringCollection)

Under certain circumstances, trying to use an item from My.Settings which is a StringCollection can result in a NullReference the first time you use it. The solution is the same, but not as obvious. Consider:

My.Settings.FooBars.Add("ziggy")         ' foobars is a string collection

Since VB is managing Settings for you, it is reasonable to expect it to initialize the collection. It will, but only if you have previously added an initial entry to the collection (in the Settings editor). Since the collection is (apparently) initialized when an item is added, it remains Nothing when there are no items in the Settings editor to add.

Remedy

Initialize the settings collection in the form’s Load event handler, if/when needed:

If My.Settings.FooBars Is Nothing Then
    My.Settings.FooBars = New System.Collections.Specialized.StringCollection
End If

Typically, the Settings collection will only need to be initialized the first time the application runs. An alternate remedy is to add an initial value to your collection in Project -> Settings | FooBars, save the project, then remove the fake value.


Key Points

You probably forgot the New operator.

or

Something you assumed would perform flawlessly to return an initialized object to your code, did not.

Don’t ignore compiler warnings (ever) and use Option Strict On (always).


MSDN NullReference Exception

NullReference Exception — Visual Basic

The NullReference Exception for Visual Basic is no different from the one in C#. After all, they are both reporting the same exception defined in the .NET Framework which they both use. Causes unique to Visual Basic are rare (perhaps only one).

This answer will use Visual Basic terms, syntax, and context. The examples used come from a large number of past Stack  Overflow questions. This is to maximize relevance by using the kinds of situations often seen in posts. A bit more explanation is also provided for those who might need it. An example similar to yours is very likely listed here.

Note:

  1. This is concept-based: there is no code for you to paste into your project. It is intended to help you understand what causes a NullReferenceException (NRE), how to find it, how to fix it, and how to avoid it. An NRE can be caused many ways so this is unlikely to be your sole encounter.
  2. The examples (from Stack  Overflow posts) do not always show the best way to do something in the first place.
  3. Typically, the simplest remedy is used.

Basic Meaning

The message «Object not set to an instance of Object» means you are trying to use an object which has not been initialized. This boils down to one of these:

  • Your code declared an object variable, but it did not initialize it (create an instance or ‘instantiate‘ it)
  • Something which your code assumed would initialize an object, did not
  • Possibly, other code prematurely invalidated an object still in use

Finding The Cause

Since the problem is an object reference which is Nothing, the answer is to examine them to find out which one. Then determine why it is not initialized. Hold the mouse over the various variables and Visual Studio (VS) will show their values — the culprit will be Nothing.

IDE debug display

You should also remove any Try/Catch blocks from the relevant code, especially ones where there is nothing in the Catch block. This will cause your code to crash when it tries to use an object which is Nothing. This is what you want because it will identify the exact location of the problem, and allow you to identify the object causing it.

A MsgBox in the Catch which displays Error while... will be of little help. This method also leads to very bad Stack  Overflow questions, because you can’t describe the actual exception, the object involved or even the line of code where it happens.

You can also use the Locals Window (Debug -> Windows -> Locals) to examine your objects.

Once you know what and where the problem is, it is usually fairly easy to fix and faster than posting a new question.

See also:

  • Breakpoints
  • MSDN: How to: Use the Try/Catch Block to Catch Exceptions
  • MSDN: Best Practices for Exceptions

Examples and Remedies

Class Objects / Creating an Instance

Dim reg As CashRegister
...
TextBox1.Text = reg.Amount         ' NRE

The problem is that Dim does not create a CashRegister object; it only declares a variable named reg of that Type. Declaring an object variable and creating an instance are two different things.

Remedy

The New operator can often be used to create the instance when you declare it:

Dim reg As New CashRegister        ' [New] creates instance, invokes the constructor

' Longer, more explicit form:
Dim reg As CashRegister = New CashRegister

When it is only appropriate to create the instance later:

Private reg As CashRegister         ' Declare
  ...
reg = New CashRegister()            ' Create instance

Note: Do not use Dim again in a procedure, including the constructor (Sub New):

Private reg As CashRegister
'...

Public Sub New()
   '...
   Dim reg As New CashRegister
End Sub

This will create a local variable, reg, which exists only in that context (sub). The reg variable with module level Scope which you will use everywhere else remains Nothing.

Missing the New operator is the #1 cause of NullReference Exceptions seen in the Stack  Overflow questions reviewed.

Visual Basic tries to make the process clear repeatedly using New: Using the New Operator creates a new object and calls Sub New — the constructor — where your object can perform any other initialization.

To be clear, Dim (or Private) only declares a variable and its Type. The Scope of the variable — whether it exists for the entire module/class or is local to a procedure — is determined by where it is declared. Private | Friend | Public defines the access level, not Scope.

For more information, see:

  • New Operator
  • Scope in Visual Basic
  • Access Levels in Visual Basic
  • Value Types and Reference Types

Arrays

Arrays must also be instantiated:

Private arr as String()

This array has only been declared, not created. There are several ways to initialize an array:

Private arr as String() = New String(10){}
' or
Private arr() As String = New String(10){}

' For a local array (in a procedure) and using 'Option Infer':
Dim arr = New String(10) {}

Note: Beginning with VS 2010, when initializing a local array using a literal and Option Infer, the As <Type> and New elements are optional:

Dim myDbl As Double() = {1.5, 2, 9.9, 18, 3.14}
Dim myDbl = New Double() {1.5, 2, 9.9, 18, 3.14}
Dim myDbl() = {1.5, 2, 9.9, 18, 3.14}

The data Type and array size are inferred from the data being assigned. Class/Module level declarations still require As <Type> with Option Strict:

Private myDoubles As Double() = {1.5, 2, 9.9, 18, 3.14}

Example: Array of class objects

Dim arrFoo(5) As Foo

For i As Integer = 0 To arrFoo.Count - 1
   arrFoo(i).Bar = i * 10       ' Exception
Next

The array has been created, but the Foo objects in it have not.

Remedy

For i As Integer = 0 To arrFoo.Count - 1
    arrFoo(i) = New Foo()         ' Create Foo instance
    arrFoo(i).Bar = i * 10
Next

Using a List(Of T) will make it quite difficult to have an element without a valid object:

Dim FooList As New List(Of Foo)     ' List created, but it is empty
Dim f As Foo                        ' Temporary variable for the loop

For i As Integer = 0 To 5
    f = New Foo()                    ' Foo instance created
    f.Bar =  i * 10
    FooList.Add(f)                   ' Foo object added to list
Next

For more information, see:

  • Option Infer Statement
  • Scope in Visual Basic
  • Arrays in Visual Basic

Lists and Collections

.NET collections (of which there are many varieties — Lists, Dictionary, etc.) must also be instantiated or created.

Private myList As List(Of String)
..
myList.Add("ziggy")           ' NullReference

You get the same exception for the same reason — myList was only declared, but no instance created. The remedy is the same:

myList = New List(Of String)

' Or create an instance when declared:
Private myList As New List(Of String)

A common oversight is a class which uses a collection Type:

Public Class Foo
    Private barList As List(Of Bar)

    Friend Function BarCount As Integer
        Return barList.Count
    End Function

    Friend Sub AddItem(newBar As Bar)
        If barList.Contains(newBar) = False Then
            barList.Add(newBar)
        End If
    End Function

Either procedure will result in an NRE, because barList is only declared, not instantiated. Creating an instance of Foo will not also create an instance of the internal barList. It may have been the intent to do this in the constructor:

Public Sub New         ' Constructor
    ' Stuff to do when a new Foo is created...
    barList = New List(Of Bar)
End Sub

As before, this is incorrect:

Public Sub New()
    ' Creates another barList local to this procedure
     Dim barList As New List(Of Bar)
End Sub

For more information, see List(Of T) Class.


Data Provider Objects

Working with databases presents many opportunities for a NullReference because there can be many objects (Command, Connection, Transaction, Dataset, DataTable, DataRows….) in use at once. Note: It does not matter which data provider you are using — MySQL, SQL Server, OleDB, etc. — the concepts are the same.

Example 1

Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim MaxRows As Integer

con.Open()
Dim sql = "SELECT * FROM tblfoobar_List"
da = New OleDbDataAdapter(sql, con)
da.Fill(ds, "foobar")
con.Close()

MaxRows = ds.Tables("foobar").Rows.Count      ' Error

As before, the ds Dataset object was declared, but an instance was never created. The DataAdapter will fill an existing DataSet, not create one. In this case, since ds is a local variable, the IDE warns you that this might happen:

img

When declared as a module/class level variable, as appears to be the case with con, the compiler can’t know if the object was created by an upstream procedure. Do not ignore warnings.

Remedy

Dim ds As New DataSet

Example 2

ds = New DataSet
da = New OleDBDataAdapter(sql, con)
da.Fill(ds, "Employees")

txtID.Text = ds.Tables("Employee").Rows(0).Item(1)
txtID.Name = ds.Tables("Employee").Rows(0).Item(2)

A typo is a problem here: Employees vs Employee. There was no DataTable named «Employee» created, so a NullReferenceException results trying to access it. Another potential problem is assuming there will be Items which may not be so when the SQL includes a WHERE clause.

Remedy

Since this uses one table, using Tables(0) will avoid spelling errors. Examining Rows.Count can also help:

If ds.Tables(0).Rows.Count > 0 Then
    txtID.Text = ds.Tables(0).Rows(0).Item(1)
    txtID.Name = ds.Tables(0).Rows(0).Item(2)
End If

Fill is a function returning the number of Rows affected which can also be tested:

If da.Fill(ds, "Employees") > 0 Then...

Example 3

Dim da As New OleDb.OleDbDataAdapter("SELECT TICKET.TICKET_NO,
        TICKET.CUSTOMER_ID, ... FROM TICKET_RESERVATION AS TICKET INNER JOIN
        FLIGHT_DETAILS AS FLIGHT ... WHERE [TICKET.TICKET_NO]= ...", con)
Dim ds As New DataSet
da.Fill(ds)

If ds.Tables("TICKET_RESERVATION").Rows.Count > 0 Then

The DataAdapter will provide TableNames as shown in the previous example, but it does not parse names from the SQL or database table. As a result, ds.Tables("TICKET_RESERVATION") references a non-existent table.

The Remedy is the same, reference the table by index:

If ds.Tables(0).Rows.Count > 0 Then

See also DataTable Class.


Object Paths / Nested

If myFoo.Bar.Items IsNot Nothing Then
   ...

The code is only testing Items while both myFoo and Bar may also be Nothing. The remedy is to test the entire chain or path of objects one at a time:

If (myFoo IsNot Nothing) AndAlso
    (myFoo.Bar IsNot Nothing) AndAlso
    (myFoo.Bar.Items IsNot Nothing) Then
    ....

AndAlso is important. Subsequent tests will not be performed once the first False condition is encountered. This allows the code to safely ‘drill’ into the object(s) one ‘level’ at a time, evaluating myFoo.Bar only after (and if) myFoo is determined to be valid. Object chains or paths can get quite long when coding complex objects:

myBase.myNodes(3).Layer.SubLayer.Foo.Files.Add("somefilename")

It is not possible to reference anything ‘downstream’ of a null object. This also applies to controls:

myWebBrowser.Document.GetElementById("formfld1").InnerText = "some value"

Here, myWebBrowser or Document could be Nothing or the formfld1 element may not exist.


UI Controls

Dim cmd5 As New SqlCommand("select Cartons, Pieces, Foobar " _
     & "FROM Invoice where invoice_no = '" & _
     Me.ComboBox5.SelectedItem.ToString.Trim & "' And category = '" & _
     Me.ListBox1.SelectedItem.ToString.Trim & "' And item_name = '" & _
     Me.ComboBox2.SelectedValue.ToString.Trim & "' And expiry_date = '" & _
     Me.expiry.Text & "'", con)

Among other things, this code does not anticipate that the user may not have selected something in one or more UI controls. ListBox1.SelectedItem may well be Nothing, so ListBox1.SelectedItem.ToString will result in an NRE.

Remedy

Validate data before using it (also use Option Strict and SQL parameters):

Dim expiry As DateTime         ' for text date validation
If (ComboBox5.SelectedItems.Count > 0) AndAlso
    (ListBox1.SelectedItems.Count > 0) AndAlso
    (ComboBox2.SelectedItems.Count > 0) AndAlso
    (DateTime.TryParse(expiry.Text, expiry) Then

    '... do stuff
Else
    MessageBox.Show(...error message...)
End If

Alternatively, you can use (ComboBox5.SelectedItem IsNot Nothing) AndAlso...


Visual Basic Forms

Public Class Form1

    Private NameBoxes = New TextBox(5) {Controls("TextBox1"), _
                   Controls("TextBox2"), Controls("TextBox3"), _
                   Controls("TextBox4"), Controls("TextBox5"), _
                   Controls("TextBox6")}

    ' same thing in a different format:
    Private boxList As New List(Of TextBox) From {TextBox1, TextBox2, TextBox3 ...}

    ' Immediate NRE:
    Private somevar As String = Me.Controls("TextBox1").Text

This is a fairly common way to get an NRE. In C#, depending on how it is coded, the IDE will report that Controls does not exist in the current context, or «cannot reference non-static member». So, to some extent, this is a VB-only situation. It is also complex because it can result in a failure cascade.

The arrays and collections cannot be initialized this way. This initialization code will run before the constructor creates the Form or the Controls. As a result:

  • Lists and Collection will simply be empty
  • The Array will contain five elements of Nothing
  • The somevar assignment will result in an immediate NRE because Nothing doesn’t have a .Text property

Referencing array elements later will result in an NRE. If you do this in Form_Load, due to an odd bug, the IDE may not report the exception when it happens. The exception will pop up later when your code tries to use the array. This «silent exception» is detailed in this post. For our purposes, the key is that when something catastrophic happens while creating a form (Sub New or Form Load event), exceptions may go unreported, the code exits the procedure and just displays the form.

Since no other code in your Sub New or Form Load event will run after the NRE, a great many other things can be left uninitialized.

Sub Form_Load(..._
   '...
   Dim name As String = NameBoxes(2).Text        ' NRE
   ' ...
   ' More code (which will likely not be executed)
   ' ...
End Sub

Note this applies to any and all control and component references making these illegal where they are:

Public Class Form1

    Private myFiles() As String = Me.OpenFileDialog1.FileName & ...
    Private dbcon As String = OpenFileDialog1.FileName & ";Jet Oledb..."
    Private studentName As String = TextBox13.Text

Partial Remedy

It is curious that VB does not provide a warning, but the remedy is to declare the containers at the form level, but initialize them in form load event handler when the controls do exist. This can be done in Sub New as long as your code is after the InitializeComponent call:

' Module level declaration
Private NameBoxes as TextBox()
Private studentName As String

' Form Load, Form Shown or Sub New:
'
' Using the OP's approach (illegal using OPTION STRICT)
NameBoxes = New TextBox() {Me.Controls("TextBox1"), Me.Controls("TestBox2"), ...)
studentName = TextBox32.Text           ' For simple control references

The array code may not be out of the woods yet. Any controls which are in a container control (like a GroupBox or Panel) will not be found in Me.Controls; they will be in the Controls collection of that Panel or GroupBox. Nor will a control be returned when the control name is misspelled ("TeStBox2"). In such cases, Nothing will again be stored in those array elements and an NRE will result when you attempt to reference it.

These should be easy to find now that you know what you are looking for:
VS shows you the error of your ways

«Button2» resides on a Panel

Remedy

Rather than indirect references by name using the form’s Controls collection, use the control reference:

' Declaration
Private NameBoxes As TextBox()

' Initialization -  simple and easy to read, hard to botch:
NameBoxes = New TextBox() {TextBox1, TextBox2, ...)

' Initialize a List
NamesList = New List(Of TextBox)({TextBox1, TextBox2, TextBox3...})
' or
NamesList = New List(Of TextBox)
NamesList.AddRange({TextBox1, TextBox2, TextBox3...})

Function Returning Nothing

Private bars As New List(Of Bars)        ' Declared and created

Public Function BarList() As List(Of Bars)
    bars.Clear
    If someCondition Then
        For n As Integer = 0 to someValue
            bars.Add(GetBar(n))
        Next n
    Else
        Exit Function
    End If

    Return bars
End Function

This is a case where the IDE will warn you that ‘not all paths return a value and a NullReferenceException may result‘. You can suppress the warning, by replacing Exit Function with Return Nothing, but that does not solve the problem. Anything which tries to use the return when someCondition = False will result in an NRE:

bList = myFoo.BarList()
For Each b As Bar in bList      ' EXCEPTION
      ...

Remedy

Replace Exit Function in the function with Return bList. Returning an empty List is not the same as returning Nothing. If there is a chance that a returned object can be Nothing, test before using it:

 bList = myFoo.BarList()
 If bList IsNot Nothing Then...

Poorly Implemented Try/Catch

A badly implemented Try/Catch can hide where the problem is and result in new ones:

Dim dr As SqlDataReader
Try
    Dim lnk As LinkButton = TryCast(sender, LinkButton)
    Dim gr As GridViewRow = DirectCast(lnk.NamingContainer, GridViewRow)
    Dim eid As String = GridView1.DataKeys(gr.RowIndex).Value.ToString()
    ViewState("username") = eid
    sqlQry = "select FirstName, Surname, DepartmentName, ExtensionName, jobTitle,
             Pager, mailaddress, from employees1 where username='" & eid & "'"
    If connection.State <> ConnectionState.Open Then
        connection.Open()
    End If
    command = New SqlCommand(sqlQry, connection)

    'More code fooing and barring

    dr = command.ExecuteReader()
    If dr.Read() Then
        lblFirstName.Text = Convert.ToString(dr("FirstName"))
        ...
    End If
    mpe.Show()
Catch

Finally
    command.Dispose()
    dr.Close()             ' <-- NRE
    connection.Close()
End Try

This is a case of an object not being created as expected, but also demonstrates the counter usefulness of an empty Catch.

There is an extra comma in the SQL (after ‘mailaddress’) which results in an exception at .ExecuteReader. After the Catch does nothing, Finally tries to perform clean up, but since you cannot Close a null DataReader object, a brand new NullReferenceException results.

An empty Catch block is the devil’s playground. This OP was baffled why he was getting an NRE in the Finally block. In other situations, an empty Catch may result in something else much further downstream going haywire and cause you to spend time looking at the wrong things in the wrong place for the problem. (The «silent exception» described above provides the same entertainment value.)

Remedy

Don’t use empty Try/Catch blocks — let the code crash so you can a) identify the cause b) identify the location and c) apply a proper remedy. Try/Catch blocks are not intended to hide exceptions from the person uniquely qualified to fix them — the developer.


DBNull is not the same as Nothing

For Each row As DataGridViewRow In dgvPlanning.Rows
    If Not IsDBNull(row.Cells(0).Value) Then
        ...

The IsDBNull function is used to test if a value equals System.DBNull: From MSDN:

The System.DBNull value indicates that the Object represents missing or non-existent data. DBNull is not the same as Nothing, which indicates that a variable has not yet been initialized.

Remedy

If row.Cells(0) IsNot Nothing Then ...

As before, you can test for Nothing, then for a specific value:

If (row.Cells(0) IsNot Nothing) AndAlso (IsDBNull(row.Cells(0).Value) = False) Then

Example 2

Dim getFoo = (From f In dbContext.FooBars
               Where f.something = something
               Select f).FirstOrDefault

If Not IsDBNull(getFoo) Then
    If IsDBNull(getFoo.user_id) Then
        txtFirst.Text = getFoo.first_name
    Else
       ...

FirstOrDefault returns the first item or the default value, which is Nothing for reference types and never DBNull:

If getFoo IsNot Nothing Then...

Controls

Dim chk As CheckBox

chk = CType(Me.Controls(chkName), CheckBox)
If chk.Checked Then
    Return chk
End If

If a CheckBox with chkName can’t be found (or exists in a GroupBox), then chk will be Nothing and be attempting to reference any property will result in an exception.

Remedy

If (chk IsNot Nothing) AndAlso (chk.Checked) Then ...

The DataGridView

The DGV has a few quirks seen periodically:

dgvBooks.DataSource = loan.Books
dgvBooks.Columns("ISBN").Visible = True       ' NullReferenceException
dgvBooks.Columns("Title").DefaultCellStyle.Format = "C"
dgvBooks.Columns("Author").DefaultCellStyle.Format = "C"
dgvBooks.Columns("Price").DefaultCellStyle.Format = "C"

If dgvBooks has AutoGenerateColumns = True, it will create the columns, but it does not name them, so the above code fails when it references them by name.

Remedy

Name the columns manually, or reference by index:

dgvBooks.Columns(0).Visible = True

Example 2 — Beware of the NewRow

xlWorkSheet = xlWorkBook.Sheets("sheet1")

For i = 0 To myDGV.RowCount - 1
    For j = 0 To myDGV.ColumnCount - 1
        For k As Integer = 1 To myDGV.Columns.Count
            xlWorkSheet.Cells(1, k) = myDGV.Columns(k - 1).HeaderText
            xlWorkSheet.Cells(i + 2, j + 1) = myDGV(j, i).Value.ToString()
        Next
    Next
Next

When your DataGridView has AllowUserToAddRows as True (the default), the Cells in the blank/new row at the bottom will all contain Nothing. Most attempts to use the contents (for example, ToString) will result in an NRE.

Remedy

Use a For/Each loop and test the IsNewRow property to determine if it is that last row. This works whether AllowUserToAddRows is true or not:

For Each r As DataGridViewRow in myDGV.Rows
    If r.IsNewRow = False Then
         ' ok to use this row

If you do use a For n loop, modify the row count or use Exit For when IsNewRow is true.


My.Settings (StringCollection)

Under certain circumstances, trying to use an item from My.Settings which is a StringCollection can result in a NullReference the first time you use it. The solution is the same, but not as obvious. Consider:

My.Settings.FooBars.Add("ziggy")         ' foobars is a string collection

Since VB is managing Settings for you, it is reasonable to expect it to initialize the collection. It will, but only if you have previously added an initial entry to the collection (in the Settings editor). Since the collection is (apparently) initialized when an item is added, it remains Nothing when there are no items in the Settings editor to add.

Remedy

Initialize the settings collection in the form’s Load event handler, if/when needed:

If My.Settings.FooBars Is Nothing Then
    My.Settings.FooBars = New System.Collections.Specialized.StringCollection
End If

Typically, the Settings collection will only need to be initialized the first time the application runs. An alternate remedy is to add an initial value to your collection in Project -> Settings | FooBars, save the project, then remove the fake value.


Key Points

You probably forgot the New operator.

or

Something you assumed would perform flawlessly to return an initialized object to your code, did not.

Don’t ignore compiler warnings (ever) and use Option Strict On (always).


MSDN NullReference Exception

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

In C#, a NullReferenceException occurs when we try to access a variable whose value has not been set or has been set to null. It can be easy to trigger this exception accidentally, so it’s important to be aware of how to avoid it in the first place. In this article, we’ll take a look at some common causes of NullReferenceException errors and how to fix them. We’ll also discuss ways to prevent NullReferenceException errors from happening in the first place.

To download the source code for this article, you can visit our GitHub repository.

Without further ado, let’s get started!

What Is a Null Object in C#?

In C#, a null object is an uninitialized object. This means that the object doesn’t have a value (other than null) assigned to it yet. When we create a new object, it’s automatically assigned a null value. We can change this by explicitly assigning a value to the object, but it will remain null until we do so.

Let’s understand what causes the NullReferenceException in C#. 

As the name suggests, the NullReferenceException in C# occurs when we try accessing a null object.

Just like other object references, we can pass null values when we attempt to dereference them or pass them to other methods, which can make it difficult to debug and fix this exception. 

We can get the NullReferenceException thrown in various scenarios, which we’ll now look at.

Forgetting to Instantiate a Reference Type

Forgetting to instantiate a reference type is one of the most common causes of this exception:

public List<string> StudentList() 
{
    List<string> studentList = null;

    studentList.Add("John Doe");

    return studentList;
}

Here, we intend to return a List<string> containing a value “John Doe” to the user but the compiler throws a NullReferenceException when we attempt to run it. 

Next, we can verify that the compiler throws the NullReferenceException successfully:

[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void GivenAListObject_WhenNotInstantiated_VerifyThrowsNullReferenceException()
{
    var studentObj = new ExceptionMethods();

    var studentList = studentObj.StudentList();

    Assert.IsNull(studentList);
}

To fix that error, we simply need to instantiate the studentList object in the right way:

public List<string> FixedStudentList()
{
    var studentList = new List<string>();

    studentList.Add("John Doe");

    return studentList;
}

We can proceed to verify that the FixedStudentList() method works correctly by checking that it returns “John Doe” and is not null:

var studentObj = new ExceptionMethods();

var studentList = studentObj.FixedStudentList();
var student = "John Doe";

Assert.IsNotNull(studentList);
Assert.IsInstanceOfType(studentList, typeof(List<string>));
CollectionAssert.Contains(studentList, student);

Failing to Dimension Arrays Before Initializing Them

We have to dimension arrays before initializing them. Therefore, when we attempt to declare an array without specifying the number of elements it is going to hold, it will result in a NullReferenceException being thrown when we attempt to initialize its values.

Let’s simulate this scenario with an example:

public int[] RandomNumbers()
{
    var rand = new Random();

    int[] numbers = null;

    for (int i = 0; i < numbers.Length; i++) 
    {
        numbers[i] = rand.Next();
    }
 
    return numbers;
}

Here, we try to generate random numbers but we do not specify the number of elements while declaring the array, which throws the NullRefereceException:

Wanna join Code Maze Team, help us produce more awesome .NET/C# content and get paid? >> JOIN US! <<

[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void GivenAnArray_WhenNotInstantiated_VerifyThrowsNullReferenceException()
{
    var arrayObj = new ExceptionMethods();

    var randomNumbers = arrayObj.RandomNumbers();

    Assert.IsNull(randomNumbers);
}

To fix this error, we need to declare the number of elements before initializing the array:

public int[] FixedRandomNumbers()
{
    var rand = new Random();

    var numbers = new int[50];

    for (int i = 0; i < numbers.Length; i++)
    {
        numbers[i] = rand.Next();
    }

    return numbers;
}

Next, we can also go ahead to verify that the method runs successfully without any errors:

var arrayObj = new ExceptionMethods();

var randomNumbers = arrayObj.FixedRandomNumbers();

Assert.IsNotNull(randomNumbers);
Assert.IsInstanceOfType(randomNumbers, typeof(int[]));          

Assuming a Method Always Returns Non-null Values

As the title suggests, sometimes we may erroneously assume that a method is going to return non-null values. For example, a database table may contain some null values, which we have to account for when implementing our business logic.

Let’s implement a simple class to simulate this scenario:

public class Teacher
{
    public string? FirstName { get; set; }

    public Teacher()
    {
    }

    public Teacher[] AddRange(string[] firstNames)
    {
        var teachers = new Teacher[firstNames.Length];

        for (int i = 0; i < firstNames.Length; i++) 
        {
            teachers[i] = new Teacher(firstNames[i]);
        }
            
        return teachers;
    }

    public Teacher(string firstName)
    {
        this.FirstName = firstName;
    }
}

First, we define a property FirstName that must always have a non-null value when exiting the constructor. The AddRange method takes a string array and returns an object of the type Teacher[]

Next, we are going to implement a simple method to search for a single teacher from the Teacher[] array:

public string Teachers(string searchString)
{
    var personObj = new Teacher();
    
    var people = personObj.AddRange(new string[] { "John", "Mary", "Jane", "Usher", "Andrew", "Grace", "Aston", "Sheila" });
    var result = Array.Find(people, p => p.FirstName == searchString);

    return result.ToString();
}

The Teachers() method takes a string searchString as its sole parameter and uses the inbuilt Array.Find() method to search for it. Since FirstName is a non-nullable property, we are assuming that the method will always return a value.

We can verify that the Teachers method throws a NullReferenceException with this test:

Wanna join Code Maze Team, help us produce more awesome .NET/C# content and get paid? >> JOIN US! <<

[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void GivenAnArray_WhenSearching_VerifyThrowsNullReferenceException()
{
    var listObj = new ExceptionMethods();

    var searchPerson = listObj.Teachers("Steve");

    Assert.IsNull(searchPerson);
}

To address this problem, we need to ensure that we check for the method’s return value to make sure it is not null:

public string FixedTeachers(string searchString)
{
    var personObj = new Teacher();

    var people = personObj.AddRange(new string[] { "John", "Mary", "Jane", "Usher", "Andrew", "Grace", "Aston", "Sheila" });
    var result = Array.Find(people, p => p.FirstName == searchString);
    if (result != null)
    {
        return result.ToString();
    }
    else
    {
        return $"{searchString} could not be found";
    }
}

We can verify that our fix works by checking whether the FixedTeachers() method returns a string and is not null: 

var listObj = new ExceptionMethods();

var searchPerson = listObj.FixedTeachers("Steve");

Assert.IsNotNull(searchPerson);
Assert.IsInstanceOfType(searchPerson, typeof(string));

Enumerating Arrays Elements With Reference Types

In some cases, we may encounter the NullReferenceException when attempting to process array elements when some of them are null. Let’s try to simulate this scenario with an example:

public string[] CapitalizeNames() 
{
    var names = new string[] { "John", "Mary", null, null, "Andrew", "Grace", "Aston", "Sheila" };

    for (int i = 0; i < names.Length; i++)
    {
        names[i] = names[i].ToUpper();
    }

    return names;
}

The CapitalizeNames() method converts the string elements in the names array to upper-case using the inbuilt string.ToUpper() method. However, since some of the elements are null, attempting to convert them to uppercase throws the NullReferenceException, which we can verify:

[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void GivenAnArray_WhenSomeElementsNull_VerifyThrowsNullReferenceException()
{
    var arrayObj = new ExceptionMethods();
    
    var capitalizedNames = arrayObj.CapitalizeNames();
    
    Assert.IsNull(capitalizedNames);
}

To resolve this error, we need to check for null values before calling the string.ToUpper() method. We can make use of the string.Length property or the string.IsNullOrEmpty() method to check if a given array element is null:

public string[] FixedCapitalizeNames()
{
    var names = new string[] { "John", "Mary", null, null, "Andrew", "Grace", "Aston", "Sheila" };

    for(int i = 0; i < names.Length; i++)
    {
        if (!string.IsNullOrEmpty(names[i])) 
        {
            names[i] = names[i].ToUpper();
        }
    }

    return names;
}

We can then proceed to check that our method works without any issues with this test:

var arrayObj = new ExceptionMethods();

var capitalizedNames = arrayObj.FixedCapitalizeNames();

Assert.IsNotNull(capitalizedNames);
CollectionAssert.Contains(capitalizedNames, "JOHN");

Passing Null Arguments to Methods

We can trigger the exception when we attempt to pass null arguments to methods. Just like other reference types, we can pass null objects across different methods. Some methods may validate the arguments they receive and end up throwing the System.ArgumentNullException when they detect null arguments. On the other hand, when the methods fail to check for null arguments they end up throwing the NullReferenceException instead. 

Let’s simulate this scenario with an example:

Wanna join Code Maze Team, help us produce more awesome .NET/C# content and get paid? >> JOIN US! <<

public List<string> PopulateList(List<string> peopleNames) 
{
    var names = new string[] { "John", "Mary", "Andrew", "Grace", "Aston", "Sheila" };

    foreach (var person in names) 
    {
        peopleNames.Add(person);
    }
        
    return peopleNames;
}

The PopulateList() method takes a peopleNames list object and appends the array of elements in the names array to that list before returning it back to the user. Here, we are assuming that the PeopleList() method is going to always receive arguments that are not null. 

Let’s test what happens when we try to pass a null List<string> object to the PopulateList() method: 

[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void GivenAMethod_WhenNullArgumentsPassed_VerifyThrowsNullReferenceException()
{
    var namesObj = new ExceptionMethods();

    List<string> currentPeople = null;
    var peopleList = namesObj.PopulateList(currentPeople);

    Assert.IsNull(peopleList);
}

Assuming that a method is going to always get non-null arguments is why we get the exception in this case. Therefore, just like in our other examples, we need to check for null arguments before appending items to the list. Besides that, we can use different exception handling techniques to deal with the exception as we invoke the PopulateList() method as we can see in this example:

public List<string> FixedPopulateList(List<string> peopleNames)
{
    var names = new string[] { "John", "Mary", "Andrew", "Grace", "Aston", "Sheila" };

    if (peopleNames == null) 
    {
        peopleNames = new List<string>();
    }

    foreach (var person in names) 
    {
        peopleNames.Add(person);
    }
        
    return peopleNames;
}

Here, we see that we check whether the list is null before attempting to append a list of names to it, which helps us avoid the NullReferenceException error. We can proceed to verify that the FixedPopulateList() method returns a List<string> object that is not null:

var namesObj = new ExceptionMethods();

List<string> currentPeople = null;
var peopleList = namesObj.FixedPopulateList(currentPeople);

Assert.IsNotNull(peopleList);
CollectionAssert.Contains(peopleList, "Mary");

How to Debug the NullReferenceException in C#?

There are different ways that we can use to find the source of a NullReferenceException in C#.

Generally, when using Visual Studio, we can use different debugging strategies to anticipate and fix syntax and logical errors. Although the compiler may show some warnings and show where the error is, one of the most common techniques that we can use is setting breakpoints strategically to anticipate any errors. 

We can also use unit tests to verify that such exceptions don’t occur before shipping out code to production. Finally, we can inspect variables and all their references to ensure that they have non-null references to avoid getting the NullReferenceException. 

How to Avoid Getting the NullReferenceExpection in C#?

To start with, we can avoid getting the NullReferenceException in C# by always checking for null values and ignoring them or replacing them with default values as we have done in our last example:

if (result != null)
{
    return result.ToString();
}
else
{
    return $"{searchString} could not be found";
}

Besides checking for null values, we can use exception handling techniques to ensure that our code does not trigger such exceptions during execution. Simple try-catch blocks or the use of custom exceptions can come in handy when debugging applications as they can help us isolate problematic code. 

On top of that, C# supports nullable types, which we can use in these situations. For example, we can define a nullable integer as int? number = null; which is the shortened version of  Nullable<int> number = null;. In this case, the question mark shows that the variable can hold  null in the variable number. We can then proceed to check whether number is null with if statements such as if (number.HasValue) or if (number == null)

To avoid getting the NullReferenceException at the project level, we can take advantage of nullable contexts. These contexts help us control how the compiler interprets reference types. To protect the project against the NullReferenceExecption, we can choose to enable it in the project’s csproj file as <Nullable>enable<Nullable>. To understand all the different options that we can use with nullable contexts, please refer to this article. 

Using the Null-Coalescing Operator

Finally, we can use the null-coalescing operator ?? as a way to avoid getting the NullReferenceException in C#. The null-coalescing operator is a binary operator that is used to assign a default value to a variable. The left-hand operand must be of a reference type, and the right-hand operand must be of the same type or convertible to the type of the left-hand operand. If the left-hand operand is not null, it is evaluated and returned; otherwise, the right-hand operand is evaluated and becomes the result of the operation.

Here is an example of how we can use the null-coalescing operator in C#:

int? num = null;
int result = num ?? 0; 

First, we assign the value null to the num variable. Next, we proceed to check if the value of num is not null, then result will be equal to num. On the other hand, if num is null, then result will be equal to 0. By using this operator, we can assign a default value to a variable if the variable is null, which helps us avoid a NullReferenceException.

These are useful ways to prevent errors in our code and make our code more robust.

Conclusion

In this article, we have learned the causes of NullReferenceException, debugging techniques, and how to avoid getting it as we code. We are eager to keep learning together, so, in case you think of some scenarios that can cause the NullReferenceExecption that are not in this article, please free to comment below and we’ll add them as soon as possible. 

Wanna join Code Maze Team, help us produce more awesome .NET/C# content and get paid? >> JOIN US! <<

Исключение NullReference — Visual Basic

NullReference Exception для Visual Basic ничем не отличается от NullReference Exception в С#. В конце концов, они оба сообщают об одном и том же исключении, определенном в.NET Framework, которое они оба используют. Причины, уникальные для Visual Basic, встречаются редко (возможно, только один).

В этом ответе будут использоваться термины, синтаксис и контекст Visual Basic. Используемые примеры исходят из большого количества прошлых вопросов о переполнении стека. Это делается для того, чтобы максимизировать актуальность, используя ситуации, часто встречающиеся в сообщениях. Немного больше объяснений также предоставляется тем, кому это может понадобиться. Пример, похожий на ваш, очень вероятно указан здесь.

Заметка:

  1. Это основано на концепции: в вашем проекте нет кода для вставки. Он призван помочь вам понять, что вызывает NullReferenceException (NRE), как его найти, как его исправить и как его избежать. NRE может быть вызвано многими способами, поэтому это вряд ли будет вашей единственной встречей.
  2. Примеры (из Qaru posts) не всегда показывают лучший способ сделать что-то в первую очередь.
  3. Как правило, используется самое простое средство.

Основное значение

Сообщение «Объект, не установленный для экземпляра объекта» означает, что вы пытаетесь использовать объект, который не был инициализирован. Это сводится к одному из следующих:

  • Ваш код объявлял объектную переменную, но не инициализировал ее (создайте экземпляр или «создайте экземпляр»)
  • Что-то, что предполагал ваш код, инициализирует объект, не
  • Возможно, другой код преждевременно аннулировал объект, который все еще используется

Поиск причины

Поскольку проблема — это ссылка на объект, которая является » Nothing, ответ заключается в том, чтобы изучить их, чтобы узнать, какой из них. Затем определите, почему он не инициализирован. Держите мышь над различными переменными, и Visual Studio (VS) покажет их значения — виновником будет Nothing.

Изображение 943

Вы также должны удалить блоки Try/Catch из соответствующего кода, особенно те, где в блоке Catch ничего нет. Это приведет к сбою вашего кода при попытке использовать объект Nothing. Это то, что вы хотите, потому что оно идентифицирует точное местоположение проблемы и позволяет идентифицировать вызывающий объект.

MsgBox в Catch, который отображает Error while..., мало поможет. Этот метод также приводит к очень плохим вопросам, потому что вы не можете описать фактическое исключение, объект или даже строку кода, где это происходит.

Вы также можете использовать Locals Window (Debug → Windows → Locals) для проверки ваших объектов.

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

Смотрите также:

  • Контрольные точки
  • MSDN: Как использовать блок Try/Catch для выхвата исключений
  • MSDN: рекомендации по исключениям

Примеры и средства защиты

Объекты класса/Создание экземпляра

Dim reg As CashRegister
...
TextBox1.Text = reg.Amount         ' NRE

Проблема заключается в том, что Dim не создает объект CashRegister; он объявляет только переменную с именем reg этого типа. Объявление переменной объекта и создание экземпляра — это две разные вещи.

средство

Оператор New может часто использоваться для создания экземпляра при его объявлении:

Dim reg As New CashRegister        ' [New] creates instance, invokes the constructor

' Longer, more explicit form:
Dim reg As CashRegister = New CashRegister

Когда целесообразно создать экземпляр позже:

Private reg As CashRegister         ' Declare
  ...
reg = New CashRegister()            ' Create instance

Примечание. Не используйте Dim снова в процедуре, включая конструктор (Sub New):

Private reg As CashRegister
'...

Public Sub New()
   '...
   Dim reg As New CashRegister
End Sub

Это создаст локальную переменную reg, которая существует только в этом контексте (sub). Переменная reg с уровнем модуля Scope который вы будете использовать везде, останется Nothing.

Отсутствие оператора New является причиной № 1 NullReference Exceptions рассмотренной в рассмотренных вопросах.

Visual Basic пытается многократно очистить процесс, используя New: Использование New Operator создает новый объект и вызывает Sub New — конструктор — где ваш объект может выполнять любую другую инициализацию.

Чтобы быть ясным, Dim (или Private) объявляет только переменную и ее Type. Область действия переменной — независимо от того, существует ли она для всего модуля/класса или является локальной процедурой — определяется тем, где она объявлена. Private | Friend | Public Private | Friend | Public определяет уровень доступа, а не Scope.

Для получения дополнительной информации см.

  • Новый оператор
  • Область применения Visual Basic
  • Уровни доступа в Visual Basic
  • Типы значений и ссылочные типы

Массивы

Массивы также должны быть созданы:

Private arr as String()

Этот массив только объявлен, а не создан. Существует несколько способов инициализации массива:

Private arr as String() = New String(10){}
' or
Private arr() As String = New String(10){}

' For a local array (in a procedure) and using 'Option Infer':
Dim arr = New String(10) {}

Примечание. Начиная с VS 2010, при инициализации локального массива с использованием литерала и Option Infer, Option Infer As <Type> и New являются необязательными:

Dim myDbl As Double() = {1.5, 2, 9.9, 18, 3.14}
Dim myDbl = New Double() {1.5, 2, 9.9, 18, 3.14}
Dim myDbl() = {1.5, 2, 9.9, 18, 3.14}

Тип данных и размер массива выводятся из назначаемых данных. Объявление уровня класса/модуля по-прежнему требует использования As <Type> с Option Strict:

Private myDoubles As Double() = {1.5, 2, 9.9, 18, 3.14}

Пример: массив объектов класса

Dim arrFoo(5) As Foo

For i As Integer = 0 To arrFoo.Count - 1
   arrFoo(i).Bar = i * 10       ' Exception
Next

Массив создан, но объекты Foo в нем нет.

средство

For i As Integer = 0 To arrFoo.Count - 1
    arrFoo(i) = New Foo()         ' Create Foo instance
    arrFoo(i).Bar = i * 10
Next

Использование List(Of T) затруднит наличие элемента без действительного объекта:

Dim FooList As New List(Of Foo)     ' List created, but it is empty
Dim f As Foo                        ' Temporary variable for the loop

For i As Integer = 0 To 5
    f = New Foo()                    ' Foo instance created
    f.Bar =  i * 10
    FooList.Add(f)                   ' Foo object added to list
Next

Для получения дополнительной информации см.

  • Вывод опциона Infer
  • Область применения Visual Basic
  • Массивы в Visual Basic

Списки и коллекции

Коллекции.NET (из которых есть много разновидностей — Списки, Словарь и т.д.) Также должны быть созданы или созданы.

Private myList As List(Of String)
..
myList.Add("ziggy")           ' NullReference

Вы получаете одно и то же исключение по той же причине — myList был объявлен, но экземпляр не создан. Средство защиты одно и то же:

myList = New List(Of String)

' Or create an instance when declared:
Private myList As New List(Of String)

Общий надзор — это класс, в котором используется коллекция Type:

Public Class Foo
    Private barList As List(Of Bar)

    Friend Function BarCount As Integer
        Return barList.Count
    End Function

    Friend Sub AddItem(newBar As Bar)
        If barList.Contains(newBar) = False Then
            barList.Add(newBar)
        End If
    End Function

Любая процедура приведет к NRE, потому что barList объявляется, а не создается. Создание экземпляра Foo также не создаст экземпляр внутреннего barList. Возможно, это было намерение сделать это в конструкторе:

Public Sub New         ' Constructor
    ' Stuff to do when a new Foo is created...
    barList = New List(Of Bar)
End Sub

Как и раньше, это неверно:

Public Sub New()
    ' Creates another barList local to this procedure
     Dim barList As New List(Of Bar)
End Sub

Дополнительные сведения см. В разделе List(Of T).


Объекты поставщика данных

Работа с базами данных предоставляет много возможностей для NullReference, потому что одновременно может быть много объектов (Command, Connection, Transaction, Dataset, DataTable, DataRows….). Примечание. Неважно, какой поставщик данных вы используете — MySQL, SQL Server, OleDB и т.д. — концепции одинаковы.

Пример 1

Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim MaxRows As Integer

con.Open()
Dim sql = "SELECT * FROM tblfoobar_List"
da = New OleDbDataAdapter(sql, con)
da.Fill(ds, "foobar")
con.Close()

MaxRows = ds.Tables("foobar").Rows.Count      ' Error

Как и ранее, объект ds Dataset был объявлен, но экземпляр никогда не создавался. DataAdapter заполнит существующий DataSet, а не создаст его. В этом случае, поскольку ds является локальной переменной, IDE предупреждает вас, что это может произойти:

Изображение 944

Когда объявляется как переменная уровня модуля/класса, как представляется, в случае с con, компилятор не может знать, был ли объект создан процедурой восходящего потока. Не игнорируйте предупреждения.

средство

Dim ds As New DataSet

Пример 2.

ds = New DataSet
da = New OleDBDataAdapter(sql, con)
da.Fill(ds, "Employees")

txtID.Text = ds.Tables("Employee").Rows(0).Item(1)
txtID.Name = ds.Tables("Employee").Rows(0).Item(2)

Опечатка является проблемой здесь: Employees против Employee. Не было создано DataTable именем «Employee», поэтому результаты NullReferenceException пытаются получить к нему доступ. Другая потенциальная проблема заключается в предположении, что будут Items которые могут быть не такими, когда SQL включает предложение WHERE.

средство

Поскольку это использует одну таблицу, использование Tables(0) позволит избежать орфографических ошибок. Изучение Rows.Count также может помочь:

If ds.Tables(0).Rows.Count > 0 Then
    txtID.Text = ds.Tables(0).Rows(0).Item(1)
    txtID.Name = ds.Tables(0).Rows(0).Item(2)
End If

Fill — это функция, возвращающая количество затронутых Rows которые также могут быть протестированы:

If da.Fill(ds, "Employees") > 0 Then...

Пример 3.

Dim da As New OleDb.OleDbDataAdapter("SELECT TICKET.TICKET_NO,
        TICKET.CUSTOMER_ID, ... FROM TICKET_RESERVATION AS TICKET INNER JOIN
        FLIGHT_DETAILS AS FLIGHT ... WHERE [TICKET.TICKET_NO]= ...", con)
Dim ds As New DataSet
da.Fill(ds)

If ds.Tables("TICKET_RESERVATION").Rows.Count > 0 Then

DataAdapter обеспечит TableNames, как показано в предыдущем примере, но не разобрать имена из SQL или базы данных таблицы. В результате ds.Tables("TICKET_RESERVATION") ссылается на несуществующую таблицу.

Средство устранения одинаково, ссылайтесь на таблицу по индексу:

If ds.Tables(0).Rows.Count > 0 Then

См. Также Класс DataTable.


Пути объектов/Вложенные

If myFoo.Bar.Items IsNot Nothing Then
   ...

Код только тестирует Items то время как myFoo и Bar также могут быть Nothing. Средство состоит в том, чтобы проверить всю цепочку или путь объектов по одному:

If (myFoo IsNot Nothing) AndAlso
    (myFoo.Bar IsNot Nothing) AndAlso
    (myFoo.Bar.Items IsNot Nothing) Then
    ....

AndAlso важно. Последующие тесты не будут выполняться после первого состояния False. Это позволяет коду безопасно «сверлить» на объект один «уровень» за раз, оценивая myFoo.Bar только после (и если) myFoo определяется как действительный. Цепочки объектов или пути могут быть довольно длинными при кодировании сложных объектов:

myBase.myNodes(3).Layer.SubLayer.Foo.Files.Add("somefilename")

Невозможно ссылаться на «нисходящий поток» null объекта. Это также относится к элементам управления:

myWebBrowser.Document.GetElementById("formfld1").InnerText = "some value"

Здесь myWebBrowser или Document может быть Nothing или элемент formfld1 может не существовать.


Элементы управления пользовательским интерфейсом

Dim cmd5 As New SqlCommand("select Cartons, Pieces, Foobar " _
     & "FROM Invoice where invoice_no = '" & _
     Me.ComboBox5.SelectedItem.ToString.Trim & "' And category = '" & _
     Me.ListBox1.SelectedItem.ToString.Trim & "' And item_name = '" & _
     Me.ComboBox2.SelectedValue.ToString.Trim & "' And expiry_date = '" & _
     Me.expiry.Text & "'", con)

Помимо всего прочего, этот код не предполагает, что пользователь может не выбрать что-либо в одном или нескольких элементах управления пользовательским интерфейсом. ListBox1.SelectedItem вполне может быть Nothing, поэтому ListBox1.SelectedItem.ToString приведет к NRE.

средство

Проверяйте данные перед их использованием (также используйте параметры Option Strict и SQL):

Dim expiry As DateTime         ' for text date validation
If (ComboBox5.SelectedItems.Count > 0) AndAlso
    (ListBox1.SelectedItems.Count > 0) AndAlso
    (ComboBox2.SelectedItems.Count > 0) AndAlso
    (DateTime.TryParse(expiry.Text, expiry) Then

    '... do stuff
Else
    MessageBox.Show(...error message...)
End If

Кроме того, вы можете использовать (ComboBox5.SelectedItem IsNot Nothing) AndAlso...


Визуальные базовые формы

Public Class Form1

    Private NameBoxes = New TextBox(5) {Controls("TextBox1"), _
                   Controls("TextBox2"), Controls("TextBox3"), _
                   Controls("TextBox4"), Controls("TextBox5"), _
                   Controls("TextBox6")}

    ' same thing in a different format:
    Private boxList As New List(Of TextBox) From {TextBox1, TextBox2, TextBox3 ...}

    ' Immediate NRE:
    Private somevar As String = Me.Controls("TextBox1").Text

Это довольно распространенный способ получить NRE. В С#, в зависимости от того, как он кодируется, среда IDE сообщит, что Controls не существуют в текущем контексте, или «не может ссылаться на нестатический член». Итак, в какой-то степени это ситуация только с VB. Это также сложно, потому что это может привести к отказу каскада.

Таким образом, массивы и коллекции не могут быть инициализированы. Этот код инициализации будет выполняться до того, как конструктор создаст Form или Controls. В результате:

  • Списки и коллекция будут просто пустыми
  • Массив будет содержать пять элементов Nothing
  • Назначение somevar приведет к немедленному NRE, потому что Nothing не имеет свойства .Text

После этого ссылки на элементы массива приведут к NRE. Если вы делаете это в Form_Load, из-за нечетной ошибки, среда IDE может не сообщать об исключении, когда это происходит. Исключение появится позже, когда ваш код попытается использовать массив. Это «молчаливое исключение» подробно описано в этом сообщении. Для наших целей ключ заключается в том, что, когда что-то катастрофическое происходит при создании формы (Sub New или Form Load event), исключения могут не сообщаться, код выходит из процедуры и просто отображает форму.

Поскольку никакой другой код в вашем событии Sub New или Form Load будет запущен после NRE, многие другие вещи могут быть оставлены неинициализированными.

Sub Form_Load(..._
   '...
   Dim name As String = NameBoxes(2).Text        ' NRE
   ' ...
   ' More code (which will likely not be executed)
   ' ...
End Sub

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

Public Class Form1

    Private myFiles() As String = Me.OpenFileDialog1.FileName & ...
    Private dbcon As String = OpenFileDialog1.FileName & ";Jet Oledb..."
    Private studentName As String = TextBox13.Text

Частичное средство

Любопытно, что VB не дает предупреждение, а средство объявить контейнеры на уровне формы, но инициализировать их в обработчик события загрузки формы, когда элементы управления существует. Это можно сделать в Sub New если ваш код после вызова InitializeComponent:

' Module level declaration
Private NameBoxes as TextBox()
Private studentName As String

' Form Load, Form Shown or Sub New:
'
' Using the OP approach (illegal using OPTION STRICT)
NameBoxes = New TextBox() {Me.Controls("TextBox1"), Me.Controls("TestBox2"), ...)
studentName = TextBox32.Text           ' For simple control references

Код массива, возможно, еще не вышел из леса. Любые элементы управления, которые находятся в контроле контейнера (например, GroupBox или Panel), не будут найдены в Me.Controls; они будут находиться в коллекции Controls этой Panel или GroupBox. Также не будет возвращен элемент управления, если контрольное имя будет написано неправильно ("TeStBox2"). В таких случаях в этих элементах массива Nothing не будет сохранено, и при попытке ссылаться на него будет возникать NRE.

Теперь вам будет легко найти, что вы знаете, что ищете: Изображение 943

«Button2» находится на Panel

средство

Вместо косвенных ссылок по имени с использованием коллекции Controls формы используйте ссылку на управление:

' Declaration
Private NameBoxes As TextBox()

' Initialization -  simple and easy to read, hard to botch:
NameBoxes = New TextBox() {TextBox1, TextBox2, ...)

' Initialize a List
NamesList = New List(Of TextBox)({TextBox1, TextBox2, TextBox3...})
' or
NamesList = New List(Of TextBox)
NamesList.AddRange({TextBox1, TextBox2, TextBox3...})

Функция, возвращающая ничего

Private bars As New List(Of Bars)        ' Declared and created

Public Function BarList() As List(Of Bars)
    bars.Clear
    If someCondition Then
        For n As Integer = 0 to someValue
            bars.Add(GetBar(n))
        Next n
    Else
        Exit Function
    End If

    Return bars
End Function

Это случай, когда IDE предупредит вас, что «не все пути возвращают значение и может возникнуть NullReferenceException «. Вы можете подавить предупреждение, заменив Exit Function на Return Nothing, но это не решит проблему. Все, что пытается использовать возврат, когда someCondition = False приведет к NRE:

bList = myFoo.BarList()
For Each b As Bar in bList      ' EXCEPTION
      ...

средство

Замените Exit Function в функции с помощью функции Return bList. Возврат пустого List не совпадает с возвратом Nothing. Если есть вероятность того, что возвращаемый объект может быть Nothing, перед его использованием проверьте:

 bList = myFoo.BarList()
 If bList IsNot Nothing Then...

Плохо реализовано Try/Catch

Плохо реализованный Try/Catch может скрыть, где проблема, и привести к новым:

Dim dr As SqlDataReader
Try
    Dim lnk As LinkButton = TryCast(sender, LinkButton)
    Dim gr As GridViewRow = DirectCast(lnk.NamingContainer, GridViewRow)
    Dim eid As String = GridView1.DataKeys(gr.RowIndex).Value.ToString()
    ViewState("username") = eid
    sqlQry = "select FirstName, Surname, DepartmentName, ExtensionName, jobTitle,
             Pager, mailaddress, from employees1 where username='" & eid & "'"
    If connection.State <> ConnectionState.Open Then
        connection.Open()
    End If
    command = New SqlCommand(sqlQry, connection)

    'More code fooing and barring

    dr = command.ExecuteReader()
    If dr.Read() Then
        lblFirstName.Text = Convert.ToString(dr("FirstName"))
        ...
    End If
    mpe.Show()
Catch

Finally
    command.Dispose()
    dr.Close()             ' <-- NRE
    connection.Close()
End Try

Это случай, когда объект создается не так, как ожидалось, но также демонстрирует полезность счетчика пустого Catch.

В SQL имеется дополнительная запятая (после «mailaddress»), которая приводит к исключению в .ExecuteReader. После того, как Catch ничего не делает, в Finally пытается выполнить очистку, но поскольку вы не можете Close нулевой объект DataReader, получается новое значение NullReferenceException.

Пустой блок Catch — площадка для дьявола. Это О.П. был озадачен, почему он был получать NRE в Finally блоке. В других ситуациях, пустой Catch может привести к чему-то еще намного дальше вниз по течению, идущему в сторону haywire, и заставить вас тратить время на то, чтобы не ошибиться в неполадке проблемы. («Тихая исключение», описанное выше, дает такое же развлекательное значение.)

средство

Не используйте пустые блоки Try/Catch — пусть сбой кода, чтобы вы могли: a) определить причину b) определить местоположение и c) применить надлежащее средство. Блоки Try/Catch не предназначены для того, чтобы скрывать исключения от лица, обладающего уникальной квалификацией для их исправления — разработчика.


DBNull — это не то же самое, что и ничего

For Each row As DataGridViewRow In dgvPlanning.Rows
    If Not IsDBNull(row.Cells(0).Value) Then
        ...

Функция IsDBNull используется для тестирования, если значение равно System.DBNull: From MSDN:

Значение System.DBNull указывает, что объект представляет отсутствующие или несуществующие данные. DBNull — это не то же самое, что и Nothing, что указывает на то, что переменная еще не была инициализирована.

средство

If row.Cells(0) IsNot Nothing Then ...

Как и прежде, вы можете протестировать Nothing, а затем для определенного значения:

If (row.Cells(0) IsNot Nothing) AndAlso (IsDBNull(row.Cells(0).Value) = False) Then

Пример 2.

Dim getFoo = (From f In dbContext.FooBars
               Where f.something = something
               Select f).FirstOrDefault

If Not IsDBNull(getFoo) Then
    If IsDBNull(getFoo.user_id) Then
        txtFirst.Text = getFoo.first_name
    Else
       ...

FirstOrDefault возвращает первый элемент или значение по умолчанию, которое не является Nothing для ссылочных типов и никогда DBNull:

If getFoo IsNot Nothing Then...

управления

Dim chk As CheckBox

chk = CType(Me.Controls(chkName), CheckBox)
If chk.Checked Then
    Return chk
End If

Если CheckBox с chkName не может быть найден (или существует в GroupBox), тогда chk будет Nothing и будет пытаться ссылаться на любое свойство, приведет к исключению.

средство

If (chk IsNot Nothing) AndAlso (chk.Checked) Then ...

DataGridView

Периодически DGV имеет несколько причуд:

dgvBooks.DataSource = loan.Books
dgvBooks.Columns("ISBN").Visible = True       ' NullReferenceException
dgvBooks.Columns("Title").DefaultCellStyle.Format = "C"
dgvBooks.Columns("Author").DefaultCellStyle.Format = "C"
dgvBooks.Columns("Price").DefaultCellStyle.Format = "C"

Если dgvBooks имеет AutoGenerateColumns = True, он будет создавать столбцы, но он их не AutoGenerateColumns = True, поэтому приведенный выше код завершится неудачей, когда он ссылается на них по имени.

средство

Назовите столбцы вручную или ссылку по индексу:

dgvBooks.Columns(0).Visible = True

Пример 2 — Остерегайтесь NewRow

xlWorkSheet = xlWorkBook.Sheets("sheet1")

For i = 0 To myDGV.RowCount - 1
    For j = 0 To myDGV.ColumnCount - 1
        For k As Integer = 1 To myDGV.Columns.Count
            xlWorkSheet.Cells(1, k) = myDGV.Columns(k - 1).HeaderText
            xlWorkSheet.Cells(i + 2, j + 1) = myDGV(j, i).Value.ToString()
        Next
    Next
Next

Когда ваш DataGridView имеет AllowUserToAddRows как True (по умолчанию), Cells в пустой/новой строке внизу будут содержать Nothing. Большинство попыток использования содержимого (например, ToString) приведет к NRE.

средство

Используйте цикл For/Each и проверьте свойство IsNewRow чтобы определить, является ли это последней строкой. Это работает, верно ли AllowUserToAddRows или нет:

For Each r As DataGridViewRow in myDGV.Rows
    If r.IsNewRow = False Then
         ' ok to use this row

Если вы используете цикл For n, измените количество строк или используйте Exit For когда IsNewRow истинно.


My.Settings(StringCollection)

При определенных обстоятельствах попытка использовать элемент из My.Settings который является StringCollection может привести к NullReference при первом использовании. Решение одно и то же, но не так очевидно. Рассматривать:

My.Settings.FooBars.Add("ziggy")         ' foobars is a string collection

Поскольку VB управляет настройками для вас, разумно ожидать, что он инициализирует коллекцию. Он будет, но только если вы ранее добавили начальную запись в коллекцию (в редакторе настроек). Поскольку коллекция (по-видимому) инициализируется при добавлении элемента, она остается » Nothing когда в редакторе настроек не добавляются элементы.

средство

Инициализировать коллекцию настроек в форме Load event handler, если/когда необходимо:

If My.Settings.FooBars Is Nothing Then
    My.Settings.FooBars = New System.Collections.Specialized.StringCollection
End If

Как правило, Settings должна быть инициализирована только при первом запуске приложения. Альтернативным средством является добавление начального значения в вашу коллекцию в Project → Settings | FooBars, сохраните проект, а затем удалите поддельное значение.


Ключевые моменты

Вероятно, вы забыли оператора » New.

или

То, что вы предположили, безупречно выполнило бы, чтобы вернуть инициализированный объект в ваш код, не сделал этого.

Не игнорируйте предупреждения компилятора (когда-либо) и используйте параметр Option Strict On (всегда).


Исключение NullReference MSDN

The “Object reference not set to an instance of an object” is a very famous error in C# that appears when you get a NullReferenceException. This occurs when you try to access a property or method of an object that points to a null value. They can be fixed using Null conditional operators and handled using try-catch blocks.

In this post, we will learn more about the error and the ways to fix it.

What is “NullReferenceException: Object reference not set to an instance of an object” error?

As mentioned earlier, the NullReferenceException indicates that your code is trying to work with an object that has a null value as its reference. This means that the reference object has not been initialized.

This is a runtime exception that can be caught using a try-catch block.

Example code

try
{
    string a = null;
    a.ToString();
}
catch (NullReferenceException e)
{
    //Code to do something with e
}

How to fix this error?

You can fix this error by using the following methods:

  • Using Null conditional operators
  • Using the Null Coalescing operator
  • Using nullable datatypes in C#   

1) Using Null conditional operators

This method is easier than using an if-else condition to check whether the variable value is null. Look at this example,

int? length = customers?.Length; // this will return null if customers is null, instead of throwing the exception

2) Using the Null Coalescing operator

This operator looks like “??” and provides a default value to variables that have a null value. It is compatible with all nullable datatypes.

Example

int length = customers?.Length ?? 0; // 0 is provided by default if customers is null      

3) Using nullable datatypes in C#   

All reference types in C# can have a null value. But some data types such as int and Boolean cannot take null values unless they are explicitly defined. This is done by using Nullable data types.

For example,

static int Add(string roll_numbers)
{
return roll_numbers.Split(","); // This code might throw a NullReferenceException as roll_numbers variable can be null 
}

Correct code

static int Add(string? roll_numbers) // As roll_numbers argument can now be null, the NullReferenceException can be avoided  
{
return roll_numbers.Split(",");  
}

The best way to avoid the «NullReferenceException: Object reference not set to an instance of an object” error is to check the values of all variables while coding. You can also use a simple if-else statement to check for null values, such as if (numbers!=null) to avoid this exception.

У меня есть некоторый код, и когда он выполняется, он бросает NullReferenceException , говоря:

В экземпляре объекта не задана ссылка на объект.

Что это значит, и что я могу сделать, чтобы исправить эту ошибку?

В чем причина?

Нижняя граница

Вы пытаетесь использовать то, что есть null (или Nothing в VB.NET). Это означает, что вы либо установили его null , либо никогда вообще ничего не устанавливали.

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

null может иметь разные значения:

    1. Переменные объекта, которые неинициализированы и, следовательно, ни на что не указывают. В этом случае, если вы обращаетесь к свойствам или методам таких объектов, это вызывает NullReferenceException .
    1. Разработчик намеренноиспользует, null чтобы указать, что значимых доступных значений нет. Обратите внимание, что C # имеет концепцию типов данных, допускающих значение NULL для переменных (например, таблицы базы данных могут иметь поля NULL), которые можно null указывать, чтобы указать, что в них нет сохраненного значения, например, int? a = null; когда знак вопроса указывает, что ему разрешено хранить нуль в переменная a . Вы можете проверить это либо с, if (a.HasValue) <.>либо с if (a==null) <.>. Обнуляемые переменные, как в a этом примере, позволяют получить доступ к значению a.Value явно или просто как обычно a .
      Обратите внимание , что доступ к нему через a.Value Выдает InvalidOperationException вместо NullReferenceException если a IS null — вы должны выполнить проверку заранее, т. е. если у вас есть другая переменная, которая может иметь значение nullable, int b; вы должны выполнять назначения, такие как if (a.HasValue) или более короткие if (a != null) .

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

Более конкретно

runtime Метании NullReferenceException всегда означает то же самое: вы пытаетесь использовать ссылку, и ссылка не инициализируется (или он был когда — то инициализируется, но не больше не инициализирован).

Это означает, что ссылка есть null , и вы не можете получить доступ к членам (таким как методы) через null ссылку. Самый простой случай:

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

Отладка

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

Если вы хотите узнать, где находится ссылка или нет, щелкните правой кнопкой мыши ее имя и выберите «Найти все ссылки». Затем вы можете установить точку останова в каждом найденном месте и запустить вашу программу с подключенным отладчиком. Каждый раз, когда отладчик прерывает работу на такой точке останова, вам нужно определить, ожидаете ли вы, что ссылка не равна нулю, проверить переменную и убедиться, что она указывает на экземпляр, когда вы этого ожидаете.

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

Примеры

Некоторые распространенные сценарии, в которых может быть выдано исключение:

общий

Если ref1 или ref2 или ref3 равны нулю, вы получите NullReferenceException . Если вы хотите решить проблему, то выясните, какая из них равна нулю, переписав выражение в его более простой эквивалент:

В частности, в HttpContext.Current.User.Identity.Name , то HttpContext.Current может быть пустым, или User свойство может быть пустым, или Identity свойство может быть пустым.

непрямой

Если вы хотите избежать нулевой ссылки дочернего (Person), вы можете инициализировать ее в конструкторе родительского (Book) объекта.

Инициализаторы вложенных объектов

То же самое относится и к инициализаторам вложенных объектов:

Это переводится как

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

Инициализаторы вложенных коллекций

Вложенная коллекция Initializers ведет себя так же:

Это переводится как

new Person Только создает экземпляр Person , но Books коллекция до сих пор null . Initializer Синтаксис коллекции не создает коллекцию p1.Books , он только переводит в p1.Books.Add(. ) операторы.

массив

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

Зубчатые массивы

Коллекция / Список / Словарь

Переменная диапазона (косвенная / отложенная)

Мероприятия

Эту проблему можно решить, следуя соглашению о добавлении префикса к полям с подчеркиванием:

Жизненный цикл страницы ASP.NET:

Значения сеанса ASP.NET

ASP.NET MVC модели пустого представления

Если исключение возникает при обращении к свойству в @Model in ASP.NET MVC View , вам нужно понимать, что Model get задается в вашем методе действия, когда вы return просматриваете. Когда вы возвращаете пустую модель (или свойство модели) из вашего контроллера, исключение возникает, когда представления обращаются к нему:

Порядок и события создания элемента управления WPF

WPF элементы управления создаются во время вызова InitializeComponent в порядке их появления в визуальном дереве. A NullReferenceException будет вызываться в случае ранее созданных элементов управления с обработчиками событий и т. Д., Которые срабатывают при InitializeComponent обращении к недавно созданным элементам управления.

Здесь comboBox1 создано раньше label1 . Если comboBox1_SelectionChanged попытка сослаться на `label1, он еще не был создан.

Изменение порядка объявлений в XAML (т. Е. Перечисление label1 ранее comboBox1 , игнорируя вопросы философии дизайна, по крайней мере, решило бы NullReferenceException здесь.

В ролях с as

Это не бросает, InvalidCastException но возвращает, null когда приведение не удается (и когда someObject само по себе является нулевым). Так что знайте об этом.

LINQ FirstOrDefault() и SingleOrDefault()

Простые версии First() и Single() исключения, когда нет ничего. Версии «OrDefault» в этом случае возвращают ноль. Так что знайте об этом.

для каждого

foreach бросает при попытке перебрать нулевую коллекцию. Обычно вызвано неожиданным null результатом от методов, которые возвращают коллекции.

Более реалистичный пример — выберите узлы из XML-документа. Выдает, если узлы не найдены, но начальная отладка показывает, что все свойства действительны:

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

Явно проверяйте null и игнорируйте нулевые значения.

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

Явно проверьте null и укажите значение по умолчанию.

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

Явно проверяйте null из вызовов методов и создавайте пользовательское исключение.

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

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

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

Хотя эта проверка не закончится в вашей сборке релиза , из-за чего она NullReferenceException снова выдаст команду во book == null время выполнения в режиме релиза.

Используйте GetValueOrDefault() для nullable типов значений, чтобы обеспечить значение по умолчанию, когда они есть null .

Используйте оператор объединения нулей: ?? [C #] или If() [VB].

Сокращение для предоставления значения по умолчанию, когда null встречается:

Используйте оператор условия null: ?. или ?[x] для массивов (доступно в C # 6 и VB.NET 14):

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

Если у человека нет заголовка, это вызовет исключение, потому что он пытается вызвать ToUpper свойство с нулевым значением.

В C# 5 и ниже, это может быть защищено с:

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

Это приведет к тому, что переменная заголовка будет null , и вызов ToUpper не будет выполнен, если person.Title есть null .

Конечно, вам все равно нужно проверить title наличие нуля или использовать оператор условия null вместе с оператором объединения нулей ( ?? ), чтобы задать значение по умолчанию:

Аналогично, для массивов вы можете использовать ?[i] следующее:

Это сделает следующее: Если значение myIntArray равно NULL, выражение возвращает значение NULL, и вы можете безопасно проверить его. Если он содержит массив, он будет делать то же самое, что и: elem = myIntArray[i]; и возвращает i<sup>th</sup> элемент.

Использовать нулевой контекст (доступно в C # 8):

Представленные C# 8 там нулевые контексты и ссылочные типы, допускающие значение NULL, выполняют статический анализ переменных и выдают предупреждение компилятору, если значение может быть потенциально нулевым или для него установлено значение NULL. Обнуляемые ссылочные типы позволяют типам быть явно пустыми.

Обнуляемый контекст аннотации и обнуляемый контекст предупреждения могут быть установлены для проекта с использованием Nullable элемента в вашем csproj файле. Этот элемент настраивает, как компилятор интерпретирует обнуляемость типов и какие предупреждения генерируются. Допустимые настройки:

  • enable: контекст аннулируемой аннотации включен. Обнуляемый контекст предупреждения включен. Переменные ссылочного типа, например строковые, не обнуляются. Все предупреждения об обнуляемости включены.
  • отключить: контекст аннулируемой аннотации отключен. Обнуляемый контекст предупреждения отключен. Переменные ссылочного типа не обращают внимания, как и более ранние версии C #. Все предупреждения об обнуляемости отключены.
  • safeonly: контекст аннулируемых аннотаций включен. Обнуляемый контекст предупреждения безопасен. Переменные ссылочного типа не обнуляются. Все предупреждения об опасности безопасности включены.
  • Предупреждения: контекст аннулируемой аннотации отключен. Обнуляемый контекст предупреждения включен. Переменные ссылочного типа не обращают внимания. Все предупреждения об обнуляемости включены.
  • safeonlywarnings: контекст аннулируемой аннотации отключен. Обнуляемый контекст предупреждения безопасен. Переменные ссылочного типа не обращают внимания. Все предупреждения об опасности безопасности включены.

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

Специальные методы отладки и исправления нулевых разыменований в итераторах

C# поддерживает «блоки итераторов» (называемые «генераторами» в некоторых других популярных языках). Исключения нулевого разыменования могут быть особенно сложными для отладки в блоках итераторов из-за отложенного выполнения:

Если whatever результаты в null то MakeFrob кинет. Теперь вы можете подумать, что правильно сделать следующее:

Почему это не так? Поскольку блок итератора фактически не выполняется до foreach ! Вызов GetFrobs просто возвращает объект, который при повторении запускает блок итератора.

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

То есть создайте приватный вспомогательный метод, который имеет логику блока итератора, и метод публичной поверхности, который выполняет нулевую проверку и возвращает итератор. Теперь, когда GetFrobs вызывается, проверка на ноль происходит немедленно, а затем GetFrobsForReal выполняется, когда последовательность повторяется.

Если вы изучите источник ссылки LINQ на Объекты, вы увидите, что эта техника используется повсеместно. Это немного более неуклюже, чтобы написать, но это делает отладку ошибок недействительности намного легче. Оптимизируйте свой код для удобства звонящего, а не для удобства автора .

Примечание о нулевых разыменованиях в небезопасном коде

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

В небезопасном режиме вы должны знать о двух важных фактах:

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

Чтобы понять, почему это так, это помогает понять, как .NET в первую очередь создает исключения с нулевым разыменованием. (Эти сведения относятся к .NET, работающему в Windows; другие операционные системы используют аналогичные механизмы.)

Память виртуализирована в Windows ; каждый процесс получает пространство виртуальной памяти из множества «страниц» памяти, которые отслеживаются операционной системой. На каждой странице памяти установлены флаги, которые определяют, как ее можно использовать: чтение, запись, выполнение и т. Д. Самая нижняя страница помечена как «выдает ошибку, если когда-либо используется каким-либо образом».

И нулевой указатель, и нулевая ссылка в нем C# внутренне представлены как нулевое число, и поэтому любая попытка разыменовать его в соответствующее хранилище памяти приводит к ошибке операционной системы. Затем среда выполнения .NET обнаруживает эту ошибку и превращает ее в исключение нулевой разыменования.

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

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

Почему это имеет смысл? Хорошо, предположим, что у нас есть структура, содержащая два целых числа, и неуправляемый указатель, равный нулю. Если мы попытаемся разыменовать второй тип int в структуре, CLR он не будет пытаться получить доступ к хранилищу в нулевом местоположении; он получит доступ к хранилищу в расположении четыре. Но логически это нулевая разыменование, потому что мы получаем по этому адресу через нуль.

Если вы работаете с небезопасным кодом и получаете исключение нулевой разыменования, просто имейте в виду, что указатель-нарушитель не обязательно должен быть нулевым. Это может быть любое место на самой нижней странице, и будет сделано это исключение.

  • Ошибка nullreferenceexception object reference not set to an instance of an object
  • Ошибка nullpointerexception java что это
  • Ошибка null при обновлении iphone 6s что означает
  • Ошибка null при обновлении ipad
  • Ошибка null при загрузке файла