I have some code and when it executes, it throws a NullReferenceException
, saying:
Object reference not set to an instance of an object.
What does this mean, and what can I do to fix this error?
I have some code and when it executes, it throws a NullReferenceException
, saying:
Object reference not set to an instance of an object.
What does this mean, and what can I do to fix this error?
On the matter of "what should I do about it", there can be many answers.
A more "formal" way of preventing such error conditions while developing is applying design by contract in your code. This means you need to set class invariants, and/or even function/method preconditions and postconditions on your system, while developing.
In short, class invariants ensure that there will be some constraints in your class that will not get violated in normal use (and therefore, the class will not get in an inconsistent state). Preconditions mean that data given as input to a function/method must follow some constraints set and never violate them, and postconditions mean that a function/method output must follow the set constraints again without ever violating them. Contract conditions should never be violated during execution of a bug-free program, therefore design by contract is checked in practice in debug mode, while being disabled in releases, to maximize the developed system performance.
This way, you can avoid
NullReferenceException
cases that are results of violation of the constraints set. For example, if you use an object propertyX
in a class and later try to invoke one of its methods andX
has a null value, then this will lead toNullReferenceException
:But if you set "property X must never have a null value" as method precondition, then you can prevent the scenario described before:
For this cause, Code Contracts project exists for .NET applications.
Alternatively, design by contract can be applied using assertions.
UPDATE: It is worth mentioning that the term was coined by Bertrand Meyer in connection with his design of the Eiffel programming language.
What can you do about it?
There is a lot of good answers here explaining what a null reference is and how to debug it. But there is very little on how to prevent the issue or at least make it easier to catch.
Check arguments
For example, methods can check the different arguments to see if they are null and throw an
ArgumentNullException
, an exception obviously created for this exact purpose.The constructor for the
ArgumentNullException
even takes the name of the parameter and a message as arguments so you can tell the developer exactly what the problem is.Use Tools
There are also several libraries that can help. "Resharper" for example can provide you with warnings while you are writing code, especially if you use their attribute: NotNullAttribute
There's "Microsoft Code Contracts" where you use syntax like
Contract.Requires(obj != null)
which gives you runtime and compile checking: Introducing Code Contracts.There's also "PostSharp" which will allow you to just use attributes like this:
By doing that and making PostSharp part of your build process
obj
will be checked for null at runtime. See: PostSharp null checkPlain Code Solution
Or you can always code your own approach using plain old code. For example here is a struct that you can use to catch null references. It's modeled after the same concept as
Nullable<T>
:You would use very similar to the same way you would use
Nullable<T>
, except with the goal of accomplishing exactly the opposite - to not allownull
. Here are some examples:NotNull<T>
is implicitly cast to and fromT
so you can use it just about anywhere you need it. For example, you can pass aPerson
object to a method that takes aNotNull<Person>
:As you can see above as with nullable you would access the underlying value through the
Value
property. Alternatively, you can use an explicit or implicit cast, you can see an example with the return value below:Or you can even use it when the method just returns
T
(in this casePerson
) by doing a cast. For example, the following code would just like the code above:Combine with Extension
Combine
NotNull<T>
with an extension method and you can cover even more situations. Here is an example of what the extension method can look like:And here is an example of how it could be used:
GitHub
For your reference I made the code above available on GitHub, you can find it at:
https://github.com/luisperezphd/NotNull
Related Language Feature
C# 6.0 introduced the "null-conditional operator" that helps with this a little. With this feature, you can reference nested objects and if any one of them is
null
the whole expression returnsnull
.This reduces the number of null checks you have to do in some cases. The syntax is to put a question mark before each dot. Take the following code for example:
Imagine that
country
is an object of typeCountry
that has a property calledState
and so on. Ifcountry
,State
,County
, orCity
isnull
thenaddress will be
null. Therefore you only have to check whether
addressis
null`.It's a great feature, but it gives you less information. It doesn't make it obvious which of the 4 is null.
Built-in like Nullable?
C# has a nice shorthand for
Nullable<T>
, you can make something nullable by putting a question mark after the type like soint?
.It would be nice if C# had something like the
NotNull<T>
struct above and had a similar shorthand, maybe the exclamation point (!) so that you could write something like:public void WriteName(Person! person)
.You can fix NullReferenceException in a clean way using Null-conditional Operators in c#6 and write less code to handle null checks.
It's used to test for null before performing a member access (?.) or index (?[) operation.
Example
is equivalent to:
The result is that the name will be null when p is null or when p.Spouse is null.
Otherwise, the variable name will be assigned the value of the p.Spouse.FirstName.
For More details : Null-conditional Operators
TL;DR: Try using
Html.Partial
instead ofRenderpage
I was getting
Object reference not set to an instance of an object
when I tried to render a View within a View by sending it a Model, like this:Debugging showed the model was Null inside MyOtherView. Until I changed it to:
And it worked.
Furthermore, the reason I didn't have
Html.Partial
to begin with was because Visual Studio sometimes throws error-looking squiggly lines underHtml.Partial
if it's inside a differently constructedforeach
loop, even though it's not really an error:But I was able to run the application with no problems with this "error". I was able to get rid of the error by changing the structure of the
foreach
loop to look like this:Although I have a feeling it was because Visual Studio was misreading the ampersands and brackets.
To use methods and member of an object you first have to create that object. If you didn't create it (variable that should hold the object is not initialized), but you try to use it's methods or variables you'll get that error.
Sometime you may just forgot to do initialization.
Edited: new can't return null, but fire's exception when failed. Long time ago it was the case in some languages, but not any more. Thanks @John Saunders for pointing that out.
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:
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.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:
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 beNothing
.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 displaysError 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:
Examples and Remedies
Class Objects / Creating an Instance
The problem is that
Dim
does not create a CashRegister object; it only declares a variable namedreg
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:When it is only appropriate to create the instance later:
Note: Do not use
Dim
again in a procedure, including the constructor (Sub New
):This will create a local variable,
reg
, which exists only in that context (sub). Thereg
variable with module levelScope
which you will use everywhere else remainsNothing
.To be clear,
Dim
(orPrivate
) only declares a variable and itsType
. 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:
Arrays
Arrays must also be instantiated:
This array has only been declared, not created. There are several ways to initialize an array:
Note: Beginning with VS 2010, when initializing a local array using a literal and
Option Infer
, theAs <Type>
andNew
elements are optional:The data Type and array size are inferred from the data being assigned. Class/Module level declarations still require
As <Type>
withOption Strict
:Example: Array of class objects
The array has been created, but the
Foo
objects in it have not.Remedy
Using a
List(Of T)
will make it quite difficult to have an element without a valid object:For more information, see:
Lists and Collections
.NET collections (of which there are many varieties - Lists, Dictionary, etc.) must also be instantiated or created.
You get the same exception for the same reason -
myList
was only declared, but no instance created. The remedy is the same:A common oversight is a class which uses a collection
Type
:Either procedure will result in an NRE, because
barList
is only declared, not instantiated. Creating an instance ofFoo
will not also create an instance of the internalbarList
. It may have been the intent to do this in the constructor:As before, this is incorrect:
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
As before, the
ds
Dataset object was declared, but an instance was never created. TheDataAdapter
will fill an existingDataSet
, not create one. In this case, sinceds
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
Example 2
A typo is a problem here:
Employees
vsEmployee
. There was noDataTable
named "Employee" created, so aNullReferenceException
results trying to access it. Another potential problem is assuming there will beItems
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. ExaminingRows.Count
can also help:Fill
is a function returning the number ofRows
affected which can also be tested:Example 3
The
DataAdapter
will provideTableNames
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:
See also DataTable Class.
Object Paths / Nested
The code is only testing
Items
while bothmyFoo
andBar
may also be Nothing. The remedy is to test the entire chain or path of objects one at a time:AndAlso
is important. Subsequent tests will not be performed once the firstFalse
condition is encountered. This allows the code to safely 'drill' into the object(s) one 'level' at a time, evaluatingmyFoo.Bar
only after (and if)myFoo
is determined to be valid. Object chains or paths can get quite long when coding complex objects:It is not possible to reference anything 'downstream' of a
null
object. This also applies to controls:Here,
myWebBrowser
orDocument
could be Nothing or theformfld1
element may not exist.UI Controls
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 beNothing
, soListBox1.SelectedItem.ToString
will result in an NRE.Remedy
Validate data before using it (also use
Option Strict
and SQL parameters):Alternatively, you can use
(ComboBox5.SelectedItem IsNot Nothing) AndAlso...
Visual Basic Forms
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 theControls
. As a result:somevar
assignment will result in an immediate NRE because Nothing doesn't have a.Text
propertyReferencing 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
orForm Load
event), exceptions may go unreported, the code exits the procedure and just displays the form.Since no other code in your
Sub New
orForm Load
event will run after the NRE, a great many other things can be left uninitialized.Note this applies to any and all control and component references making these illegal where they are:
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 theInitializeComponent
call:The array code may not be out of the woods yet. Any controls which are in a container control (like a
GroupBox
orPanel
) will not be found inMe.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](https://i.stack.imgur.com/5ndSA.jpg)
"Button2" resides on a
Panel
Remedy
Rather than indirect references by name using the form's
Controls
collection, use the control reference:Function Returning Nothing
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 replacingExit Function
withReturn Nothing
, but that does not solve the problem. Anything which tries to use the return whensomeCondition = False
will result in an NRE:Remedy
Replace
Exit Function
in the function withReturn bList
. Returning an emptyList
is not the same as returningNothing
. If there is a chance that a returned object can beNothing
, test before using it:Poorly Implemented Try/Catch
A badly implemented Try/Catch can hide where the problem is and result in new ones:
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 theCatch
does nothing,Finally
tries to perform clean up, but since you cannotClose
a nullDataReader
object, a brand newNullReferenceException
results.An empty
Catch
block is the devil's playground. This OP was baffled why he was getting an NRE in theFinally
block. In other situations, an emptyCatch
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
The
IsDBNull
function is used to test if a value equalsSystem.DBNull
: From MSDN:Remedy
As before, you can test for Nothing, then for a specific value:
Example 2
FirstOrDefault
returns the first item or the default value, which isNothing
for reference types and neverDBNull
:Controls
If a
CheckBox
withchkName
can't be found (or exists in aGroupBox
), thenchk
will be Nothing and be attempting to reference any property will result in an exception.Remedy
The DataGridView
The DGV has a few quirks seen periodically:
If
dgvBooks
hasAutoGenerateColumns = 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:
Example 2 — Beware of the NewRow
When your
DataGridView
hasAllowUserToAddRows
asTrue
(the default), theCells
in the blank/new row at the bottom will all containNothing
. Most attempts to use the contents (for example,ToString
) will result in an NRE.Remedy
Use a
For/Each
loop and test theIsNewRow
property to determine if it is that last row. This works whetherAllowUserToAddRows
is true or not:If you do use a
For n
loop, modify the row count or useExit For
whenIsNewRow
is true.My.Settings (StringCollection)
Under certain circumstances, trying to use an item from
My.Settings
which is aStringCollection
can result in a NullReference the first time you use it. The solution is the same, but not as obvious. Consider: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: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