Avoiding null reference exceptions
When a null reference exception is displayed to the user, this indicates a defect in the code resulting from an error on the part of the developer. Here are some ideas on how to prevent these errors.
My top recommendation for people who care about software quality, and are also using the.net programming platform, is to install and use Microsoft code contracts ( http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx ). It includes capabilities to do run-time checking and static verification. The essential capability to build these contracts into your code is being included in the 4.0 version of the.net framework. If you are interested in code quality, and it sounds like you are, you may really enjoy using Microsoft code contracts.
With Microsoft code contracts, you can guard your method from null values by adding preconditions like this "Contract.Requires(customer != null);". Adding a precondition like this is equivalent to the practice recommended by many others in their comments above. Prior to code contracts, I would have recommended you do something like this
if (customer == null) {throw new ArgumentNullException("customer");}
Now I recommend
Contract.Requires(customer != null);
You can then enable the run-time checking system which will catch these defects as early as possible, leading you towards diagnosis and correction of the defective code. But don't let me give you the impression that code contracts are simply a fancy way to replace argument null exceptions. They are much more powerful than that.
With Microsoft code contracts, you can also run the static checker, and ask it to investigate possible sites in your code where null reference exceptions might occur. The static checker requires a bit more experience to use easily. I would not recommend it first for beginners. But feel free to try it out and see for yourself.
RESEARCH ON THE PREVALENCE OF NULL REFERENCE ERRORS
There has been some debate in this thread on whether null reference errors are a significant problem. A long-winded answer is below. For people who don't want to wade through that, I will summarize.
- Microsoft's leading researchers in
program correctness on the Spec# and
code contracts projects believe it is
a problem worth addressing. - Dr. Bertrand Meyer and the team of
software engineers at ISE, who
developed and support the Eiffel
programming language, also believe it
is a problem worth addressing. - In my own commercial experience developing ordinary software, I have seen null reference errors often enough, that I would like to address the problem in my own products and practices.
For years, Microsoft has invested in research designed to improve software quality. One of their efforts was the Spec# project. One of the most exciting developments in my opinion with the.net 4.0 framework, is the introduction of Microsoft code contracts, which is an outgrowth of the earlier work done by the Spec# research team.
Regarding your remark "the vast majority of errors in code are null reference exceptions", I believe it is the qualifier "the vast majority" that will cause some disagreements. The phrase "Vast majority" suggests that perhaps 70-90% of faults have a null reference exception as the root cause. This seems far too high to me. I prefer to quote from the research of the Microsoft Spec#. In their article The Spec# programming system: An overview, by Mike Barnett, K. Rustan M. Leino, and Wolfram Schulte. In CASSIS 2004, LNCS vol. 3362, Springer, 2004, they wrote
1.0 Non-Null Types Many errors in modern programs manifest themselves as
null-dereference errors, suggesting
the importance of a programming
language providing the ability to
discriminate between expressions that
may evaluate to null and those that
are sure not to (for some experimental
evidence, see [24, 22]). In fact, we
would like to eradicate all null
dereference errors.
This is a likely source for people at Microsoft who are familiar with this research. This article is available at the Spec# site.
I've copied references 22 and 24 below, and included the ISBN for your convenience.
Manuel Fahndrich and K. Rustan M. Leino. Declaring and checking non-null types in an
object-oriented language. In Proceedings of the 2003 ACM Conference on Object-Oriented
Programming, Systems, Languages, and Applications, OOPSLA 2003, volume 38, number
11 in SIGPLAN Notices, pages 302–312. ACM, November 2003. isbn = {1-58113-712-5},Cormac Flanagan, K. Rustan M. Leino, Mark Lillibridge, Greg Nelson, James B. Saxe,
and Raymie Stata. Extended static checking for Java. In Proceedings of the 2002 ACM
SIGPLAN Conference on Programming Language Design and Implementation (PLDI), volume
37, number 5 in SIGPLAN Notices, pages 234–245. ACM, May 2002.
I reviewed these references. The first reference indicates some experiments they did reviewing their own code for possible null reference defects. Not only did they find several, but in many cases, the identification of a potential null reference indicated a broader problem with the design.
The second reference does not provide any specific evidence for the assertion that null reference errors are problem. But the authors do state that in their experience, these null reference errors are significant source of software defects. The paper then proceeds to explain how they try to eradicate these defects.
I also remembered seeing something about this in an announcement from ISE on a recent release of Eiffel. They refer to this issue as "void safety", and like so many things inspired or developed by Dr. Bertrand Meyer, they have an eloquent and educational description of the problem and how they go about preventing it in their language and tools. I recommend you read their article http://doc.eiffel.com/book/method/void-safety-background-definition-and-tools to learn more.
If you want to learn more about Microsoft code contracts, there are tons of articles that have arisen recently. You can also check my blog at http: SLASH SLASH codecontracts.info which is primarily devoted to conversations about software quality through the use of programming with contracts.
How to avoid Nullreference exception when searching in List
if the list does not contain a particular string it throws an error:
That is not correct. The exception is thrown, because you have a null
value inside your list! If all of your items in the list are valid strings then there will be no exception thrown. In this case FirstOrDefault
will simply return null
.
I think that I should somehow validate if the string first exists
You can check whether it is null the oldscool way first and combine it with an logical AND &&
with the Contains
method
string myListLine= myList.FirstOrDefault(stringToCheck =>
stringToCheck != null && stringToCheck.Contains("mystring "));
This way the second Contains
will not be evaluated if the first condition evaluates to FALSE
How to avoid null reference exceptions when iterating through IEnumerable datarow?
You can make a little extension method like this:
public static class MyExtensions {
public static string NullSafe(this string s){
if(s == null){
return "";
}
return s;
}
}
And then use it like this to convert null data to empty string:
dataRow.Field<string>("TranslateFrom").NullSafe().ToLower().Trim()
How to prevent null reference exception in this case
var ans = await client.GetStringAsync(uri);
JToken[] JSONResponseWeb = new JToken[1];
JToken[] JSONResponseImage = new JToken[1];
var responseResult = JToken.Parse(ans);
// Check if you have a null value anywhere in these parameters. This doesn't assume any handling for null values you'll have to figure that out on your end. Add en else statement and go to town.
if (responseResult?["webPages"]?["value"] != null)
{
JSONResponseWeb = responseResult ["webPages"]["value"].ToArray();
}
if (responseResult?["images"]?["value"] != null)
{
JSONResponseWeb = responseResult ["images"]["value"].ToArray();
}
How to avoid from exception on providing null model reference in View
.Equals()
cannot be used to compare with null
values, because if an object is null, it won't have the method .Equals()
! In your case, the method itself is throwing the Nullreference exception.
Just change to:
@if(Model.JoinedClasses != null) {
//...code
}
F# Options... do they really prevent Null Reference Exceptions
To add to the answer by Tomas, a major benefit of Option
type lies in the higher order functions that it supports, that give you more brevity and safety. You might find my blog post on the topic useful.
how to prevent null reference exception when doing a linear search of array?
In the first case, I think you just want to initialize the items with what the user has typed:
for (int i = 0; i < stuff.Length; i++)
{
Console.Write("Lägg till ett föremål: ");
stuff[i] = Console.ReadLine();
}
If you use +=
(to concatenate the current item with the user entry), an exception will be thrown if stuff[i]
is null.
In case 3, you have to check whether an item is null:
for (int i = 0; i < stuff.Length; i++)
{
if (stuff[i]?.ToUpper() == searchword.ToUpper())
Console.WriteLine("vi hittade" + stuff[i]);
}
Note that both issues would be fixed if you created the array this way:
string[] stuff = Enumerable.Repeat(string.Empty, 200).ToArray();
How to avoid Null reference exception when datagrid rows are not visible within grid?
if (((CheckBox)gridView.Columns[0].GetCellContent(row)).IsChecked != null)
{
bool IsChecked = (bool)((CheckBox)gridView.Columns[0].GetCellContent(row)).IsChecked;
if (IsChecked)
{
var part = ((TextBlock)gridView.Columns[1].GetCellContent(row)).Text;
var code = ((TextBlock)gridView.Columns[2].GetCellContent(row)).Text;
var um = ((TextBlock)gridView.Columns[3].GetCellContent(row)).Text;
addToPartList(part, code, um);
checkCount += 1;
}
}
As @Sievajet suggested, I added a check for nulls before the conversion and resolved the issue. This no longer throws a null reference exception.
What is a NullReferenceException, and how do I fix it?
What is the cause?
Bottom Line
You are trying to use something that is null
(or Nothing
in VB.NET). This means you either set it to null
, or you never set it to anything at all.
Like anything else, null
gets passed around. If it is null
in method "A", it could be that method "B" passed a null
to method "A".
null
can have different meanings:
- Object variables that are uninitialized and hence point to nothing. In this case, if you access members of such objects, it causes a
NullReferenceException
. - The developer is using
null
intentionally to indicate there is no meaningful value available. Note that C# has the concept of nullable datatypes for variables (like database tables can have nullable fields) - you can assignnull
to them to indicate there is no value stored in it, for exampleint? a = null;
(which is a shortcut forNullable<int> a = null;
) where the question mark indicates it is allowed to storenull
in variablea
. You can check that either withif (a.HasValue) {...}
or withif (a==null) {...}
. Nullable variables, likea
this example, allow to access the value viaa.Value
explicitly, or just as normal viaa
.
Note that accessing it viaa.Value
throws anInvalidOperationException
instead of aNullReferenceException
ifa
isnull
- you should do the check beforehand, i.e. if you have another non-nullable variableint b;
then you should do assignments likeif (a.HasValue) { b = a.Value; }
or shorterif (a != null) { b = a; }
.
The rest of this article goes into more detail and shows mistakes that many programmers often make which can lead to a NullReferenceException
.
More Specifically
The runtime
throwing a NullReferenceException
always means the same thing: you are trying to use a reference, and the reference is not initialized (or it was once initialized, but is no longer initialized).
This means the reference is null
, and you cannot access members (such as methods) through a null
reference. The simplest case:
string foo = null;
foo.ToUpper();
This will throw a NullReferenceException
at the second line because you can't call the instance method ToUpper()
on a string
reference pointing to null
.
Debugging
How do you find the source of a NullReferenceException
? Apart from looking at the exception itself, which will be thrown exactly at the location where it occurs, the general rules of debugging in Visual Studio apply: place strategic breakpoints and inspect your variables, either by hovering the mouse over their names, opening a (Quick)Watch window or using the various debugging panels like Locals and Autos.
If you want to find out where the reference is or isn't set, right-click its name and select "Find All References". You can then place a breakpoint at every found location and run your program with the debugger attached. Every time the debugger breaks on such a breakpoint, you need to determine whether you expect the reference to be non-null, inspect the variable, and verify that it points to an instance when you expect it to.
By following the program flow this way, you can find the location where the instance should not be null, and why it isn't properly set.
Examples
Some common scenarios where the exception can be thrown:
Generic
ref1.ref2.ref3.member
If ref1 or ref2 or ref3 is null, then you'll get a NullReferenceException
. If you want to solve the problem, then find out which one is null by rewriting the expression to its simpler equivalent:
var r1 = ref1;
var r2 = r1.ref2;
var r3 = r2.ref3;
r3.member
Specifically, in HttpContext.Current.User.Identity.Name
, the HttpContext.Current
could be null, or the User
property could be null, or the Identity
property could be null.
Indirect
public class Person
{
public int Age { get; set; }
}
public class Book
{
public Person Author { get; set; }
}
public class Example
{
public void Foo()
{
Book b1 = new Book();
int authorAge = b1.Author.Age; // You never initialized the Author property.
// there is no Person to get an Age from.
}
}
If you want to avoid the child (Person) null reference, you could initialize it in the parent (Book) object's constructor.
Nested Object Initializers
The same applies to nested object initializers:
Book b1 = new Book
{
Author = { Age = 45 }
};
This translates to:
Book b1 = new Book();
b1.Author.Age = 45;
While the new
keyword is used, it only creates a new instance of Book
, but not a new instance of Person
, so the Author
the property is still null
.
Nested Collection Initializers
public class Person
{
public ICollection<Book> Books { get; set; }
}
public class Book
{
public string Title { get; set; }
}
The nested collection Initializers
behave the same:
Person p1 = new Person
{
Books = {
new Book { Title = "Title1" },
new Book { Title = "Title2" },
}
};
This translates to:
Person p1 = new Person();
p1.Books.Add(new Book { Title = "Title1" });
p1.Books.Add(new Book { Title = "Title2" });
The new Person
only creates an instance of Person
, but the Books
collection is still null
. The collection Initializer
syntax does not create a collection
for p1.Books
, it only translates to the p1.Books.Add(...)
statements.
Array
int[] numbers = null;
int n = numbers[0]; // numbers is null. There is no array to index.
Array Elements
Person[] people = new Person[5];
people[0].Age = 20 // people[0] is null. The array was allocated but not
// initialized. There is no Person to set the Age for.
Jagged Arrays
long[][] array = new long[1][];
array[0][0] = 3; // is null because only the first dimension is yet initialized.
// Use array[0] = new long[2]; first.
Collection/List/Dictionary
Dictionary<string, int> agesForNames = null;
int age = agesForNames["Bob"]; // agesForNames is null.
// There is no Dictionary to perform the lookup.
Range Variable (Indirect/Deferred)
public class Person
{
public string Name { get; set; }
}
var people = new List<Person>();
people.Add(null);
var names = from p in people select p.Name;
string firstName = names.First(); // Exception is thrown here, but actually occurs
// on the line above. "p" is null because the
// first element we added to the list is null.
Events (C#)
public class Demo
{
public event EventHandler StateChanged;
protected virtual void OnStateChanged(EventArgs e)
{
StateChanged(this, e); // Exception is thrown here
// if no event handlers have been attached
// to StateChanged event
}
}
(Note: The VB.NET compiler inserts null checks for event usage, so it's not necessary to check events for Nothing
in VB.NET.)
Bad Naming Conventions:
If you named fields differently from locals, you might have realized that you never initialized the field.
public class Form1
{
private Customer customer;
private void Form1_Load(object sender, EventArgs e)
{
Customer customer = new Customer();
customer.Name = "John";
}
private void Button_Click(object sender, EventArgs e)
{
MessageBox.Show(customer.Name);
}
}
This can be solved by following the convention to prefix fields with an underscore:
private Customer _customer;
ASP.NET Page Life cycle:
public partial class Issues_Edit : System.Web.UI.Page
{
protected TestIssue myIssue;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Only called on first load, not when button clicked
myIssue = new TestIssue();
}
}
protected void SaveButton_Click(object sender, EventArgs e)
{
myIssue.Entry = "NullReferenceException here!";
}
}
ASP.NET Session Values
// if the "FirstName" session value has not yet been set,
// then this line will throw a NullReferenceException
string firstName = Session["FirstName"].ToString();
ASP.NET MVC empty view models
If the exception occurs when referencing a property of @Model
in an ASP.NET MVC View
, you need to understand that the Model
gets set in your action method, when you return
a view. When you return an empty model (or model property) from your controller, the exception occurs when the views access it:
// Controller
public class Restaurant:Controller
{
public ActionResult Search()
{
return View(); // Forgot the provide a Model here.
}
}
// Razor view
@foreach (var restaurantSearch in Model.RestaurantSearch) // Throws.
{
}
<p>@Model.somePropertyName</p> <!-- Also throws -->
WPF Control Creation Order and Events
WPF
controls are created during the call to InitializeComponent
in the order they appear in the visual tree. A NullReferenceException
will be raised in the case of early-created controls with event handlers, etc., that fire during InitializeComponent
which reference late-created controls.
For example:
<Grid>
<!-- Combobox declared first -->
<ComboBox Name="comboBox1"
Margin="10"
SelectedIndex="0"
SelectionChanged="comboBox1_SelectionChanged">
<ComboBoxItem Content="Item 1" />
<ComboBoxItem Content="Item 2" />
<ComboBoxItem Content="Item 3" />
</ComboBox>
<!-- Label declared later -->
<Label Name="label1"
Content="Label"
Margin="10" />
</Grid>
Here comboBox1
is created before label1
. If comboBox1_SelectionChanged
attempts to reference `label1, it will not yet have been created.
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
label1.Content = comboBox1.SelectedIndex.ToString(); // NullReferenceException here!!
}
Changing the order of the declarations in the XAML
(i.e., listing label1
before comboBox1
, ignoring issues of design philosophy) would at least resolve the NullReferenceException
here.
Cast with as
var myThing = someObject as Thing;
This doesn't throw an InvalidCastException
but returns a null
when the cast fails (and when someObject
is itself null). So be aware of that.
LINQ FirstOrDefault()
and SingleOrDefault()
The plain versions First()
and Single()
throw exceptions when there is nothing. The "OrDefault" versions return null
in that case. So be aware of that.
foreach
foreach
throws when you try to iterate on a null
collection. Usually caused by unexpected null
result from methods that return collections.
List<int> list = null;
foreach(var v in list) { } // NullReferenceException here
More realistic example - select nodes from XML document. Will throw if nodes are not found but initial debugging shows that all properties valid:
foreach (var node in myData.MyXml.DocumentNode.SelectNodes("//Data"))
Ways to Avoid
Explicitly check for null
and ignore null
values.
If you expect the reference sometimes to be null
, you can check for it being null
before accessing instance members:
void PrintName(Person p)
{
if (p != null)
{
Console.WriteLine(p.Name);
}
}
Explicitly check for null
and provide a default value.
Methods you call expecting an instance can return null
, for example when the object being sought cannot be found. You can choose to return a default value when this is the case:
string GetCategory(Book b)
{
if (b == null)
return "Unknown";
return b.Category;
}
Explicitly check for null
from method calls and throw a custom exception.
You can also throw a custom exception, only to catch it in the calling code:
string GetCategory(string bookTitle)
{
var book = library.FindBook(bookTitle); // This may return null
if (book == null)
throw new BookNotFoundException(bookTitle); // Your custom exception
return book.Category;
}
Use Debug.Assert
if a value should never be null
, to catch the problem earlier than the exception occurs.
When you know during development that a method could, but never should return null
, you can use Debug.Assert()
to break as soon as possible when it does occur:
string GetTitle(int knownBookID)
{
// You know this should never return null.
var book = library.GetBook(knownBookID);
// Exception will occur on the next line instead of at the end of this method.
Debug.Assert(book != null, "Library didn't return a book for known book ID.");
// Some other code
return book.Title; // Will never throw NullReferenceException in Debug mode.
}
Though this check will not end up in your release build, causing it to throw the NullReferenceException
again when book == null
at runtime in release mode.
Use GetValueOrDefault()
for nullable
value types to provide a default value when they are null
.
DateTime? appointment = null;
Console.WriteLine(appointment.GetValueOrDefault(DateTime.Now));
// Will display the default value provided (DateTime.Now), because appointment is null.
appointment = new DateTime(2022, 10, 20);
Console.WriteLine(appointment.GetValueOrDefault(DateTime.Now));
// Will display the appointment date, not the default
Use the null coalescing operator: ??
[C#] or If()
[VB].
The shorthand to providing a default value when a null
is encountered:
IService CreateService(ILogger log, Int32? frobPowerLevel)
{
var serviceImpl = new MyService(log ?? NullLog.Instance);
// Note that the above "GetValueOrDefault()" can also be rewritten to use
// the coalesce operator:
serviceImpl.FrobPowerLevel = frobPowerLevel ?? 5;
}
Use the null condition operator: ?.
or ?[x]
for arrays (available in C# 6 and VB.NET 14):
This is also sometimes called the safe navigation or Elvis (after its shape) operator. If the expression on the left side of the operator is null, then the right side will not be evaluated, and null is returned instead. That means cases like this:
var title = person.Title.ToUpper();
If the person does not have a title, this will throw an exception because it is trying to call ToUpper
on a property with a null value.
In C# 5
and below, this can be guarded with:
var title = person.Title == null ? null : person.Title.ToUpper();
Now the title variable will be null instead of throwing an exception. C# 6 introduces a shorter syntax for this:
var title = person.Title?.ToUpper();
This will result in the title variable being null
, and the call to ToUpper
is not made if person.Title
is null
.
Of course, you still have to check title
for null
or use the null condition operator together with the null coalescing operator (??
) to supply a default value:
// regular null check
int titleLength = 0;
if (title != null)
titleLength = title.Length; // If title is null, this would throw NullReferenceException
// combining the `?` and the `??` operator
int titleLength = title?.Length ?? 0;
Likewise, for arrays you can use ?[i]
as follows:
int[] myIntArray = null;
var i = 5;
int? elem = myIntArray?[i];
if (!elem.HasValue) Console.WriteLine("No value");
This will do the following: If myIntArray
is null
, the expression returns null
and you can safely check it. If it contains an array, it will do the same as:elem = myIntArray[i];
and returns the ith element.
Use null context (available in C# 8):
Introduced in C# 8
, null contexts and nullable reference types perform static analysis on variables and provide a compiler warning if a value can be potentially null
or have been set to null
. The nullable reference types allow types to be explicitly allowed to be null
.
Related Topics
Is There a .Net Way to Enumerate All Available Network Printers
Resharper Complains When Method Can Be Static, But Isn'T
"The Linq Expression Node Type 'Invoke' Is Not Supported in Linq to Entities" - Stumped!
Wpf Binding - Default Value for Empty String
Outofmemoryexception When I Read 500Mb Filestream
How to Copy Value from Class X to Class Y with the Same Property Name in C#
Reasons for Why a Winforms Label Does Not Want to Be Transparent
Benefits of Using Async and Await Keywords
Controls in Container Form Come Over Child Form
Are Ienumerable Linq Methods Thread-Safe
How to Put Text on Progressbar
Equivalent Code of Createobject in C#
How to Remove a Single Attribute (E.G. Readonly) from a File
Trace Listener to Write to a Text Box (Wpf Application)