Содержание
- Необъявленная ошибка NULL в C / C ++ и способы ее устранения
- Разрешение предупреждений, допускающих значения NULL
- Возможная разыменовка null
- Возможное значение NULL, присвоенное ненуляемой ссылке
- Ссылка, не инициализируемая
- Несоответствие в объявлении допустимости значений NULL
- Код не соответствует объявлению атрибута
- Исчерпывающее выражение переключателя
Необъявленная ошибка NULL в C / C ++ и способы ее устранения
Что такое необъявленная ошибка:
Когда мы используем некоторую константу в нашей программе, они могут быть встроенными константами и могут быть созданы пользователем в соответствии с требованиями. Но когда мы используем некоторую константу, а они не являются встроенными и также не определены пользователем в этом состоянии, мы получаем необъявленную ошибку.
Ниже приведен код, показывающий пример необъявленной ошибки NULL:
Приведенный выше код покажет ошибку как «необъявленная ошибка NULL» . Причина необъявленной ошибки NULL в том, что «NULL» не является встроенной константой.
Зачем нам NULL?
Когда мы создаем какой-либо указатель в нашей программе, они используются для хранения адресов. Но неинициализированные переменные-указатели очень опасны, так что мы можем присвоить им NULL, что означает, что они не указывают ни на какую ячейку памяти, поэтому наша программа работает плавно и безопасно.
Теперь, если NULL не является встроенной константой, как мы можем преодолеть необъявленную ошибку NULL.
Ниже приведен код, который используется для удаления необъявленной ошибки NULL:
- Присвоить 0: вместо присвоения NULL для num мы можем просто присвоить 0, что означает, что он не указывает какой-либо адрес, поэтому простейшим решением является просто присвоение 0.
Код ниже показывает его реализацию:
Источник
Разрешение предупреждений, допускающих значения NULL
В этой статье рассматриваются следующие предупреждения компилятора:
- CS8602 — Разыменовка возможной пустой ссылки.
- CS8670 — Инициализатор объекта или коллекции неявно разыменовывает элемент NULL.
- CS8601 — Возможное назначение пустой ссылки.
- CS8605 — Распаковка возможного значения NULL.
- CS8603 — Возможный возврат пустой ссылки.
- CS8604 — Возможный аргумент пустой ссылки для параметра.
- CS8600 — Преобразование литерала NULL или возможного значения NULL в тип, не допускающий значения NULL.
- CS8597 — Выброшенное значение может иметь значение NULL.
- CS8625 — Не удается преобразовать литерал NULL в ссылочный тип, не допускающий значения NULL.
- CS8629 — Тип значения, допускающий значение NULL, может иметь значение NULL.
- CS8618 — Переменная, не допускающая значения NULL, должна содержать значение, отличное от NULL, при выходе из конструктора. Рассмотрите возможность объявления этого параметра как допускающего значение NULL.
- CS8762 — При выходе параметр должен иметь значение, отличное от NULL.
- CS8619 — Допустимость значений NULL ссылочных типов в значении не соответствует целевому типу.
- CS8621 — Допустимость значений NULL ссылочных типов в возвращаемом типе не соответствует целевому делегату (возможно, из-за атрибутов допустимости значений NULL).
- CS8622 — Допустимость значений NULL ссылочных типов в типе параметра не соответствует целевому делегату (возможно, из-за атрибутов допустимости значений NULL).
- CS8631 — Тип нельзя использовать в качестве параметра типа в универсальном типе или методе. Допустимость значения NULL аргумента типа не соответствует типу ограничения.
- CS8634 — Тип нельзя использовать в качестве параметра типа в универсальном типе или методе. Допустимость значений NULL для аргумента типа не соответствует ограничению «class».
- CS8714 — Тип нельзя использовать в качестве параметра типа в универсальном типе или методе. Допустимость значения NULL для аргумента типа не соответствует ограничению notnull.
- CS8608 — Допустимость значений NULL ссылочных типов в типе не соответствует переопределенным членам.
- CS8609 — Допустимость значений NULL ссылочных типов в возвращаемом типе не соответствует переопределенным элементу.
- CS8819 — Допустимость значений NULL ссылочных типов в возвращаемом типе не соответствует объявлению частичного метода.
- CS8610 — Допустимость значений NULL ссылочных типов в параметре типа не совпадает с переопределенным элементом.
- CS8611 — Допустимость значений NULL ссылочных типов в параметре типа не соответствует объявлению частичного метода.
- CS8612 — Допустимость значений NULL ссылочных типов в типе не соответствует неявно реализованного члена.
- CS8613 — Допустимость значений NULL ссылочных типов в возвращаемом типе не соответствует неявно реализованного члена.
- CS8614 — Допустимость значений NULL ссылочных типов в типе параметра не соответствует неявно реализованного члена.
- CS8615 — Допустимость значений NULL ссылочных типов в типе не соответствует реализованной член.
- CS8616 — Допустимость значений NULL ссылочных типов в возвращаемом типе не совпадает с реализованным элементом.
- CS8617 — Допустимость значений NULL ссылочных типов в типе параметра не совпадает с реализованным элементом.
- CS8633 — Допустимость значений NULL в ограничениях для параметра типа метода не соответствует ограничениям для параметра типа метода интерфейса. Вместо этого рекомендуется использовать явную реализацию интерфейса.
- CS8643 — Допустимость значений NULL ссылочных типов в явном описателье интерфейса не соответствует интерфейсу, реализованного типом .
- CS8644 — Тип не реализует член интерфейса. Допустимость значений NULL ссылочных типов в интерфейсе, реализованном базовым типом, не совпадает.
- CS8620 — Аргумент нельзя использовать для параметра из-за различий в допустимости значений NULL ссылочных типов.
- CS8624 — Аргумент не может использоваться в качестве выходных данных из-за различий в допустимости значений NULL ссылочных типов.
- CS8645 — Элемент уже указан в списке интерфейсов по типу с разными значениями NULL ссылочных типов.
- CS8667 — Объявления разделяемых методов имеют несовместимость значений NULL в ограничениях для параметра типа.
- CS8764 — Допустимость значений NULL возвращаемого типа не соответствует переопределенным членам (возможно, из-за атрибутов допустимости значений NULL).
- CS8765 — Допустимость значений NULL типа параметра не соответствует переопределенным членам (возможно, из-за атрибутов допустимости значений NULL).
- CS8768 — Допустимость значений NULL ссылочных типов в возвращаемом типе не совпадает с реализованным элементом (возможно, из-за атрибутов допустимости значений NULL).
- CS8767 — Допустимость значений NULL ссылочных типов в типе параметра не соответствует неявно реализованного члена (возможно, из-за атрибутов допустимости значений NULL).
- CS8766 — Допустимость значений NULL ссылочных типов в возвращаемом типе не соответствует неявно реализованного члена (возможно, из-за атрибутов допустимости значений NULL).
- CS8769 — Допустимость значений NULL ссылочных типов в типе параметра не совпадает с реализованным элементом (возможно, из-за атрибутов допустимости значений NULL).
- CS8607 — Возможное значение NULL не может использоваться для типа, помеченного или [NotNull] [DisallowNull]
- CS8763 — Помеченный метод [DoesNotReturn] не должен возвращать.
- CS8770 — В методе отсутствует заметка [DoesNotReturn] для сопоставления реализованного или переопределенного элемента.
- CS8774 — При выходе член должен иметь значение, отличное от NULL.
- CS8776 — Элемент нельзя использовать в этом атрибуте.
- CS8775 — При выходе член должен иметь значение, отличное от NULL.
- CS8777 — При выходе параметр должен иметь значение, отличное от NULL.
- CS8824 — Параметр должен иметь значение, отличное от NULL, при выходе из него, так как параметр не равен NULL.
- CS8825 — Возвращаемое значение должно быть не null, так как параметр не равен NULL.
- CS8655 — Выражение switch не обрабатывает некоторые входные значения NULL (оно не является исчерпывающим).
- CS8847 — Выражение switch не обрабатывает некоторые входные значения NULL (оно не является исчерпывающим). Однако шаблон с предложением when может успешно соответствовать этому значению.
Назначение предупреждений, допускающих значение NULL, заключается в том, чтобы свести к минимуму вероятность того, что приложение вызовет System.NullReferenceException при запуске. Для достижения этой цели компилятор использует статический анализ и выдает предупреждения, если в коде есть конструкции, которые могут привести к исключениям пустой ссылки. Вы предоставляете компилятору сведения для его статического анализа путем применения заметок и атрибутов типа. Эти заметки и атрибуты описывают допустимость значений NULL для аргументов, параметров и членов типов. В этой статье вы узнаете о различных методах устранения предупреждений, допускающих значение NULL, которые компилятор создает в рамках статического анализа. Описанные здесь методы предназначены для общего кода C#. Сведения о работе с ссылочными типами, допускаемыми значение NULL, и ядром Entity Framework см. в статье Работа с ссылочными типами, допускаемыми значения NULL.
Вы будете устранять почти все предупреждения с помощью одного из четырех способов:
- Добавление необходимых проверок null.
- Добавление ? заметок или ! допускающих значение NULL.
- Добавление атрибутов, описывающих семантику null.
- Правильная инициализация переменных.
Возможная разыменовка null
Этот набор предупреждений предупреждает, что разыменовывание переменной с состоянием NULL может быть равно NULL. Ниже приведены следующие предупреждения:
- CS8602 — Разыменовка возможной пустой ссылки.
- CS8670 — Инициализатор объекта или коллекции неявно разыменовывает элемент NULL.
В следующем коде показан один пример каждого из предыдущих предупреждений:
В приведенном выше примере предупреждение заключается в Container том, что , c может иметь значение NULL для States свойства . При назначении новых состояний коллекции, которая может иметь значение NULL, возникает предупреждение.
Чтобы удалить эти предупреждения, необходимо добавить код для изменения состояния null переменной на not-null перед разыменовыванием. Предупреждение инициализатора коллекции может быть трудно обнаружить. Компилятор обнаруживает, что коллекция может иметь значение NULL , когда инициализатор добавляет в нее элементы.
Во многих случаях эти предупреждения можно исправить, проверив, что переменная не имеет значение NULL, прежде чем разыменовывание ее. Например, приведенный выше пример можно переписать следующим образом:
Другие случаи, когда вы получаете эти предупреждения, могут быть ложноположительными. У вас может быть частный служебный метод, который проверяет значение NULL. Компилятор не знает, что метод обеспечивает проверку значения NULL. Рассмотрим следующий пример, в котором используется метод частной служебной программы, IsNotNull :
Компилятор предупреждает, что при записи свойства message.Length может быть разыменовывание null, так как статический анализ определяет, что message это может быть null . Вы можете знать, что IsNotNull обеспечивает проверку значения NULL, и при возврате true значение null-состояния message должно быть не null. Эти факты необходимо сообщить компилятору. Одним из способов является использование оператора прощения значений NULL, ! . Инструкцию WriteLine можно изменить в соответствии со следующим кодом:
Оператор с прощением значений NULL делает выражение не пустым , даже если оно было возможно null без примененного ! . В этом примере лучшим решением является добавление атрибута в сигнатуру IsNotNull объекта :
сообщает System.Diagnostics.CodeAnalysis.NotNullWhenAttribute компилятору, что аргумент, используемый obj для параметра, не равен NULL, если метод возвращает true значение . Когда метод возвращает false , аргумент имеет то же состояние NULL , что и до вызова метода.
Существует широкий набор атрибутов, которые можно использовать для описания того, как ваши методы и свойства влияют на состояние NULL. О них можно узнать в справочной статье по языку атрибутов статического анализа, допускающих значение NULL.
Исправление предупреждения для разыменовки переменной типа maybe-null включает один из трех методов:
- Добавьте проверку отсутствующих значений NULL.
- Добавьте атрибуты анализа null в API, чтобы повлиять на статический анализ компилятора с состоянием NULL . Эти атрибуты сообщают компилятору, когда возвращаемое значение или аргумент после вызова метода должно быть равно null или не null .
- Примените оператор ! прощения значений NULL к выражению, чтобы принудить состояние к значению не null.
Возможное значение NULL, присвоенное ненуляемой ссылке
Этот набор предупреждений оповещает о том, что вы назначаете переменную, тип которой не может быть указан для выражения, состояние null которого может быть равно NULL. Ниже приведены следующие предупреждения.
- CS8601 — Возможное назначение пустой ссылки.
- CS8605 — Распаковка возможного значения NULL.
- CS8603 — Возможный возврат пустой ссылки.
- CS8604 — Возможный аргумент пустой ссылки для параметра.
- CS8600 — Преобразование литерала NULL или возможного значения NULL в тип, не допускающий значения NULL.
- CS8597 — Выброшенное значение может иметь значение NULL.
- CS8625 — Не удается преобразовать литерал NULL в ссылочный тип, не допускающий значения NULL.
- CS8629 — Тип значения, допускающий значение NULL, может иметь значение NULL.
Компилятор выдает эти предупреждения при попытке назначить выражение, которое может иметь значение NULL , переменной, которая не может быть указана. Например:
Различные предупреждения указывают на предоставление сведений о коде, таких как назначение, распаковка назначения, операторы return, аргументы методов и выражения throw.
Для устранения этих предупреждений можно выполнить одно из трех действий. Один из них заключается в добавлении заметки ? , чтобы сделать переменную ссылочным типом, допускающий значение NULL. Это изменение может вызвать другие предупреждения. Изменение переменной со ссылки, не допускающей значения NULL, на ссылку, допускающую значение NULL, изменяет ее состояние null по умолчанию с not-null на возможное значение NULL. Статический анализ компилятора может найти экземпляры, в которых разыменовывается переменная, которая может иметь значение NULL.
Другие действия указывают компилятору, что правая часть назначения не имеет значения NULL. Выражение справа может быть проверено на null перед назначением, как показано в следующем примере:
В предыдущих примерах показано назначение возвращаемого значения метода. Можно добавить заметки к методу (или свойству), чтобы указать, когда метод возвращает значение, не равное NULL. Часто System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute указывает, что возвращаемое значение не равно NULL , если входной аргумент не равен NULL. Другой альтернативой является добавление оператора null forgiving в ! правую часть:
Исправление предупреждения о назначении выражения может быть null переменной, не равной NULL, включает один из четырех способов:
- Измените левую часть назначения на тип, допускающий значение NULL. Это действие может привести к новым предупреждениям при разыменовании этой переменной.
- Перед назначением укажите проверку null.
- Примечания к API, который создает правую часть назначения.
- Добавьте оператор для прощения значений NULL в правую часть назначения.
Ссылка, не инициализируемая
Этот набор предупреждений оповещает вас о том, что вы назначаете переменную, тип которой не допускает значения NULL выражению, состояние NULL которого может быть равно NULL. Ниже приведены следующие предупреждения.
- CS8618 — Переменная, не допускающая значения NULL, должна содержать значение, отличное от NULL, при выходе из конструктора. Рекомендуется объявить его как допускающий значение NULL.
- CS8762 — Параметр должен иметь значение, отличное от NULL, при выходе.
Рассмотрим следующий класс в качестве примера:
Инициализация FirstName не LastName гарантируется. Если этот код является новым, попробуйте изменить общедоступный интерфейс. Приведенный выше пример можно обновить следующим образом:
Если требуется создать Person объект перед заданием имени, можно инициализировать свойства, используя значение по умолчанию, отличное от NULL:
Другой альтернативой может быть изменение этих элементов на ссылочные типы, допускающие значение NULL. Класс Person можно определить следующим образом, если null он должен быть разрешен для имени:
Существующий код может потребовать других изменений, чтобы сообщить компилятору о семантике null для этих членов. Возможно, вы создали несколько конструкторов, а класс может иметь частный вспомогательный метод, который инициализирует один или несколько членов. Вы можете переместить код инициализации в один конструктор и убедиться, что все конструкторы вызывают один с общим кодом инициализации. Можно также использовать атрибуты System.Diagnostics.CodeAnalysis.MemberNotNullAttribute и System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute . Эти атрибуты сообщают компилятору, что после вызова метода член не равен NULL . В приведенном ниже коде показан пример каждого метода. Класс Person использует общий конструктор, вызываемый всеми другими конструкторами. Класс Student имеет вспомогательный метод, помеченный атрибутом System.Diagnostics.CodeAnalysis.MemberNotNullAttribute :
Наконец, можно использовать оператор null forgiving, чтобы указать, что элемент инициализирован в другом коде. В другом примере рассмотрим следующие классы, представляющие модель Entity Framework Core:
Свойство DbSet инициализировано значением параметра null! . Это сообщает компилятору, что свойству присвоено значение, не равное NULL . Фактически база DbContext выполняет инициализацию набора. Статический анализ компилятора не выполняет этого. Дополнительные сведения о работе с ссылочными типами, допускаемыми значение NULL, и Entity Framework Core см. в статье Работа с ссылочными типами, допускаемыми значение NULL, в EF Core.
Исправление предупреждения об инициализации элемента, не допускающего значения, включает один из четырех способов:
- Измените конструкторы или инициализаторы полей, чтобы обеспечить инициализацию всех элементов, не допускающих значения.
- Измените один или несколько членов на типы, допускаемые значением NULL.
- Примечайте все вспомогательные методы, чтобы указать, какие члены назначены.
- Добавьте инициализатор в , null! чтобы указать, что элемент инициализирован в другом коде.
Несоответствие в объявлении допустимости значений NULL
Многие предупреждения указывают на несоответствие допустимости значений NULL между сигнатурами для методов, делегатов или параметров типа.
- CS8619 — Допустимость значения NULL ссылочных типов в значении не соответствует целевому типу.
- CS8621 — Допустимость значений NULL ссылочных типов в возвращаемом типе не соответствует целевому делегату (возможно, из-за атрибутов допустимости значений NULL).
- CS8622 — Допустимость значений NULL ссылочных типов в типе параметра не соответствует целевому делегату (возможно, из-за атрибутов допустимости значений NULL).
- CS8631 — Тип нельзя использовать в качестве параметра типа в универсальном типе или методе. Допустимость значения NULL аргумента типа не соответствует типу ограничения.
- CS8634 — Тип нельзя использовать в качестве параметра типа в универсальном типе или методе. Допустимость значения NULL для аргумента типа не соответствует ограничению «класс».
- CS8714 — Тип нельзя использовать в качестве параметра типа в универсальном типе или методе. Допустимость значения NULL аргумента типа не соответствует ограничению notnull.
- CS8608 — Допустимость значений NULL ссылочных типов в типе не соответствует переопределенному элементу.
- CS8609 — Допустимость значений NULL ссылочных типов в возвращаемом типе не соответствует переопределенному элементу.
- CS8819 — Допустимость значений NULL ссылочных типов в возвращаемом типе не соответствует объявлению разделяемого метода.
- CS8610 — Допустимость значений NULL ссылочных типов в параметре типа не соответствует переопределенному элементу.
- CS8611 — Допустимость значений NULL ссылочных типов в параметре типа не соответствует объявлению частичного метода.
- CS8612 — Допустимость значений NULL ссылочных типов в типе не соответствует неявно реализованного члена.
- CS8613 — Допустимость значений NULL ссылочных типов в возвращаемом типе не соответствует неявно реализованного члена.
- CS8614 — Допустимость значений NULL ссылочных типов в типе параметра не соответствует неявно реализованного члена.
- CS8615 — Допустимость значений NULL ссылочных типов в типе не соответствует реализованной элемент.
- CS8616 — Допустимость значений NULL ссылочных типов в возвращаемом типе не совпадает с реализованным элементом.
- CS8617 — Допустимость значений NULL ссылочных типов в типе параметра не совпадает с реализованным элементом.
- CS8633 — Допустимость значений NULL в ограничениях для параметра типа метода не соответствует ограничениям для параметра типа метода интерфейса. Вместо этого рекомендуется использовать явную реализацию интерфейса.
- CS8643 — Допустимость значений NULL ссылочных типов в явном описателье интерфейса не соответствует интерфейсу, реализованного типом.
- CS8644 — Тип не реализует член интерфейса. Допустимость значений NULL ссылочных типов в интерфейсе, реализованном базовым типом, не соответствует.
- CS8620 — Аргумент нельзя использовать для параметра из-за различий в допустимости значений NULL ссылочных типов.
- CS8624 — Аргумент не может использоваться в качестве выходных данных из-за различий в допустимости значений NULL ссылочных типов.
- CS8645 — Элемент уже указан в списке интерфейсов по типу с разными допусками значений NULL ссылочных типов.
- CS8667 — Частичные объявления методов имеют несогласованную допустимость значений NULL в ограничениях для параметра типа.
- CS8764 — Допустимость значений NULL для возвращаемого типа не соответствует переопределенному элементу (возможно, из-за атрибутов допустимости значений NULL).
- CS8765 — Допустимость значений NULL для типа параметра не соответствует переопределенному элементу (возможно, из-за атрибутов допустимости значений NULL).
- CS8768 — Допустимость значений NULL ссылочных типов в возвращаемом типе не совпадает с реализованным элементом (возможно, из-за атрибутов допустимости значений NULL).
- CS8767 — Допустимость значений NULL ссылочных типов в типе параметра не соответствует неявно реализованного члена (возможно, из-за атрибутов допустимости значений NULL).
- CS8766 — Допустимость значений NULL ссылочных типов в возвращаемом типе не соответствует неявно реализованного члена (возможно, из-за атрибутов допустимости значений NULL).
- CS8769 — Допустимость значений NULL ссылочных типов в типе параметра не совпадает с реализованным элементом (возможно, из-за атрибутов допустимости значений NULL).
Следующий код демонстрирует cs8764:
В предыдущем примере показан virtual метод в базовом классе и с override разными значениями NULL. Базовый класс возвращает строку, не допускаемую значение NULL, но производный класс возвращает строку, допускаемую значение NULL. string Если и string? являются обратными, это будет разрешено, так как производный класс является более строгим. Аналогичным образом, объявления параметров должны совпадать. Параметры в методе переопределения могут разрешать значение NULL, даже если базовый класс этого не выполняет.
Эти предупреждения могут создаваться в других ситуациях. Возможно, у вас есть несоответствие в объявлении метода интерфейса и реализации этого метода. Или тип делегата и выражение для этого делегата могут отличаться. Параметр типа и аргумент типа могут отличаться по допустимости значений NULL.
Чтобы устранить эти предупреждения, обновите соответствующее объявление.
Код не соответствует объявлению атрибута
В предыдущих разделах обсуждалось, как использовать атрибуты для статического анализа, допускающего значение NULL , чтобы сообщить компилятору о семантике null кода. Компилятор выдает предупреждение, если код не соответствует обещаниям этого атрибута:
- CS8607 — Возможное значение NULL не может использоваться для типа, помеченного или [NotNull] [DisallowNull]
- CS8763 — Помеченный метод [DoesNotReturn] не должен возвращать.
- CS8770 — В методе отсутствует заметка [DoesNotReturn] для сопоставления реализованного или переопределенного элемента.
- CS8774 — При выходе член должен иметь значение, отличное от NULL.
- CS8776 — Элемент нельзя использовать в этом атрибуте.
- CS8775 — При выходе член должен иметь значение, отличное от NULL.
- CS8777 — При выходе параметр должен иметь значение, отличное от NULL.
- CS8824 — Параметр должен иметь значение, отличное от NULL, при выходе из него, так как параметр не равен NULL.
- CS8825 — Возвращаемое значение должно быть не null, так как параметр не равен NULL.
Рассмотрим следующий метод.
Компилятор выдает предупреждение, так как message параметр назначается null , а метод возвращает true . Атрибут NotNullWhen указывает, что это не должно происходить.
Чтобы устранить эти предупреждения, обновите код, чтобы он соответствовал ожиданиям примененных атрибутов. Вы можете изменить атрибуты или алгоритм.
Исчерпывающее выражение переключателя
Выражения switch должны быть исчерпывающими, что означает, что все входные значения должны обрабатываться. Даже для ссылочных типов, не допускающих значения NULL, null необходимо учитывать значение . Компилятор выдает предупреждения, если значение NULL не обрабатывается:
- CS8655 — Выражение switch не обрабатывает некоторые входные значения NULL (оно не является исчерпывающим).
- CS8847 — Выражение switch не обрабатывает некоторые входные значения NULL (оно не является исчерпывающим). Однако шаблон с предложением when может успешно соответствовать этому значению.
Это условие демонстрируется в следующем примере кода:
Входное выражение является string , а string? не . Компилятор по-прежнему создает это предупреждение. Шаблон < >обрабатывает все значения, отличные от NULL, но не соответствует null . Чтобы устранить эти ошибки, можно либо добавить явный null регистр, либо заменить < >шаблоном _ (отменить). Шаблон отмены соответствует значению NULL, а также любому другому значению.
Источник
Иногда при попытке настроить общий доступ к точке W-Fi, созданной на ПК, пользователи получают сообщение от системы об ошибке с кодом null. Что это за сбой и как быть в данной ситуации.
Почему пользователи сталкиваются с данной ошибкой
Ноутбук может работать в качестве роутера, то есть раздавать интернет другим устройствам, находящимся рядом. Например, ПК подключён к интернету с помощью обычного провода: Wi-Fi в этом случае нет. При этом у человека есть ещё и другие гаджеты (планшет, смартфон и т. д.), которые также желательно подключить к сети. Решение проблемы может быть в виде покупки роутера либо же создания виртуального Wi-Fi на компьютере, которое будет использоваться в качестве маршрутизатора, раздающего сигнал.
Однако настроить точку доступа на компьютере недостаточно: другие устройства смогут к ней подключиться, но при этом интернета у них не будет. В окне «Свойства» созданного подключения нужно для этого разрешить общий доступ к сети.
Во вкладке «Доступ» пользователь может активировать общий доступ к созданной им точке Wi-Fi
При попытке это сделать некоторые пользователи сталкиваются с ошибкой под кодом null. Почему система не даёт разрешить другим девайсом пользоваться данным подключением к интернету?
При попытке включить общий доступ на экране может появится сообщение об ошибке с кодом null
Основная причина — отключённый «Брандмауэр Защитника Windows». В этой ОС нельзя дать доступ к интернету, если стандартная программа для защиты устройства была изъята из служб. При этом неважно, какая у вас версия: «семёрка», «десятка» или XP.
Как исправить ошибку
Выход из ситуации довольно простой — нужно снова запустить «Брандмауэр Защитника Windows», который был по каким-то причинам отключён. Возможно, это сделал сам пользователь ранее или же произошёл какой-то сбой в системе, так как по умолчанию служба защитника всегда работает. После включения клиент Windows сможет разрешить доступ и начать раздавать интернет со своего ПК.
Активация «Брандмауэра Windows»
Запустить эту встроенную утилиту можно двумя способами: в «Службах» и на «Панели управления». Они оба просты в освоении, поэтому справится с задачей даже новичок.
Через «Службы»
В системном окне «Службы» любой пользователь ПК может включить ту или иную службу, а также настроить её автоматический запуск вместе с ОС. У «Защитника Windows» есть своя служба. Как её найти в перечне и включить, рассмотрим в инструкции:
- Быстро открыть окно «Службы» поможет стандартный сервис «Выполнить». Зажимаем на клавиатуре две кнопки: Win + R. В появившемся маленьком окошке вставляем код services.msc. Вы можете его также просто напечатать. Будьте внимательны, чтобы не сделать ошибку в слове. Тут же жмём на ОК.
В строке «Открыть» печатаем или вставляем заранее скопированный код services.msc
- В правой части окна с большим перечнем служб находим «Брандмауэр Защитника Windows». Пункты в списке упорядочены по алфавиту. Прокрутите объекты с английскими названиями. Нужная служба будет практически в начале списка.
В окне «Службы» ищем пункт «Брандмауэр Защитника Windows»
- Щёлкаем по нему правой клавишей мышки — в небольшом сером меню выбираем последний раздел «Свойства».
В сером меню кликаем по пункту «Свойства» для запуска дополнительного окна
- Поверх главного окна со службами появится дополнительное, в котором и нужно выполнить манипуляции. В выпадающем меню «Тип запуска» кликаем по «Автоматически». Это позволит системе запускать программу для защиты сразу после включения компьютера и загрузки ОС. Теперь жмём на первую кнопку в ряде под названием «Запустить».
Выбираем автоматический тип запуска и кликаем по кнопке «Запустить»
- Чтобы внесённые изменения начали сразу же действовать, кликаем сначала по «Применить», а затем по ОК.
- Пробуем снова дать доступ к точке Wi-Fi. Проблема должна быть решена.
Через «Панель управления»
Встроенную программу для защиты можно активировать и через «Панель управления». Рассмотрим подробно, как добраться до его раздела:
- Запустить эту классическую утилиту Windows можно несколькими методами. Если у вас «семёрка», вы можете найти её в системном меню «Пуск», которое открывается кликом по иконке виде окна, расположенной в левом нижнем углу экрана.
Откройте «Панель управления» через меню «Пуск», если у вас Windows 7
- Если у вас Windows 10, кликните по значку в виде лупы на «Панели задач» — появится панель для поиска. Напечатайте соответствующий запрос. По мере ввода система уже начнёт выдавать в небольшом окне результаты. Кликаем по нужному пункту, чтобы запустить панель.
В Windows 10 вы можете найти «Панель управления» через универсальное окошко для поиска разделов по системе
- Универсальный метод для запуска, который подходит для всех версий «операционки» — окно «Выполнить». Вызываем его комбинацией клавиш Win + R, а затем в поле «Открыть» печатаем простой код control. Кликаем по ОК — на экране появится «Панель управления».
Команда control в окне «Открыть» поможет запустить «Панель управления»
- В перечне находим название блока «Брандмауэр Защитника Windows». Если у вас стоит значение «Мелкие значки» для параметра «Просмотр», он будет вторым в первом столбце.
Найдите в списке пункт «Брандмауэр Защитника Windows»
- В левой колонке с множеством ссылок синего цвета кликаем по четвёртой «Включение и отключение брандмауэра…».
Жмём по ссылке ««Включение и отключение брандмауэра…» для открытия следующей страницы
- Ставим круглые отметки рядом с пунктами о включении защитника. Нужно активировать программу как для частных, так и для общедоступных сетей.
Включите «Брандмауэр Windows» для каждого типа сети
- Жмём на ОК, закрываем все окна и перезапускаем ПК.
Видео: два метода запуска «Брандмауэра Windows»
Проблема с кодом null решается простой активацией «Брандмауэра Windows». Данную утилиту, встроенную в систему Windows, можно включить в одном из двух окон: «Панель управления» либо «Службы». В последнем вы также можете настроить автоматический запуск приложения с каждой загрузкой ОС.
- Автор: Екатерина Васильева
- Распечатать
Оцените статью:
- 5
- 4
- 3
- 2
- 1
(0 голосов, среднее: 0 из 5)
Поделитесь с друзьями!
Что такое необъявленная ошибка:
Когда мы используем некоторую константу в нашей программе, они могут быть встроенными константами и могут быть созданы пользователем в соответствии с требованиями. Но когда мы используем некоторую константу, а они не являются встроенными и также не определены пользователем в этом состоянии, мы получаем необъявленную ошибку.
Ниже приведен код, показывающий пример необъявленной ошибки NULL:
using
namespace
std;
int
main()
{
int
* num = NULL;
return
0;
}
Приведенный выше код покажет ошибку как «необъявленная ошибка NULL» . Причина необъявленной ошибки NULL в том, что «NULL» не является встроенной константой.
Зачем нам NULL?
Когда мы создаем какой-либо указатель в нашей программе, они используются для хранения адресов. Но неинициализированные переменные-указатели очень опасны, так что мы можем присвоить им NULL, что означает, что они не указывают ни на какую ячейку памяти, поэтому наша программа работает плавно и безопасно.
Теперь, если NULL не является встроенной константой, как мы можем преодолеть необъявленную ошибку NULL.
Ниже приведен код, который используется для удаления необъявленной ошибки NULL:
- Присвоить 0: вместо присвоения NULL для num мы можем просто присвоить 0, что означает, что он не указывает какой-либо адрес, поэтому простейшим решением является просто присвоение 0.
Код ниже показывает его реализацию:using
namespace
std;
int
main()
{
int
* num = 0;
return
0;
}
- Включите файл заголовка «stddef.h»: в файле заголовка stddef.h уже определен NULL , поэтому мы можем включить этот файл заголовка в нашу программу, и наша программа будет компилироваться и выполняться без каких-либо ошибок.
Код ниже показывает его реализацию:#include <stddef.h>
int
main()
{
int
* num = NULL;
return
0;
}
- Включите файл заголовка iostream: в C ++, если мы хотим выполнить нашу программу без необнаруженной ошибки NULL, мы можем просто включить iostream в нашу программу и сделать это без ошибок.
Код ниже показывает его реализацию:#include <iostream>
using
namespace
std;
int
main()
{
int
* num = NULL;
return
0;
}
- #define NULL 0: Используя строку #define NULL 0 в нашей программе, мы можем решить необъявленную ошибку NULL.
Код ниже показывает его реализацию:#define NULL 0
using
namespace
std;
int
main()
{
int
* num = NULL;
return
0;
}
- В новом C ++ (C ++ 11 и выше):: nullptr — это встроенная константа, поэтому мы можем использовать ее вместо NULL.
#include <iostream>
using
namespace
std;
int
main()
{
int
* num = nullptr;
return
0;
}
Хотите учиться на лучших видео и практических задачах, ознакомьтесь с базовым курсом C ++ для базового и продвинутого уровня C ++ и курсом C ++ STL для языка и STL. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .
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:
- 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. - The examples (from Stack Overflow posts) do not always show the best way to do something in the first place.
- 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
.
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 ofNullReference Exceptions
seen in the Stack Overflow questions reviewed.Visual Basic tries to make the process clear repeatedly using
New
: Using theNew
Operator creates a new object and callsSub 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:
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:
«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:
- 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. - The examples (from Stack Overflow posts) do not always show the best way to do something in the first place.
- 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
.
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 ofNullReference Exceptions
seen in the Stack Overflow questions reviewed.Visual Basic tries to make the process clear repeatedly using
New
: Using theNew
Operator creates a new object and callsSub 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:
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:
«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
Разработчики регулярно выпускают обновления для свой игры, в котором они улучшают производительность и различные проблемы. После очередного обновления многие игроки начали сталкиваться с ошибкой null при входе на сервер Minecraft. В этой статье мы расскажем, базовые действия, которые помогут решить эту проблему.
Чаще всего данная ошибка появляется у игроков, которые играют без лицензии. На сегодняшний день существует множество способов, как можно обойти эту проверку. Но это временное решение, которое помогает убрать это уведомление при входе на сервер. Играть на официальных серверах с пиратской версии стало сложнее. Возникновения данной ошибки может происходить из-за установленных модов или дополнений. Ниже мы разберем все возможные способы, как можно её обойти.
Что делать с ошибкой null
Если у вас нет лицензионной игры, вы скорее всего играете через лаунчер по названием Tlauncher. Данный клиент, позволяет играть в Майнкрафт в последние версии абсолютно бесплатно. Разработчики ежемесячно обновляют совой клиент. После обновлений пользователь может столкнуться с ошибкой при входе на сервер. В этом случае попробуйте выполнить последовательно следующие действия:
- Запустите приложение Tlauncher;
- Авторизуйтесь в клиенте, если у вас нет аккаунта, то необходимо создать;
- Далее в нижем левом экране, возле «Аккаунты» уберите галочку;
- Готово, теперь вы можете попробовать зайти на какой-нибудь сервер.
Отключение антивируса
Отключите антивирус во время обновления или игры Майнкрафт. Для этого проделайте действия:
- Нажмите правой кнопкой мыши значок антивируса в области уведомлений;
- Теперь выберите приостановить антивирус;
- Далее выберите подходящий параметр времени для отключения антивируса (например, 10 минут, 1 час, приостановить до следующей перезагрузки).
Заключение
Из этой статьи вы узнали, как исправить проблему при соединении к серверам в Майнкрафт. Если ничего не помогло из вышеперечисленного, удалите полностью игру с вашего компьютера, и установите новую скачав с официального сайта. Остались вопросы? Напишите ниже в форме комментариев.
Оценка статьи:
Загрузка…