What is a NullReferenceException, and how do I fix it?











1878















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?









share















locked by Bhargav Rao Dec 19 '17 at 6:38


This question's answers are a collaborative effort: if you see something that can be improved, just edit the answer to improve it! No additional answers can be added here
















  • The exception helper in VS 2017 will be more helpful in diagnosing the cause of this exception -- blogs.msdn.microsoft.com/visualstudio/2016/11/28/… under New Exception Helper.

    – Zev Spitz
    Dec 29 '16 at 9:06













  • Cant we just say "object has not been initialized"? So if you have a variable declaration: SomeClass myVariable; this will create myVariable with reference to SomeClass, but it is Not initialized and will be equal to null. You have to invoke the class initialization by doing SomeClass myVariable = new SomeClass(); Or, if you are returning a reference to another variable: SomeClass myVariable = anotherVariableDeclared();

    – Arvin Amir
    Jun 7 '17 at 0:06








  • 1





    @Arvin what about `MfClass

    – John Saunders
    Jun 7 '17 at 18:15











  • Dear future visitors, the answers to this question equally apply to an ArgumentNullException. If your question has been closed as a duplicate of this one, and you are experiencing an ANE, please follow the directions in the answers to debug and fix your problem.

    – Will
    Oct 13 '17 at 17:56











  • @will ANE should only happen if a null is passed as a parameter. Can you give an example if an ANE question closed as a duplicate of this one?

    – John Saunders
    Oct 14 '17 at 21:44
















1878















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?









share















locked by Bhargav Rao Dec 19 '17 at 6:38


This question's answers are a collaborative effort: if you see something that can be improved, just edit the answer to improve it! No additional answers can be added here
















  • The exception helper in VS 2017 will be more helpful in diagnosing the cause of this exception -- blogs.msdn.microsoft.com/visualstudio/2016/11/28/… under New Exception Helper.

    – Zev Spitz
    Dec 29 '16 at 9:06













  • Cant we just say "object has not been initialized"? So if you have a variable declaration: SomeClass myVariable; this will create myVariable with reference to SomeClass, but it is Not initialized and will be equal to null. You have to invoke the class initialization by doing SomeClass myVariable = new SomeClass(); Or, if you are returning a reference to another variable: SomeClass myVariable = anotherVariableDeclared();

    – Arvin Amir
    Jun 7 '17 at 0:06








  • 1





    @Arvin what about `MfClass

    – John Saunders
    Jun 7 '17 at 18:15











  • Dear future visitors, the answers to this question equally apply to an ArgumentNullException. If your question has been closed as a duplicate of this one, and you are experiencing an ANE, please follow the directions in the answers to debug and fix your problem.

    – Will
    Oct 13 '17 at 17:56











  • @will ANE should only happen if a null is passed as a parameter. Can you give an example if an ANE question closed as a duplicate of this one?

    – John Saunders
    Oct 14 '17 at 21:44














1878












1878








1878


397






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?









share
















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?







c# .net vb.net null nullreferenceexception





share














share












share



share








edited Sep 3 '17 at 16:06


























community wiki





22 revs, 16 users 40%
John Saunders





locked by Bhargav Rao Dec 19 '17 at 6:38


This question's answers are a collaborative effort: if you see something that can be improved, just edit the answer to improve it! No additional answers can be added here






locked by Bhargav Rao Dec 19 '17 at 6:38


This question's answers are a collaborative effort: if you see something that can be improved, just edit the answer to improve it! No additional answers can be added here















  • The exception helper in VS 2017 will be more helpful in diagnosing the cause of this exception -- blogs.msdn.microsoft.com/visualstudio/2016/11/28/… under New Exception Helper.

    – Zev Spitz
    Dec 29 '16 at 9:06













  • Cant we just say "object has not been initialized"? So if you have a variable declaration: SomeClass myVariable; this will create myVariable with reference to SomeClass, but it is Not initialized and will be equal to null. You have to invoke the class initialization by doing SomeClass myVariable = new SomeClass(); Or, if you are returning a reference to another variable: SomeClass myVariable = anotherVariableDeclared();

    – Arvin Amir
    Jun 7 '17 at 0:06








  • 1





    @Arvin what about `MfClass

    – John Saunders
    Jun 7 '17 at 18:15











  • Dear future visitors, the answers to this question equally apply to an ArgumentNullException. If your question has been closed as a duplicate of this one, and you are experiencing an ANE, please follow the directions in the answers to debug and fix your problem.

    – Will
    Oct 13 '17 at 17:56











  • @will ANE should only happen if a null is passed as a parameter. Can you give an example if an ANE question closed as a duplicate of this one?

    – John Saunders
    Oct 14 '17 at 21:44



















  • The exception helper in VS 2017 will be more helpful in diagnosing the cause of this exception -- blogs.msdn.microsoft.com/visualstudio/2016/11/28/… under New Exception Helper.

    – Zev Spitz
    Dec 29 '16 at 9:06













  • Cant we just say "object has not been initialized"? So if you have a variable declaration: SomeClass myVariable; this will create myVariable with reference to SomeClass, but it is Not initialized and will be equal to null. You have to invoke the class initialization by doing SomeClass myVariable = new SomeClass(); Or, if you are returning a reference to another variable: SomeClass myVariable = anotherVariableDeclared();

    – Arvin Amir
    Jun 7 '17 at 0:06








  • 1





    @Arvin what about `MfClass

    – John Saunders
    Jun 7 '17 at 18:15











  • Dear future visitors, the answers to this question equally apply to an ArgumentNullException. If your question has been closed as a duplicate of this one, and you are experiencing an ANE, please follow the directions in the answers to debug and fix your problem.

    – Will
    Oct 13 '17 at 17:56











  • @will ANE should only happen if a null is passed as a parameter. Can you give an example if an ANE question closed as a duplicate of this one?

    – John Saunders
    Oct 14 '17 at 21:44

















The exception helper in VS 2017 will be more helpful in diagnosing the cause of this exception -- blogs.msdn.microsoft.com/visualstudio/2016/11/28/… under New Exception Helper.

– Zev Spitz
Dec 29 '16 at 9:06







The exception helper in VS 2017 will be more helpful in diagnosing the cause of this exception -- blogs.msdn.microsoft.com/visualstudio/2016/11/28/… under New Exception Helper.

– Zev Spitz
Dec 29 '16 at 9:06















Cant we just say "object has not been initialized"? So if you have a variable declaration: SomeClass myVariable; this will create myVariable with reference to SomeClass, but it is Not initialized and will be equal to null. You have to invoke the class initialization by doing SomeClass myVariable = new SomeClass(); Or, if you are returning a reference to another variable: SomeClass myVariable = anotherVariableDeclared();

– Arvin Amir
Jun 7 '17 at 0:06







Cant we just say "object has not been initialized"? So if you have a variable declaration: SomeClass myVariable; this will create myVariable with reference to SomeClass, but it is Not initialized and will be equal to null. You have to invoke the class initialization by doing SomeClass myVariable = new SomeClass(); Or, if you are returning a reference to another variable: SomeClass myVariable = anotherVariableDeclared();

– Arvin Amir
Jun 7 '17 at 0:06






1




1





@Arvin what about `MfClass

– John Saunders
Jun 7 '17 at 18:15





@Arvin what about `MfClass

– John Saunders
Jun 7 '17 at 18:15













Dear future visitors, the answers to this question equally apply to an ArgumentNullException. If your question has been closed as a duplicate of this one, and you are experiencing an ANE, please follow the directions in the answers to debug and fix your problem.

– Will
Oct 13 '17 at 17:56





Dear future visitors, the answers to this question equally apply to an ArgumentNullException. If your question has been closed as a duplicate of this one, and you are experiencing an ANE, please follow the directions in the answers to debug and fix your problem.

– Will
Oct 13 '17 at 17:56













@will ANE should only happen if a null is passed as a parameter. Can you give an example if an ANE question closed as a duplicate of this one?

– John Saunders
Oct 14 '17 at 21:44





@will ANE should only happen if a null is passed as a parameter. Can you give an example if an ANE question closed as a duplicate of this one?

– John Saunders
Oct 14 '17 at 21:44












31 Answers
31






active

oldest

votes













1 2
next












2185














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:




  1. Object variables which are uninitialized and hence point to nothing. In this case, if you access properties or methods of such objects, it causes a NullReferenceException.

  2. 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 assign null to them to indicate there is no value stored in it, for example int? a = null; where the question mark indicates it is allowed to store null in variable a. You can check that either with if (a.HasValue) {...} or with if (a==null) {...}. Nullable variables, like a this example, allow to access the value via a.Value explicitly, or just as normal via a.
    Note that accessing it via a.Value throws an InvalidOperationException instead of a NullReferenceException if a is null - you should do the check beforehand, i.e. if you have another on-nullable variable int b; then you should do assignments like if (a.HasValue) { b = a.Value; } or shorter if (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 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



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


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(); // NullReference 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 null collection. Usually caused by unexpected null result from methods that return collections.



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


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 call you expect to return 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 maybe can, 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.



Special techniques for debugging and fixing null derefs in iterators



C# supports "iterator blocks" (called "generators" in some other popular languages). Null dereference exceptions can be particularly tricky to debug in iterator blocks because of deferred execution:



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


If whatever results in null then MakeFrob will throw. Now, you might think that the right thing to do is this:



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


Why is this wrong? Because the iterator block does not actually run until the foreach! The call to GetFrobs simply returns an object which when iterated will run the iterator block.



By writing a null check like this you prevent the null dereference, but you move the null argument exception to the point of the iteration, not to the point of the call, and that is very confusing to debug.



The correct fix is:



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


That is, make a private helper method that has the iterator block logic, and a public surface method that does the null check and returns the iterator. Now when GetFrobs is called, the null check happens immediately, and then GetFrobsForReal executes when the sequence is iterated.



If you examine the reference source for LINQ to Objects you will see that this technique is used throughout. It is slightly more clunky to write, but it makes debugging nullity errors much easier. Optimize your code for the convenience of the caller, not the convenience of the author.



A note on null dereferences in unsafe code



C# has an "unsafe" mode which is, as the name implies, extremely dangerous because the normal safety mechanisms which provide memory safety and type safety are not enforced. You should not be writing unsafe code unless you have a thorough and deep understanding of how memory works.



In unsafe mode, you should be aware of two important facts:




  • dereferencing a null pointer produces the same exception as dereferencing a null reference

  • dereferencing an invalid non-null pointer can produce that exception
    in some circumstances


To understand why that is, it helps to understand how .NET produces null dereference exceptions in the first place. (These details apply to .NET running on Windows; other operating systems use similar mechanisms.)



Memory is virtualized in Windows; each process gets a virtual memory space of many "pages" of memory that are tracked by the operating system. Each page of memory has flags set on it which determine how it may be used: read from, written to, executed, and so on. The lowest page is marked as "produce an error if ever used in any way".



Both a null pointer and a null reference in C# are internally represented as the number zero, and so any attempt to dereference it into its corresponding memory storage causes the operating system to produce an error. The .NET runtime then detects this error and turns it into the null dereference exception.



That's why dereferencing both a null pointer and a null reference produces the same exception.



What about the second point? Dereferencing any invalid pointer that falls in the lowest page of virtual memory causes the same operating system error, and thereby the same exception.



Why does this make sense? Well, suppose we have a struct containing two ints, and an unmanaged pointer equal to null. If we attempt to dereference the second int in the struct, the CLR will not attempt to access the storage at location zero; it will access the storage at location four. But logically this is a null dereference because we are getting to that address via the null.



If you are working with unsafe code and you get a null dereference exception, just be aware that the offending pointer need not be null. It can be any location in the lowest page, and this exception will be produced.





share





















  • 49





    Maybe this is a dumb comment but wouldnt the first and best way to avoid this problem be to initialize the object? For me if this error occurs it is usually because I forgot to initialize something like the array element. I think it is far less common to define the object as null and then reference it. Maybe give the way to solve each problem adjacent to the description. Still a good post.

    – JPK
    May 20 '14 at 6:39






  • 28





    What if there is no object, but rather the return value from a method or property?

    – John Saunders
    May 20 '14 at 6:41






  • 6





    The book/author example is a little weird.... How does that even compile? How does intellisense even work? What is this I'm not good with computar...

    – Will
    Sep 8 '14 at 18:26






  • 5





    @Will: does my last edit help? If not, then please be more explicit about what you see as a problem.

    – John Saunders
    Sep 8 '14 at 18:41






  • 6





    @JohnSaunders Oh, no, sorry, I meant the object initializer version of that. new Book { Author = { Age = 45 } }; How does the inner initialization even... I can't think of a situation where inner init would ever work, yet it compiles and intellisense works... Unless for structs?

    – Will
    Sep 8 '14 at 18:44





















289














NullReference Exception — Visual Basic



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



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



Note:




  1. This is concept-based: there is no code for you to paste into your project. It is intended to help you understand what causes a NullReferenceException (NRE), how to find it, how to fix it, and how to avoid it. An NRE can be caused many ways so this is unlikely to be your sole encounter.

  2. The examples (from Stack  Overflow posts) do not always show the best way to do something in the first place.

  3. Typically, the simplest remedy is used.


Basic Meaning



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




  • Your code declared an object variable, but it did not initialize it (create an instance or 'instantiate' it)

  • Something which your code assumed would initialize an object, did not

  • Possibly, other code prematurely invalidated an object still in use


Finding The Cause



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



IDE debug display



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



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



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



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



See also:




  • Breakpoints

  • MSDN: How to: Use the Try/Catch Block to Catch Exceptions

  • MSDN: Best Practices for Exceptions


Examples and Remedies



Class Objects / Creating an Instance





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


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



Remedy



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



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

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


When it is only appropriate to create the instance later:



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


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



Private reg As CashRegister
'...

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


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




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



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




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



For more information, see:




  • New Operator

  • Scope in Visual Basic

  • Access Levels in Visual Basic

  • Value Types and Reference Types




Arrays



Arrays must also be instantiated:



Private arr as String()


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



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

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


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



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


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



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


Example: Array of class objects



Dim arrFoo(5) As Foo

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


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



Remedy



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


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



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

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


For more information, see:




  • Option Infer Statement

  • Scope in Visual Basic

  • Arrays in Visual Basic




Lists and Collections



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



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


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



myList = New List(Of String)

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


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



Public Class Foo
Private barList As List(Of Bar)

Friend Function BarCount As Integer
Return barList.Count
End Function

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


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



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


As before, this is incorrect:



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


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





Data Provider Objects



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



Example 1



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

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

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


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



img



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



Remedy



Dim ds As New DataSet


Example 2



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

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


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



Remedy



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



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


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



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


Example 3



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

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


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



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



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


See also DataTable Class.





Object Paths / Nested



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


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



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


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



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


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



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


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





UI Controls



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


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



Remedy



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



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

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


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





Visual Basic Forms



Public Class Form1

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

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

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


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



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




  • Lists and Collection will simply be empty

  • The Array will contain five elements of Nothing

  • The somevar assignment will result in an immediate NRE because Nothing doesn't have a .Text property


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



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



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


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



Public Class Form1

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


Partial Remedy



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



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

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


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



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



"Button2" resides on a Panel



Remedy



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



' Declaration
Private NameBoxes As TextBox()

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

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




Function Returning Nothing



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

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

Return bars
End Function


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



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


Remedy



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



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




Poorly Implemented Try/Catch



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



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

'More code fooing and barring

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

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


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



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



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



Remedy



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





DBNull is not the same as Nothing



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


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




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




Remedy



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


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



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


Example 2



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

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


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



If getFoo IsNot Nothing Then...




Controls



Dim chk As CheckBox

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


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



Remedy



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


The DataGridView



The DGV has a few quirks seen periodically:



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


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



Remedy



Name the columns manually, or reference by index:



dgvBooks.Columns(0).Visible = True


Example 2 — Beware of the NewRow



xlWorkSheet = xlWorkBook.Sheets("sheet1")

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


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



Remedy



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



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


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





My.Settings (StringCollection)



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



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


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



Remedy



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



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


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





Key Points



You probably forgot the New operator.



or



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



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





MSDN NullReference Exception





share

































    220














    Another scenario is when you cast a null object into a value type. For example, the code below:



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


    It will throw a NullReferenceException on the cast. It seems quite obvious in the above sample, but this can happen in more "late-binding" intricate scenarios where the null object has been returned from some code you don't own, and the cast is for example generated by some automatic system.



    One example of this is this simple ASP.NET binding fragment with the Calendar control:



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


    Here, SelectedDate is in fact a property - of DateTime type - of the Calendar Web Control type, and the binding could perfectly return something null. The implicit ASP.NET Generator will create a piece of code that will be equivalent to the cast code above. And this will raise a NullReferenceException that is quite difficult to spot, because it lies in ASP.NET generated code which compiles fine...





    share





















    • 7





      Great catch. One-liner way to avoid: DateTime x = (DateTime) o as DateTime? ?? defaultValue;

      – Serge Shultz
      Jun 29 '15 at 11:07



















    148














    It means that the variable in question is pointed at nothing. I could generate this like so:



    SqlConnection connection = null;
    connection.Open();


    That will throw the error because while I've declared the variable "connection", it's not pointed to anything. When I try to call the member "Open", there's no reference for it to resolve, and it will throw the error.



    To avoid this error:




    1. Always initialize your objects before you try to do anything with them.

    2. If you're not sure whether the object is null, check it with object == null.


    JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error, allowing you to put in a null check. This error is the number one source of bugs, IMHO.





    share





















    • 2





      JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error. This is incorrect. I have a solution without that detection, yet the code occasionally results to the exception. I suspect it's occasionally undetectable - by them at least - when multithreading is involved, but I can't comment further because I didn't identify the location of my bug yet.

      – j riv
      Jan 21 '18 at 7:42













    • But how to solve it when the NullReferenceException comes in usign HttpContext.Current.Responce.Clear(). It is not getting solved by any of the above solution. because while creating its object object of HttpContext then a error comes "Overload resolution failed because no accessible 'New' accepts this Number of arguments.

      – Sunny Sandeep
      Feb 2 '18 at 11:14





















    139














    It means your code used an object reference variable that was set to null (i.e. it did not reference an actual object instance).



    To prevent the error, objects that could be null should be tested for null before being used.



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




    share

































      93














      Be aware that regardless of the scenario, the cause is always the same in .NET:




      You are trying to use a reference variable whose value is Nothing/null. When the value is Nothing/null for the reference variable, that means it is not actually holding a reference to an instance of any object that exists on the heap.



      You either never assigned something to the variable, never created an instance of the value assigned to the variable, or you set the variable equal to Nothing/null manually, or you called a function that set the variable to Nothing/null for you.






      share

































        82














        An example of this exception being thrown is: When you are trying to check something, that is null.



        For example:



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

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


        The .NET runtime will throw a NullReferenceException when you attempt to perform an action on something which hasn't been instantiated i.e. the code above.



        In comparison to an ArgumentNullException which is typically thrown as a defensive measure if a method expects that what is being passed to it is not null.



        More information is in C# NullReferenceException and Null Parameter.





        share

































          79














          If you have not initialized a reference type, and you want to set or read one of its properties, it will throw a NullReferenceException.



          Example:



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


          You can simply avoid this by checking if the variable is not null:



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


          To fully understand why a NullReferenceException is thrown, it is important to know the difference between value types and reference types.



          So, if you're dealing with value types, NullReferenceExceptions can not occur. Though you need to keep alert when dealing with reference types!



          Only reference types, as the name is suggesting, can hold references or point literally to nothing (or 'null'). Whereas value types always contain a value.



          Reference types (these ones must be checked):




          • dynamic

          • object

          • string


          Value types (you can simply ignore these ones):




          • Numeric types

          • Integral types

          • Floating-point types

          • decimal

          • bool

          • User defined structs





          share





















          • 5





            -1: since the question is "What is a NullReferenceException", value types are not relevant.

            – John Saunders
            May 16 '13 at 22:00






          • 18





            @John Saunders: I disagree. As a software developer it is really important to be able to distinguish between value and reference types. else people will end up checking if integers are null.

            – Fabian Bigler
            May 16 '13 at 22:28








          • 4





            True, just not in the context of this question.

            – John Saunders
            May 16 '13 at 22:44






          • 3





            Thanks for the hint. I improved it a bit and added an example at the top. I still think mentioning Reference & Value Types is useful.

            – Fabian Bigler
            May 16 '13 at 23:02






          • 4





            I think you haven't added anything that wasn't in the other answers, since the question pre-supposes a reference type.

            – John Saunders
            May 18 '13 at 23:24



















          70














          Another case where NullReferenceExceptions can happen is the (incorrect) use of the as operator:



          class Book {
          public string Name { get; set; }
          }
          class Car { }

          Car mycar = new Car();
          Book mybook = mycar as Book; // Incompatible conversion --> mybook = null

          Console.WriteLine(mybook.Name); // NullReferenceException


          Here, Book and Car are incompatible types; a Car cannot be converted/cast to a Book. When this cast fails, as returns null. Using mybook after this causes a NullReferenceException.



          In general, you should use a cast or as, as follows:



          If you are expecting the type conversion to always succeed (ie. you know what the object should be ahead of time), then you should use a cast:



          ComicBook cb = (ComicBook)specificBook;


          If you are unsure of the type, but you want to try to use it as a specific type, then use as:



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




          share





















          • 2





            This can happen a lot when unboxing a variable. I find it happens often in event handlers after I changed the type of the UI element but forget to update the code-behind.

            – Brendan
            Feb 19 '14 at 0:24





















          62














          You are using the object that contains the null value reference. So it's giving a null exception. In the example the string value is null and when checking its length, the exception occurred.



          Example:



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


          The exception error is:




          Unhandled Exception:



          System.NullReferenceException: Object reference not set to an instance
          of an object. at Program.Main()






          share





















          • 1





            How profound! I never considered the 'null' constant a reference value. So this is how C# abstracts a "NullPointer" huh ? B/c as I recall in C++, a NPE can be caused by dereferencing an uninitialized pointer (ie, ref type in c#) whose default value happens to be an address that is not allocated to that process (many cases this would be 0, especially in later versions of C++ that did auto-initialization, which belongs to the OS - f with it and die beeotch (or just catch the sigkill the OS attacks your process with)).

            – samis
            Jul 31 '13 at 18:55





















          51














          Simon Mourier gave this example:



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


          where an unboxing conversion (cast) from object (or from one of the classes System.ValueType or System.Enum, or from an interface type) to a value type (other than Nullable<>) in itself gives the NullReferenceException.



          In the other direction, a boxing conversion from a Nullable<> which has HasValue equal to false to a reference type, can give a null reference which can then later lead to a NullReferenceException. The classic example is:



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


          Sometimes the boxing happens in another way. For example with this non-generic extension method:



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


          the following code will be problematic:



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


          These cases arise because of the special rules the runtime uses when boxing Nullable<> instances.





          share

































            51














            While what causes a NullReferenceExceptions and approaches to avoid/fix such an exception have been addressed in other answers, what many programmers haven't learned yet is how to independently debug such exceptions during development.



            In Visual Studio this is usually easy thanks to the Visual Studio Debugger.





            First, make sure that the correct error is going to be caught - see
            How do I allow breaking on 'System.NullReferenceException' in VS2010? Note1



            Then either Start with Debugging (F5) or Attach [the VS Debugger] to Running Process. On occasion it may be useful to use Debugger.Break, which will prompt to launch the debugger.



            Now, when the NullReferenceException is thrown (or unhandled) the debugger will stop (remember the rule set above?) on the line on which the exception occurred. Sometimes the error will be easy to spot.



            For instance,
            in the following line the only code that can cause the exception is if myString evaluates to null. This can be verified by looking at the Watch Window or running expressions in the Immediate Window.



            var x = myString.Trim();


            In more advanced cases, such as the following, you'll need to use one of the techniques above (Watch or Immediate Windows) to inspect the expressions to determine if str1 was null or if str2 was null.



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


            Once where the exception is throw has been located, it's usually trivial to reason backwards to find out where the null value was [incorrectly] introduced --



            Take the time required to understand the cause of the exception. Inspect for null expressions. Inspect the previous expressions which could have resulted in such null expressions. Add breakpoints and step through the program as appropriate. Use the debugger.





            1 If Break on Throws is too aggressive and the debugger stops on an NPE in the .NET or 3rd-party library, Break on User-Unhandled can be used to limit the exceptions caught. Additionally, VS2012 introduces Just My Code which I recommend enabling as well.




            If you are debugging with Just My Code enabled, the behavior is slightly different. With Just My Code enabled, the debugger ignores first-chance common language runtime (CLR) exceptions that are thrown outside of My Code and do not pass through My Code






            share

































              39














              Adding a case when the class name for entity used in entity framework is same as class name for a web form code-behind file.



              Suppose you have a web form Contact.aspx whose codebehind class is Contact and you have an entity name Contact.



              Then following code will throw a NullReferenceException when you call context.SaveChanges()



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


              For the sake of completeness DataContext class



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


              and Contact entity class. Sometimes entity classes are partial classes so that you can extend them in other files too.



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


              The error occurs when both the entity and codebehind class are in same namespace.
              To fix this, rename the entity class or the codebehind class for Contact.aspx.



              Reason
              I am still not sure about the reason. But whenever any of the entity class will extend System.Web.UI.Page this error occurs.



              For discussion have a look at NullReferenceException in DbContext.saveChanges()





              share

































                38














                Another general case where one might receive this exception involves mocking classes during unit testing. Regardless of the mocking framework being used, you must ensure that all appropriate levels of the class hierarchy are properly mocked. In particular, all properties of HttpContext which are referenced by the code under test must be mocked.



                See "NullReferenceException thrown when testing custom AuthorizationAttribute" for a somewhat verbose example.





                share

































                  37














                  I have a different perspective to answering this. This sort of answers "what else can I do to avoid it?"



                  When working across different layers, for example in an MVC application, a controller needs services to call business operations. In such scenarios Dependency Injection Container can be used to initialize the services to avoid the NullReferenceException. So that means you don't need to worry about checking for null and just call the services from the controller as though they will always to available (and initialized) as either a singleton or a prototype.



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

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

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




                  share





















                  • 5





                    -1: this only handles a single scenario - that of uninitialized dependencies. This is a minority scenario for NullReferenceException. Most cases are simple misunderstanding of how objects work. Next most frequent are other situations where the developer assumed that the object would be initialized automatically.

                    – John Saunders
                    Mar 7 '14 at 0:06






                  • 3





                    All others have already been answered above.

                    – Mukus
                    Mar 7 '14 at 0:23











                  • Dependency injection is not generally used in order to avoid NullReferenceException. I don't believe that you have found a general scenario here. In any case, if you edit your answer to be more in the style of stackoverflow.com/a/15232518/76337, then I will remove the downvote.

                    – John Saunders
                    Mar 7 '14 at 0:30



















                  35














                  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 property X in a class and later try to invoke one of its methods and X has a null value, then this will lead to NullReferenceException:



                  public X { get; set; }

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


                  But if you set "property X must never have a null value" as method precondition, then you can prevent the scenario described before:



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


                  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.





                  share





















                  • 1





                    I thought to add this as no one mentioned this, and as far as it exists as an approach, my intention was to enrich the topic.

                    – Nick Louloudakis
                    Dec 26 '14 at 1:03








                  • 1





                    Thank you for enriching the topic. I have given my opinion of your addition. Now others can do the same.

                    – John Saunders
                    Dec 26 '14 at 1:05






                  • 1





                    I thought this was a worthwhile addition to the topic given that this is a highly viewed thread. I've heard of code contracts before and this was a good reminder to consider using them.

                    – VoteCoffee
                    Jan 8 '15 at 2:03



















                  33














                  A NullReferenceException is thrown when we are trying to access Properties of a null object or when a string value becomes empty and we are trying to access string methods.



                  For example:





                  1. When a string method of an empty string accessed:



                    string str = string.Empty;
                    str.ToLower(); // throw null reference exception



                  2. When a property of a null object accessed:



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






                  share





















                  • 2





                    This is incorrect. String.Empty.ToLower() will not throw a null reference exception. It represents an actual string, albeit an empty one (i.e. ""). Since this has an object to call ToLower() on, it would not make sense to throw a null reference exception there.

                    – Kjartan
                    Jul 24 '15 at 6:00





















                  29














                  TL;DR: Try using Html.Partial instead of Renderpage





                  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:



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


                  Debugging showed the model was Null inside MyOtherView. Until I changed it to:



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


                  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 under Html.Partial if it's inside a differently constructed foreach loop, even though it's not really an error:



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


                  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:



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


                  Although I have a feeling it was because Visual Studio was misreading the ampersands and brackets.





                  share


























                  • You wanted Html.Partial, not @Html.Partial

                    – John Saunders
                    Jul 24 '15 at 13:55











                  • Also, please show which line threw the exception, and why.

                    – John Saunders
                    Jul 24 '15 at 13:56











                  • The error occurred in MyOtherView.cshtml, which I did not include here, because the Model was not being properly sent in (it was Null), so I knew the error was with how I was sending the Model in.

                    – Travis Heeter
                    Jul 27 '15 at 11:44



















                  21














                  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.



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


                  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:



                  public void DoSometing([NotNull] obj)


                  By doing that and making PostSharp part of your build process obj will be checked for null at runtime. See: PostSharp null check



                  Plain 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>:



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

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

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

                  _value = value;
                  }
                  }

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

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


                  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 allow null. Here are some examples:



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


                  NotNull<T> is implicitly cast to and from T so you can use it just about anywhere you need it. For example, you can pass a Person object to a method that takes a NotNull<Person>:



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

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


                  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:



                  Person person = GetPerson();

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


                  Or you can even use it when the method just returns T (in this case Person) by doing a cast. For example, the following code would just like the code above:



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

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


                  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:



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

                  return @this;
                  }
                  }


                  And here is an example of how it could be used:



                  var person = GetPerson().NotNull();


                  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 returns null.



                  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:



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


                  Imagine that country is an object of type Country that has a property called State and so on. If country, State, County, or City is null then address will benull. Therefore you only have to check whetheraddressisnull`.



                  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 so int?.



                  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).





                  share





















                  • 2





                    Never throw NullReferenceException

                    – John Saunders
                    Mar 6 '16 at 20:35











                  • @JohnSaunders dare I ask why? (Seriously though why?)

                    – Luis Perez
                    Mar 7 '16 at 15:29






                  • 2





                    NullReferenceException is meant to be thrown by the CLR. It means that a reference to a null has occurred. It does not mean that a reference to a null would occur except that you cleverly checked first.

                    – John Saunders
                    Mar 7 '16 at 15:43











                  • I see your point about how that would be confusing. I've updated it to a regular exception for this example and a custom exception in GitHub.

                    – Luis Perez
                    Mar 7 '16 at 18:41













                  • Great answer for such a basic question. It's not so bad when it is your code that is failing. It's horrible when it's coming from deep inside some a commercial third party library you are relying on, and the customer support keeps insisting that it has to be your code that is causing the problem. And your not entirely sure it's not and the whole project is ground to halt.. I actually think this might make an appropriate epitaph for my tombstone: "Object reference not set to an instance of an object."

                    – Darrel Lee
                    May 3 '16 at 4:01



















                  9














                  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



                    var name = p?.Spouse?.FirstName;


                  is equivalent to:



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


                  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





                  share

































                    8














                    The error line "Object reference not set to an instance of an object.
                    " states that you have not assigned instance object to a object reference and still you are accessing properies/methods of that object.



                    for example: let say you have a class called myClass and it contains one property prop1.



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


                    Now you are accessing this prop1 in some other class just like below:



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


                    above line throws error because reference of class myClass is declared but not instantiated or an instance of object is not assigned to referecne of that class.



                    To fix this you have to instantiate (assign object to reference of that class).



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




                    share





















                    • 2





                      This answer has already been answered.

                      – John Saunders
                      Mar 8 '17 at 11:31



















                    8














                    Interestingly, none of the answers on this page mention the two edge cases, hope no one minds if I add them:



                    Edge case #1: concurrent access to a Dictionary



                    Generic dictionaries in .NET are not thread-safe and they sometimes might throw a NullReference or even (more frequent) a KeyNotFoundException when you try to access a key from two concurrent threads. The exception is quite misleading in this case.



                    Edge case #2: unsafe code



                    If a NullReferenceException is thrown by unsafe code, you might look at your pointer variables, and check them for IntPtr.Zero or something. Which is the same thing ("null pointer exception"), but in unsafe code, variables are often cast to value-types/arrays, etc., and you bang your head against the wall, wondering how a value-type can throw this exception.



                    (Another reason for non-using unsafe code unless you need it, by the way)





                    share





















                    • 5





                      Your dictionary example is not an edge case. If the object is not thread safe, then using it from multiple threads produces random results. Your unsafe code example differs from null in which way?

                      – John Saunders
                      Mar 24 '17 at 2:03



















                    3














                    NullReferenceException or Object reference not set to an instance of an object occurs when an object of the class you are trying to use is not instantiated.
                    For example:



                    Assume that you have a class named Student.



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


                    Now, consider another class where you are trying to retrieve the student's full name.



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


                    As seen in the above code, the statement
                    Student s - only declares the variable of type Student, note that the Student class is not instantiated at this point.
                    Hence, when the statement s.GetFullName() gets executed, it will throw the NullReferenceException.





                    share

































                      2














                      Well, in simple terms:



                      You are trying to access an object that isn't created or currently not in memory.



                      So how to tackle this:




                      1. Debug and let the debugger break... It will directly take you to the variable that is broken... Now your task is to simply fix this.. Using the new keyword in the appropriate place.



                      2. If it is caused on some database commands because the object isn't present then all you need to do is do a null check and handle it:



                        if (i == null) {
                        // Handle this
                        }


                      3. The hardest one .. if the GC collected the object already... This generally occurs if you are trying to find an object using strings... That is, finding it by name of the object then it may happen that the GC might already cleaned it up... This is hard to find and will become quite a problem... A better way to tackle this is do null checks wherever necessary during the development process. This will save you a lot of time.



                      By finding by name I mean some framework allow you to FIndObjects using strings and the code might look like this: FindObject("ObjectName");





                      share





















                      • 3





                        If you have a reference to an object, then the GC never cleans it up

                        – John Saunders
                        Dec 24 '15 at 7:51






                      • 2





                        if you use things like FindObject("Name of Object") there is no way GC will know before hand that you are going to refernece that object .. this is what is was trying to explaing .. these occur at runtime

                        – Akash Gutha
                        Dec 24 '15 at 8:11






                      • 2





                        There are some frameworks that Provide this functionality in C# such as Unity . the question has nothing related to BCl. Search the Internet before Criticizing there are a ton of functions like them and for ur kind information i even use it daily. Now please tell me how does the answer doesn't make anysense.

                        – Akash Gutha
                        Dec 24 '15 at 12:35






                      • 2





                        docs.unity3d.com/ScriptReference/… check the link and correct urself mr.expert :p

                        – Akash Gutha
                        Dec 24 '15 at 12:54











                      • The examples I saw in your link assign the results of GameObject.Find to a member field. That's a reference and the GC will not collect it until the containing object is collected.

                        – John Saunders
                        May 25 '16 at 18:00



















                      0














                      If we consider common scenarios where this exception can be thrown, accessing properties withing object at the top.



                      Ex:



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


                      in here , if address is null , then you will get NullReferenceException.



                      So, as a practice we should always use null check, before accessing properties in such objects (specially in generic)



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




                      share





















                      • 3





                        This has already been answered several times.

                        – John Saunders
                        Aug 26 '17 at 3:24



















                      0














                      Literally the easiest way to fix a NullReferenceExeption has two ways.
                      If you have a GameObject for example with a script attached and a variable named rb (rigidbody) this variable will start null when you start your game.

                      This is why you get a NullReferenceExeption because the computer does not have data stored in that variable.



                      I'll be using a RigidBody variable as an example.

                      We can add data really easily actually in a few ways:




                      1. Add a RigidBody to your object with AddComponent > Physics > Rigidbody

                        Then go into your script and type rb = GetComponent<Rigidbody>();

                        This line of code works best under your Start() or Awake() functions.

                      2. You can add a component programmatically and assign the variable at the same time with one line of code: rb = AddComponent<RigidBody>();


                      Further Notes: If you want unity to add a component to your object and you might have forgotten to add one, you can type [RequireComponent(typeof(RigidBody))] above your class declaration (the space below all of your usings).

                      Enjoy and have fun making games!





                      share

































                        -1














                        If one is getting this message during saving or compiling the build, just close all the files and then open any file to compile and save.



                        For me the reason was that I had rename the file and old file was still open.





                        share

































                          -1














                          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.





                          share


























                          • new never returns null

                            – John Saunders
                            Oct 27 '17 at 13:00











                          • @John did you mean to accept this answer?

                            – CodeCaster
                            Oct 27 '17 at 14:48











                          • @code no. Thanks for the catch. Phone app.

                            – John Saunders
                            Oct 27 '17 at 14:49



















                          -2














                          This is basically is a Null reference exception. As Microsoft states-




                          A NullReferenceException exception is thrown when you try to access a
                          member of a type whose value is null.




                          What does that mean?



                          That means if any member which doesn’t hold any value and we are making that member to perform certain task then the system will undoubtedly toss a message and say-



                          “Hey wait, that member has no values so it can’t perform the task which you are handing it over.”



                          The exception itself says that something is being referred but whose value is not being set. So this denotes that it only occurs while using reference types as Value types are non-nullable.



                          NullReferenceException won't occur if we are using Value type members.



                          class Program
                          {
                          static void Main(string args)
                          {
                          string str = null;
                          Console.WriteLine(str.Length);
                          Console.ReadLine();
                          }
                          }


                          The above code shows simple string which is assigned with a null value.



                          Now, when I try to print the length of the string str, I do get An unhandled exception of type ‘System.NullReferenceException’ occurred message because member str is pointing to null and there can’t be any length of null.



                          NullReferenceException’ also occurs when we forget to instantiate a reference type.



                          Suppose I have a class and member method in it. I have not instantiated my class but only named my class. Now if I try to use the method, the compiler will throw an error or issue a warning (depending on the compiler).



                          class Program
                          {
                          static void Main(string args)
                          {
                          MyClass1 obj;
                          obj.foo(); //Use of unassigned local variable 'obj'
                          }
                          }

                          public class MyClass1
                          {
                          internal void foo()
                          {
                          Console.WriteLine("hello from foo");

                          }
                          }


                          Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing. Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing.



                          Why it occurs?




                          • NullReferenceException arises due to our fault for not checking the object’s value. We often leave the object values unchecked in the code development.


                          • It also arises when we forget to instantiate our objects. Using methods, properties, collections etc. which can return or set null values can also be the cause of this exception.



                          How can it be avoided?



                          There are various ways and methods to avoid this renowned exception:




                          1. Explicit Checking: We should adhere to the tradition of checking the objects, properties, methods, arrays, and collections whether they are null. This can be simply implemented using conditional statements like if-else if-else etc.


                          2. Exception handling: One of the important ways of managing this exception. Using simple try-catch-finally blocks we can control this exception and also maintain a log of it. This can be very useful when your application is on production stage.


                          3. Null operators: Null Coalescing operator and null conditional operators can also be used in handy while setting values to objects, variables, properties and fields.


                          4. Debugger: For developers, we have the big weapon of Debugging with us. If have we face NullReferenceException during the development face we can use the debugger to get to the source of the exception.


                          5. In-built method: System methods such as GetValueOrDefault(),IsNullOrWhiteSpace() and IsNullorEmpty() checks for nulls and assign the default value if there is a null value.



                          There are many good answers already here. You can also check more detailed description with examples on my blog.



                          Hope this helps too!





                          share


























                          • You basically copied half of that blog post and added nothing new that existing answers don't address.

                            – CodeCaster
                            Jul 18 '17 at 14:41











                          • @codecaster Is it said copying when you rewrite a summary from your own blog. I know there is nothing new in my answer and nothing new that previous answers dont have but I wish to contribute in more sophisticated way and let others understand the way I understood. Will be glad even if it helps a single person. In good faith.

                            – Wasim
                            Jul 18 '17 at 18:01



















                          -5















                          You are trying to access an object that isn't created or currently not in memory.




                          When I get this error, I add the following code to any event handler that triggers the error.



                          if (!IsLoaded) return;


                          The ! means "not", FYI. So if the object in question isn't loaded, the program terminates the execution and prevents the crash.





                          share





















                          • 6





                            This makes no sense at all.

                            – DavidG
                            Jul 5 '17 at 12:37











                          • It works for me when an event handler fires but the object it's looking for hasn't been created yet. Last time it was a TextChanged event for a textbox.

                            – Nathan
                            Jul 5 '17 at 20:13















                          1 2
                          next


                          protected by ken2k Dec 4 '13 at 10:11



                          Thank you for your interest in this question.
                          Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                          Would you like to answer one of these unanswered questions instead?














                          31 Answers
                          31






                          active

                          oldest

                          votes








                          31 Answers
                          31






                          active

                          oldest

                          votes









                          active

                          oldest

                          votes






                          active

                          oldest

                          votes








                          1 2
                          next










                          2185














                          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:




                          1. Object variables which are uninitialized and hence point to nothing. In this case, if you access properties or methods of such objects, it causes a NullReferenceException.

                          2. 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 assign null to them to indicate there is no value stored in it, for example int? a = null; where the question mark indicates it is allowed to store null in variable a. You can check that either with if (a.HasValue) {...} or with if (a==null) {...}. Nullable variables, like a this example, allow to access the value via a.Value explicitly, or just as normal via a.
                            Note that accessing it via a.Value throws an InvalidOperationException instead of a NullReferenceException if a is null - you should do the check beforehand, i.e. if you have another on-nullable variable int b; then you should do assignments like if (a.HasValue) { b = a.Value; } or shorter if (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 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



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


                          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(); // NullReference 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 null collection. Usually caused by unexpected null result from methods that return collections.



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


                          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 call you expect to return 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 maybe can, 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.



                          Special techniques for debugging and fixing null derefs in iterators



                          C# supports "iterator blocks" (called "generators" in some other popular languages). Null dereference exceptions can be particularly tricky to debug in iterator blocks because of deferred execution:



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


                          If whatever results in null then MakeFrob will throw. Now, you might think that the right thing to do is this:



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


                          Why is this wrong? Because the iterator block does not actually run until the foreach! The call to GetFrobs simply returns an object which when iterated will run the iterator block.



                          By writing a null check like this you prevent the null dereference, but you move the null argument exception to the point of the iteration, not to the point of the call, and that is very confusing to debug.



                          The correct fix is:



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


                          That is, make a private helper method that has the iterator block logic, and a public surface method that does the null check and returns the iterator. Now when GetFrobs is called, the null check happens immediately, and then GetFrobsForReal executes when the sequence is iterated.



                          If you examine the reference source for LINQ to Objects you will see that this technique is used throughout. It is slightly more clunky to write, but it makes debugging nullity errors much easier. Optimize your code for the convenience of the caller, not the convenience of the author.



                          A note on null dereferences in unsafe code



                          C# has an "unsafe" mode which is, as the name implies, extremely dangerous because the normal safety mechanisms which provide memory safety and type safety are not enforced. You should not be writing unsafe code unless you have a thorough and deep understanding of how memory works.



                          In unsafe mode, you should be aware of two important facts:




                          • dereferencing a null pointer produces the same exception as dereferencing a null reference

                          • dereferencing an invalid non-null pointer can produce that exception
                            in some circumstances


                          To understand why that is, it helps to understand how .NET produces null dereference exceptions in the first place. (These details apply to .NET running on Windows; other operating systems use similar mechanisms.)



                          Memory is virtualized in Windows; each process gets a virtual memory space of many "pages" of memory that are tracked by the operating system. Each page of memory has flags set on it which determine how it may be used: read from, written to, executed, and so on. The lowest page is marked as "produce an error if ever used in any way".



                          Both a null pointer and a null reference in C# are internally represented as the number zero, and so any attempt to dereference it into its corresponding memory storage causes the operating system to produce an error. The .NET runtime then detects this error and turns it into the null dereference exception.



                          That's why dereferencing both a null pointer and a null reference produces the same exception.



                          What about the second point? Dereferencing any invalid pointer that falls in the lowest page of virtual memory causes the same operating system error, and thereby the same exception.



                          Why does this make sense? Well, suppose we have a struct containing two ints, and an unmanaged pointer equal to null. If we attempt to dereference the second int in the struct, the CLR will not attempt to access the storage at location zero; it will access the storage at location four. But logically this is a null dereference because we are getting to that address via the null.



                          If you are working with unsafe code and you get a null dereference exception, just be aware that the offending pointer need not be null. It can be any location in the lowest page, and this exception will be produced.





                          share





















                          • 49





                            Maybe this is a dumb comment but wouldnt the first and best way to avoid this problem be to initialize the object? For me if this error occurs it is usually because I forgot to initialize something like the array element. I think it is far less common to define the object as null and then reference it. Maybe give the way to solve each problem adjacent to the description. Still a good post.

                            – JPK
                            May 20 '14 at 6:39






                          • 28





                            What if there is no object, but rather the return value from a method or property?

                            – John Saunders
                            May 20 '14 at 6:41






                          • 6





                            The book/author example is a little weird.... How does that even compile? How does intellisense even work? What is this I'm not good with computar...

                            – Will
                            Sep 8 '14 at 18:26






                          • 5





                            @Will: does my last edit help? If not, then please be more explicit about what you see as a problem.

                            – John Saunders
                            Sep 8 '14 at 18:41






                          • 6





                            @JohnSaunders Oh, no, sorry, I meant the object initializer version of that. new Book { Author = { Age = 45 } }; How does the inner initialization even... I can't think of a situation where inner init would ever work, yet it compiles and intellisense works... Unless for structs?

                            – Will
                            Sep 8 '14 at 18:44


















                          2185














                          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:




                          1. Object variables which are uninitialized and hence point to nothing. In this case, if you access properties or methods of such objects, it causes a NullReferenceException.

                          2. 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 assign null to them to indicate there is no value stored in it, for example int? a = null; where the question mark indicates it is allowed to store null in variable a. You can check that either with if (a.HasValue) {...} or with if (a==null) {...}. Nullable variables, like a this example, allow to access the value via a.Value explicitly, or just as normal via a.
                            Note that accessing it via a.Value throws an InvalidOperationException instead of a NullReferenceException if a is null - you should do the check beforehand, i.e. if you have another on-nullable variable int b; then you should do assignments like if (a.HasValue) { b = a.Value; } or shorter if (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 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



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


                          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(); // NullReference 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 null collection. Usually caused by unexpected null result from methods that return collections.



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


                          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 call you expect to return 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 maybe can, 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.



                          Special techniques for debugging and fixing null derefs in iterators



                          C# supports "iterator blocks" (called "generators" in some other popular languages). Null dereference exceptions can be particularly tricky to debug in iterator blocks because of deferred execution:



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


                          If whatever results in null then MakeFrob will throw. Now, you might think that the right thing to do is this:



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


                          Why is this wrong? Because the iterator block does not actually run until the foreach! The call to GetFrobs simply returns an object which when iterated will run the iterator block.



                          By writing a null check like this you prevent the null dereference, but you move the null argument exception to the point of the iteration, not to the point of the call, and that is very confusing to debug.



                          The correct fix is:



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


                          That is, make a private helper method that has the iterator block logic, and a public surface method that does the null check and returns the iterator. Now when GetFrobs is called, the null check happens immediately, and then GetFrobsForReal executes when the sequence is iterated.



                          If you examine the reference source for LINQ to Objects you will see that this technique is used throughout. It is slightly more clunky to write, but it makes debugging nullity errors much easier. Optimize your code for the convenience of the caller, not the convenience of the author.



                          A note on null dereferences in unsafe code



                          C# has an "unsafe" mode which is, as the name implies, extremely dangerous because the normal safety mechanisms which provide memory safety and type safety are not enforced. You should not be writing unsafe code unless you have a thorough and deep understanding of how memory works.



                          In unsafe mode, you should be aware of two important facts:




                          • dereferencing a null pointer produces the same exception as dereferencing a null reference

                          • dereferencing an invalid non-null pointer can produce that exception
                            in some circumstances


                          To understand why that is, it helps to understand how .NET produces null dereference exceptions in the first place. (These details apply to .NET running on Windows; other operating systems use similar mechanisms.)



                          Memory is virtualized in Windows; each process gets a virtual memory space of many "pages" of memory that are tracked by the operating system. Each page of memory has flags set on it which determine how it may be used: read from, written to, executed, and so on. The lowest page is marked as "produce an error if ever used in any way".



                          Both a null pointer and a null reference in C# are internally represented as the number zero, and so any attempt to dereference it into its corresponding memory storage causes the operating system to produce an error. The .NET runtime then detects this error and turns it into the null dereference exception.



                          That's why dereferencing both a null pointer and a null reference produces the same exception.



                          What about the second point? Dereferencing any invalid pointer that falls in the lowest page of virtual memory causes the same operating system error, and thereby the same exception.



                          Why does this make sense? Well, suppose we have a struct containing two ints, and an unmanaged pointer equal to null. If we attempt to dereference the second int in the struct, the CLR will not attempt to access the storage at location zero; it will access the storage at location four. But logically this is a null dereference because we are getting to that address via the null.



                          If you are working with unsafe code and you get a null dereference exception, just be aware that the offending pointer need not be null. It can be any location in the lowest page, and this exception will be produced.





                          share





















                          • 49





                            Maybe this is a dumb comment but wouldnt the first and best way to avoid this problem be to initialize the object? For me if this error occurs it is usually because I forgot to initialize something like the array element. I think it is far less common to define the object as null and then reference it. Maybe give the way to solve each problem adjacent to the description. Still a good post.

                            – JPK
                            May 20 '14 at 6:39






                          • 28





                            What if there is no object, but rather the return value from a method or property?

                            – John Saunders
                            May 20 '14 at 6:41






                          • 6





                            The book/author example is a little weird.... How does that even compile? How does intellisense even work? What is this I'm not good with computar...

                            – Will
                            Sep 8 '14 at 18:26






                          • 5





                            @Will: does my last edit help? If not, then please be more explicit about what you see as a problem.

                            – John Saunders
                            Sep 8 '14 at 18:41






                          • 6





                            @JohnSaunders Oh, no, sorry, I meant the object initializer version of that. new Book { Author = { Age = 45 } }; How does the inner initialization even... I can't think of a situation where inner init would ever work, yet it compiles and intellisense works... Unless for structs?

                            – Will
                            Sep 8 '14 at 18:44
















                          2185












                          2185








                          2185







                          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:




                          1. Object variables which are uninitialized and hence point to nothing. In this case, if you access properties or methods of such objects, it causes a NullReferenceException.

                          2. 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 assign null to them to indicate there is no value stored in it, for example int? a = null; where the question mark indicates it is allowed to store null in variable a. You can check that either with if (a.HasValue) {...} or with if (a==null) {...}. Nullable variables, like a this example, allow to access the value via a.Value explicitly, or just as normal via a.
                            Note that accessing it via a.Value throws an InvalidOperationException instead of a NullReferenceException if a is null - you should do the check beforehand, i.e. if you have another on-nullable variable int b; then you should do assignments like if (a.HasValue) { b = a.Value; } or shorter if (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 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



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


                          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(); // NullReference 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 null collection. Usually caused by unexpected null result from methods that return collections.



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


                          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 call you expect to return 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 maybe can, 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.



                          Special techniques for debugging and fixing null derefs in iterators



                          C# supports "iterator blocks" (called "generators" in some other popular languages). Null dereference exceptions can be particularly tricky to debug in iterator blocks because of deferred execution:



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


                          If whatever results in null then MakeFrob will throw. Now, you might think that the right thing to do is this:



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


                          Why is this wrong? Because the iterator block does not actually run until the foreach! The call to GetFrobs simply returns an object which when iterated will run the iterator block.



                          By writing a null check like this you prevent the null dereference, but you move the null argument exception to the point of the iteration, not to the point of the call, and that is very confusing to debug.



                          The correct fix is:



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


                          That is, make a private helper method that has the iterator block logic, and a public surface method that does the null check and returns the iterator. Now when GetFrobs is called, the null check happens immediately, and then GetFrobsForReal executes when the sequence is iterated.



                          If you examine the reference source for LINQ to Objects you will see that this technique is used throughout. It is slightly more clunky to write, but it makes debugging nullity errors much easier. Optimize your code for the convenience of the caller, not the convenience of the author.



                          A note on null dereferences in unsafe code



                          C# has an "unsafe" mode which is, as the name implies, extremely dangerous because the normal safety mechanisms which provide memory safety and type safety are not enforced. You should not be writing unsafe code unless you have a thorough and deep understanding of how memory works.



                          In unsafe mode, you should be aware of two important facts:




                          • dereferencing a null pointer produces the same exception as dereferencing a null reference

                          • dereferencing an invalid non-null pointer can produce that exception
                            in some circumstances


                          To understand why that is, it helps to understand how .NET produces null dereference exceptions in the first place. (These details apply to .NET running on Windows; other operating systems use similar mechanisms.)



                          Memory is virtualized in Windows; each process gets a virtual memory space of many "pages" of memory that are tracked by the operating system. Each page of memory has flags set on it which determine how it may be used: read from, written to, executed, and so on. The lowest page is marked as "produce an error if ever used in any way".



                          Both a null pointer and a null reference in C# are internally represented as the number zero, and so any attempt to dereference it into its corresponding memory storage causes the operating system to produce an error. The .NET runtime then detects this error and turns it into the null dereference exception.



                          That's why dereferencing both a null pointer and a null reference produces the same exception.



                          What about the second point? Dereferencing any invalid pointer that falls in the lowest page of virtual memory causes the same operating system error, and thereby the same exception.



                          Why does this make sense? Well, suppose we have a struct containing two ints, and an unmanaged pointer equal to null. If we attempt to dereference the second int in the struct, the CLR will not attempt to access the storage at location zero; it will access the storage at location four. But logically this is a null dereference because we are getting to that address via the null.



                          If you are working with unsafe code and you get a null dereference exception, just be aware that the offending pointer need not be null. It can be any location in the lowest page, and this exception will be produced.





                          share















                          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:




                          1. Object variables which are uninitialized and hence point to nothing. In this case, if you access properties or methods of such objects, it causes a NullReferenceException.

                          2. 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 assign null to them to indicate there is no value stored in it, for example int? a = null; where the question mark indicates it is allowed to store null in variable a. You can check that either with if (a.HasValue) {...} or with if (a==null) {...}. Nullable variables, like a this example, allow to access the value via a.Value explicitly, or just as normal via a.
                            Note that accessing it via a.Value throws an InvalidOperationException instead of a NullReferenceException if a is null - you should do the check beforehand, i.e. if you have another on-nullable variable int b; then you should do assignments like if (a.HasValue) { b = a.Value; } or shorter if (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 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



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


                          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(); // NullReference 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 null collection. Usually caused by unexpected null result from methods that return collections.



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


                          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 call you expect to return 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 maybe can, 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.



                          Special techniques for debugging and fixing null derefs in iterators



                          C# supports "iterator blocks" (called "generators" in some other popular languages). Null dereference exceptions can be particularly tricky to debug in iterator blocks because of deferred execution:



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


                          If whatever results in null then MakeFrob will throw. Now, you might think that the right thing to do is this:



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


                          Why is this wrong? Because the iterator block does not actually run until the foreach! The call to GetFrobs simply returns an object which when iterated will run the iterator block.



                          By writing a null check like this you prevent the null dereference, but you move the null argument exception to the point of the iteration, not to the point of the call, and that is very confusing to debug.



                          The correct fix is:



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


                          That is, make a private helper method that has the iterator block logic, and a public surface method that does the null check and returns the iterator. Now when GetFrobs is called, the null check happens immediately, and then GetFrobsForReal executes when the sequence is iterated.



                          If you examine the reference source for LINQ to Objects you will see that this technique is used throughout. It is slightly more clunky to write, but it makes debugging nullity errors much easier. Optimize your code for the convenience of the caller, not the convenience of the author.



                          A note on null dereferences in unsafe code



                          C# has an "unsafe" mode which is, as the name implies, extremely dangerous because the normal safety mechanisms which provide memory safety and type safety are not enforced. You should not be writing unsafe code unless you have a thorough and deep understanding of how memory works.



                          In unsafe mode, you should be aware of two important facts:




                          • dereferencing a null pointer produces the same exception as dereferencing a null reference

                          • dereferencing an invalid non-null pointer can produce that exception
                            in some circumstances


                          To understand why that is, it helps to understand how .NET produces null dereference exceptions in the first place. (These details apply to .NET running on Windows; other operating systems use similar mechanisms.)



                          Memory is virtualized in Windows; each process gets a virtual memory space of many "pages" of memory that are tracked by the operating system. Each page of memory has flags set on it which determine how it may be used: read from, written to, executed, and so on. The lowest page is marked as "produce an error if ever used in any way".



                          Both a null pointer and a null reference in C# are internally represented as the number zero, and so any attempt to dereference it into its corresponding memory storage causes the operating system to produce an error. The .NET runtime then detects this error and turns it into the null dereference exception.



                          That's why dereferencing both a null pointer and a null reference produces the same exception.



                          What about the second point? Dereferencing any invalid pointer that falls in the lowest page of virtual memory causes the same operating system error, and thereby the same exception.



                          Why does this make sense? Well, suppose we have a struct containing two ints, and an unmanaged pointer equal to null. If we attempt to dereference the second int in the struct, the CLR will not attempt to access the storage at location zero; it will access the storage at location four. But logically this is a null dereference because we are getting to that address via the null.



                          If you are working with unsafe code and you get a null dereference exception, just be aware that the offending pointer need not be null. It can be any location in the lowest page, and this exception will be produced.






                          share













                          share


                          share








                          edited Jul 18 '18 at 6:27


























                          community wiki





                          45 revs, 29 users 17%
                          John Saunders









                          • 49





                            Maybe this is a dumb comment but wouldnt the first and best way to avoid this problem be to initialize the object? For me if this error occurs it is usually because I forgot to initialize something like the array element. I think it is far less common to define the object as null and then reference it. Maybe give the way to solve each problem adjacent to the description. Still a good post.

                            – JPK
                            May 20 '14 at 6:39






                          • 28





                            What if there is no object, but rather the return value from a method or property?

                            – John Saunders
                            May 20 '14 at 6:41






                          • 6





                            The book/author example is a little weird.... How does that even compile? How does intellisense even work? What is this I'm not good with computar...

                            – Will
                            Sep 8 '14 at 18:26






                          • 5





                            @Will: does my last edit help? If not, then please be more explicit about what you see as a problem.

                            – John Saunders
                            Sep 8 '14 at 18:41






                          • 6





                            @JohnSaunders Oh, no, sorry, I meant the object initializer version of that. new Book { Author = { Age = 45 } }; How does the inner initialization even... I can't think of a situation where inner init would ever work, yet it compiles and intellisense works... Unless for structs?

                            – Will
                            Sep 8 '14 at 18:44
















                          • 49





                            Maybe this is a dumb comment but wouldnt the first and best way to avoid this problem be to initialize the object? For me if this error occurs it is usually because I forgot to initialize something like the array element. I think it is far less common to define the object as null and then reference it. Maybe give the way to solve each problem adjacent to the description. Still a good post.

                            – JPK
                            May 20 '14 at 6:39






                          • 28





                            What if there is no object, but rather the return value from a method or property?

                            – John Saunders
                            May 20 '14 at 6:41






                          • 6





                            The book/author example is a little weird.... How does that even compile? How does intellisense even work? What is this I'm not good with computar...

                            – Will
                            Sep 8 '14 at 18:26






                          • 5





                            @Will: does my last edit help? If not, then please be more explicit about what you see as a problem.

                            – John Saunders
                            Sep 8 '14 at 18:41






                          • 6





                            @JohnSaunders Oh, no, sorry, I meant the object initializer version of that. new Book { Author = { Age = 45 } }; How does the inner initialization even... I can't think of a situation where inner init would ever work, yet it compiles and intellisense works... Unless for structs?

                            – Will
                            Sep 8 '14 at 18:44










                          49




                          49





                          Maybe this is a dumb comment but wouldnt the first and best way to avoid this problem be to initialize the object? For me if this error occurs it is usually because I forgot to initialize something like the array element. I think it is far less common to define the object as null and then reference it. Maybe give the way to solve each problem adjacent to the description. Still a good post.

                          – JPK
                          May 20 '14 at 6:39





                          Maybe this is a dumb comment but wouldnt the first and best way to avoid this problem be to initialize the object? For me if this error occurs it is usually because I forgot to initialize something like the array element. I think it is far less common to define the object as null and then reference it. Maybe give the way to solve each problem adjacent to the description. Still a good post.

                          – JPK
                          May 20 '14 at 6:39




                          28




                          28





                          What if there is no object, but rather the return value from a method or property?

                          – John Saunders
                          May 20 '14 at 6:41





                          What if there is no object, but rather the return value from a method or property?

                          – John Saunders
                          May 20 '14 at 6:41




                          6




                          6





                          The book/author example is a little weird.... How does that even compile? How does intellisense even work? What is this I'm not good with computar...

                          – Will
                          Sep 8 '14 at 18:26





                          The book/author example is a little weird.... How does that even compile? How does intellisense even work? What is this I'm not good with computar...

                          – Will
                          Sep 8 '14 at 18:26




                          5




                          5





                          @Will: does my last edit help? If not, then please be more explicit about what you see as a problem.

                          – John Saunders
                          Sep 8 '14 at 18:41





                          @Will: does my last edit help? If not, then please be more explicit about what you see as a problem.

                          – John Saunders
                          Sep 8 '14 at 18:41




                          6




                          6





                          @JohnSaunders Oh, no, sorry, I meant the object initializer version of that. new Book { Author = { Age = 45 } }; How does the inner initialization even... I can't think of a situation where inner init would ever work, yet it compiles and intellisense works... Unless for structs?

                          – Will
                          Sep 8 '14 at 18:44







                          @JohnSaunders Oh, no, sorry, I meant the object initializer version of that. new Book { Author = { Age = 45 } }; How does the inner initialization even... I can't think of a situation where inner init would ever work, yet it compiles and intellisense works... Unless for structs?

                          – Will
                          Sep 8 '14 at 18:44















                          289














                          NullReference Exception — Visual Basic



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



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



                          Note:




                          1. This is concept-based: there is no code for you to paste into your project. It is intended to help you understand what causes a NullReferenceException (NRE), how to find it, how to fix it, and how to avoid it. An NRE can be caused many ways so this is unlikely to be your sole encounter.

                          2. The examples (from Stack  Overflow posts) do not always show the best way to do something in the first place.

                          3. Typically, the simplest remedy is used.


                          Basic Meaning



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




                          • Your code declared an object variable, but it did not initialize it (create an instance or 'instantiate' it)

                          • Something which your code assumed would initialize an object, did not

                          • Possibly, other code prematurely invalidated an object still in use


                          Finding The Cause



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



                          IDE debug display



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



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



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



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



                          See also:




                          • Breakpoints

                          • MSDN: How to: Use the Try/Catch Block to Catch Exceptions

                          • MSDN: Best Practices for Exceptions


                          Examples and Remedies



                          Class Objects / Creating an Instance





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


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



                          Remedy



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



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

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


                          When it is only appropriate to create the instance later:



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


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



                          Private reg As CashRegister
                          '...

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


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




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



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




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



                          For more information, see:




                          • New Operator

                          • Scope in Visual Basic

                          • Access Levels in Visual Basic

                          • Value Types and Reference Types




                          Arrays



                          Arrays must also be instantiated:



                          Private arr as String()


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



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

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


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



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


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



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


                          Example: Array of class objects



                          Dim arrFoo(5) As Foo

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


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



                          Remedy



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


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



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

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


                          For more information, see:




                          • Option Infer Statement

                          • Scope in Visual Basic

                          • Arrays in Visual Basic




                          Lists and Collections



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



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


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



                          myList = New List(Of String)

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


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



                          Public Class Foo
                          Private barList As List(Of Bar)

                          Friend Function BarCount As Integer
                          Return barList.Count
                          End Function

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


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



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


                          As before, this is incorrect:



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


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





                          Data Provider Objects



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



                          Example 1



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

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

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


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



                          img



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



                          Remedy



                          Dim ds As New DataSet


                          Example 2



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

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


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



                          Remedy



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



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


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



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


                          Example 3



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

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


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



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



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


                          See also DataTable Class.





                          Object Paths / Nested



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


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



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


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



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


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



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


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





                          UI Controls



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


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



                          Remedy



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



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

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


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





                          Visual Basic Forms



                          Public Class Form1

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

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

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


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



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




                          • Lists and Collection will simply be empty

                          • The Array will contain five elements of Nothing

                          • The somevar assignment will result in an immediate NRE because Nothing doesn't have a .Text property


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



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



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


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



                          Public Class Form1

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


                          Partial Remedy



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



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

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


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



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



                          "Button2" resides on a Panel



                          Remedy



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



                          ' Declaration
                          Private NameBoxes As TextBox()

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

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




                          Function Returning Nothing



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

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

                          Return bars
                          End Function


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



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


                          Remedy



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



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




                          Poorly Implemented Try/Catch



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



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

                          'More code fooing and barring

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

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


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



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



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



                          Remedy



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





                          DBNull is not the same as Nothing



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


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




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




                          Remedy



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


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



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


                          Example 2



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

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


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



                          If getFoo IsNot Nothing Then...




                          Controls



                          Dim chk As CheckBox

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


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



                          Remedy



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


                          The DataGridView



                          The DGV has a few quirks seen periodically:



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


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



                          Remedy



                          Name the columns manually, or reference by index:



                          dgvBooks.Columns(0).Visible = True


                          Example 2 — Beware of the NewRow



                          xlWorkSheet = xlWorkBook.Sheets("sheet1")

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


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



                          Remedy



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



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


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





                          My.Settings (StringCollection)



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



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


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



                          Remedy



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



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


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





                          Key Points



                          You probably forgot the New operator.



                          or



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



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





                          MSDN NullReference Exception





                          share






























                            289














                            NullReference Exception — Visual Basic



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



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



                            Note:




                            1. This is concept-based: there is no code for you to paste into your project. It is intended to help you understand what causes a NullReferenceException (NRE), how to find it, how to fix it, and how to avoid it. An NRE can be caused many ways so this is unlikely to be your sole encounter.

                            2. The examples (from Stack  Overflow posts) do not always show the best way to do something in the first place.

                            3. Typically, the simplest remedy is used.


                            Basic Meaning



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




                            • Your code declared an object variable, but it did not initialize it (create an instance or 'instantiate' it)

                            • Something which your code assumed would initialize an object, did not

                            • Possibly, other code prematurely invalidated an object still in use


                            Finding The Cause



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



                            IDE debug display



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



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



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



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



                            See also:




                            • Breakpoints

                            • MSDN: How to: Use the Try/Catch Block to Catch Exceptions

                            • MSDN: Best Practices for Exceptions


                            Examples and Remedies



                            Class Objects / Creating an Instance





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


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



                            Remedy



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



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

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


                            When it is only appropriate to create the instance later:



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


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



                            Private reg As CashRegister
                            '...

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


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




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



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




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



                            For more information, see:




                            • New Operator

                            • Scope in Visual Basic

                            • Access Levels in Visual Basic

                            • Value Types and Reference Types




                            Arrays



                            Arrays must also be instantiated:



                            Private arr as String()


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



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

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


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



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


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



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


                            Example: Array of class objects



                            Dim arrFoo(5) As Foo

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


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



                            Remedy



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


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



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

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


                            For more information, see:




                            • Option Infer Statement

                            • Scope in Visual Basic

                            • Arrays in Visual Basic




                            Lists and Collections



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



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


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



                            myList = New List(Of String)

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


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



                            Public Class Foo
                            Private barList As List(Of Bar)

                            Friend Function BarCount As Integer
                            Return barList.Count
                            End Function

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


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



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


                            As before, this is incorrect:



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


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





                            Data Provider Objects



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



                            Example 1



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

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

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


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



                            img



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



                            Remedy



                            Dim ds As New DataSet


                            Example 2



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

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


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



                            Remedy



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



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


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



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


                            Example 3



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

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


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



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



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


                            See also DataTable Class.





                            Object Paths / Nested



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


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



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


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



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


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



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


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





                            UI Controls



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


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



                            Remedy



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



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

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


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





                            Visual Basic Forms



                            Public Class Form1

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

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

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


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



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




                            • Lists and Collection will simply be empty

                            • The Array will contain five elements of Nothing

                            • The somevar assignment will result in an immediate NRE because Nothing doesn't have a .Text property


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



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



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


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



                            Public Class Form1

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


                            Partial Remedy



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



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

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


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



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



                            "Button2" resides on a Panel



                            Remedy



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



                            ' Declaration
                            Private NameBoxes As TextBox()

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

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




                            Function Returning Nothing



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

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

                            Return bars
                            End Function


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



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


                            Remedy



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



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




                            Poorly Implemented Try/Catch



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



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

                            'More code fooing and barring

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

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


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



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



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



                            Remedy



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





                            DBNull is not the same as Nothing



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


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




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




                            Remedy



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


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



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


                            Example 2



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

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


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



                            If getFoo IsNot Nothing Then...




                            Controls



                            Dim chk As CheckBox

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


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



                            Remedy



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


                            The DataGridView



                            The DGV has a few quirks seen periodically:



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


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



                            Remedy



                            Name the columns manually, or reference by index:



                            dgvBooks.Columns(0).Visible = True


                            Example 2 — Beware of the NewRow



                            xlWorkSheet = xlWorkBook.Sheets("sheet1")

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


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



                            Remedy



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



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


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





                            My.Settings (StringCollection)



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



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


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



                            Remedy



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



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


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





                            Key Points



                            You probably forgot the New operator.



                            or



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



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





                            MSDN NullReference Exception





                            share




























                              289












                              289








                              289







                              NullReference Exception — Visual Basic



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



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



                              Note:




                              1. This is concept-based: there is no code for you to paste into your project. It is intended to help you understand what causes a NullReferenceException (NRE), how to find it, how to fix it, and how to avoid it. An NRE can be caused many ways so this is unlikely to be your sole encounter.

                              2. The examples (from Stack  Overflow posts) do not always show the best way to do something in the first place.

                              3. Typically, the simplest remedy is used.


                              Basic Meaning



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




                              • Your code declared an object variable, but it did not initialize it (create an instance or 'instantiate' it)

                              • Something which your code assumed would initialize an object, did not

                              • Possibly, other code prematurely invalidated an object still in use


                              Finding The Cause



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



                              IDE debug display



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



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



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



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



                              See also:




                              • Breakpoints

                              • MSDN: How to: Use the Try/Catch Block to Catch Exceptions

                              • MSDN: Best Practices for Exceptions


                              Examples and Remedies



                              Class Objects / Creating an Instance





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


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



                              Remedy



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



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

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


                              When it is only appropriate to create the instance later:



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


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



                              Private reg As CashRegister
                              '...

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


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




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



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




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



                              For more information, see:




                              • New Operator

                              • Scope in Visual Basic

                              • Access Levels in Visual Basic

                              • Value Types and Reference Types




                              Arrays



                              Arrays must also be instantiated:



                              Private arr as String()


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



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

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


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



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


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



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


                              Example: Array of class objects



                              Dim arrFoo(5) As Foo

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


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



                              Remedy



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


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



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

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


                              For more information, see:




                              • Option Infer Statement

                              • Scope in Visual Basic

                              • Arrays in Visual Basic




                              Lists and Collections



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



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


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



                              myList = New List(Of String)

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


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



                              Public Class Foo
                              Private barList As List(Of Bar)

                              Friend Function BarCount As Integer
                              Return barList.Count
                              End Function

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


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



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


                              As before, this is incorrect:



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


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





                              Data Provider Objects



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



                              Example 1



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

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

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


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



                              img



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



                              Remedy



                              Dim ds As New DataSet


                              Example 2



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

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


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



                              Remedy



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



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


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



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


                              Example 3



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

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


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



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



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


                              See also DataTable Class.





                              Object Paths / Nested



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


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



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


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



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


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



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


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





                              UI Controls



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


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



                              Remedy



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



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

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


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





                              Visual Basic Forms



                              Public Class Form1

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

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

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


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



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




                              • Lists and Collection will simply be empty

                              • The Array will contain five elements of Nothing

                              • The somevar assignment will result in an immediate NRE because Nothing doesn't have a .Text property


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



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



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


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



                              Public Class Form1

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


                              Partial Remedy



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



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

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


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



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



                              "Button2" resides on a Panel



                              Remedy



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



                              ' Declaration
                              Private NameBoxes As TextBox()

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

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




                              Function Returning Nothing



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

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

                              Return bars
                              End Function


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



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


                              Remedy



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



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




                              Poorly Implemented Try/Catch



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



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

                              'More code fooing and barring

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

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


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



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



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



                              Remedy



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





                              DBNull is not the same as Nothing



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


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




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




                              Remedy



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


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



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


                              Example 2



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

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


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



                              If getFoo IsNot Nothing Then...




                              Controls



                              Dim chk As CheckBox

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


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



                              Remedy



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


                              The DataGridView



                              The DGV has a few quirks seen periodically:



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


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



                              Remedy



                              Name the columns manually, or reference by index:



                              dgvBooks.Columns(0).Visible = True


                              Example 2 — Beware of the NewRow



                              xlWorkSheet = xlWorkBook.Sheets("sheet1")

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


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



                              Remedy



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



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


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





                              My.Settings (StringCollection)



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



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


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



                              Remedy



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



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


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





                              Key Points



                              You probably forgot the New operator.



                              or



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



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





                              MSDN NullReference Exception





                              share















                              NullReference Exception — Visual Basic



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



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



                              Note:




                              1. This is concept-based: there is no code for you to paste into your project. It is intended to help you understand what causes a NullReferenceException (NRE), how to find it, how to fix it, and how to avoid it. An NRE can be caused many ways so this is unlikely to be your sole encounter.

                              2. The examples (from Stack  Overflow posts) do not always show the best way to do something in the first place.

                              3. Typically, the simplest remedy is used.


                              Basic Meaning



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




                              • Your code declared an object variable, but it did not initialize it (create an instance or 'instantiate' it)

                              • Something which your code assumed would initialize an object, did not

                              • Possibly, other code prematurely invalidated an object still in use


                              Finding The Cause



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



                              IDE debug display



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



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



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



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



                              See also:




                              • Breakpoints

                              • MSDN: How to: Use the Try/Catch Block to Catch Exceptions

                              • MSDN: Best Practices for Exceptions


                              Examples and Remedies



                              Class Objects / Creating an Instance





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


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



                              Remedy



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



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

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


                              When it is only appropriate to create the instance later:



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


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



                              Private reg As CashRegister
                              '...

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


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




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



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




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



                              For more information, see:




                              • New Operator

                              • Scope in Visual Basic

                              • Access Levels in Visual Basic

                              • Value Types and Reference Types




                              Arrays



                              Arrays must also be instantiated:



                              Private arr as String()


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



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

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


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



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


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



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


                              Example: Array of class objects



                              Dim arrFoo(5) As Foo

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


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



                              Remedy



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


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



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

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


                              For more information, see:




                              • Option Infer Statement

                              • Scope in Visual Basic

                              • Arrays in Visual Basic




                              Lists and Collections



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



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


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



                              myList = New List(Of String)

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


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



                              Public Class Foo
                              Private barList As List(Of Bar)

                              Friend Function BarCount As Integer
                              Return barList.Count
                              End Function

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


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



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


                              As before, this is incorrect:



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


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





                              Data Provider Objects



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



                              Example 1



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

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

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


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



                              img



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



                              Remedy



                              Dim ds As New DataSet


                              Example 2



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

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


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



                              Remedy



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



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


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



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


                              Example 3



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

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


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



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



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


                              See also DataTable Class.





                              Object Paths / Nested



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


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



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


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



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


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



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


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





                              UI Controls



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


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



                              Remedy



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



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

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


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





                              Visual Basic Forms



                              Public Class Form1

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

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

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


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



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




                              • Lists and Collection will simply be empty

                              • The Array will contain five elements of Nothing

                              • The somevar assignment will result in an immediate NRE because Nothing doesn't have a .Text property


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



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



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


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



                              Public Class Form1

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


                              Partial Remedy



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



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

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


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



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



                              "Button2" resides on a Panel



                              Remedy



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



                              ' Declaration
                              Private NameBoxes As TextBox()

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

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




                              Function Returning Nothing



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

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

                              Return bars
                              End Function


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



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


                              Remedy



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



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




                              Poorly Implemented Try/Catch



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



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

                              'More code fooing and barring

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

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


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



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



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



                              Remedy



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





                              DBNull is not the same as Nothing



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


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




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




                              Remedy



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


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



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


                              Example 2



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

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


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



                              If getFoo IsNot Nothing Then...




                              Controls



                              Dim chk As CheckBox

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


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



                              Remedy



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


                              The DataGridView



                              The DGV has a few quirks seen periodically:



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


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



                              Remedy



                              Name the columns manually, or reference by index:



                              dgvBooks.Columns(0).Visible = True


                              Example 2 — Beware of the NewRow



                              xlWorkSheet = xlWorkBook.Sheets("sheet1")

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


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



                              Remedy



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



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


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





                              My.Settings (StringCollection)



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



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


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



                              Remedy



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



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


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





                              Key Points



                              You probably forgot the New operator.



                              or



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



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





                              MSDN NullReference Exception






                              share













                              share


                              share








                              edited Jul 18 '18 at 6:31


























                              community wiki





                              12 revs, 7 users 76%
                              Plutonix
























                                  220














                                  Another scenario is when you cast a null object into a value type. For example, the code below:



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


                                  It will throw a NullReferenceException on the cast. It seems quite obvious in the above sample, but this can happen in more "late-binding" intricate scenarios where the null object has been returned from some code you don't own, and the cast is for example generated by some automatic system.



                                  One example of this is this simple ASP.NET binding fragment with the Calendar control:



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


                                  Here, SelectedDate is in fact a property - of DateTime type - of the Calendar Web Control type, and the binding could perfectly return something null. The implicit ASP.NET Generator will create a piece of code that will be equivalent to the cast code above. And this will raise a NullReferenceException that is quite difficult to spot, because it lies in ASP.NET generated code which compiles fine...





                                  share





















                                  • 7





                                    Great catch. One-liner way to avoid: DateTime x = (DateTime) o as DateTime? ?? defaultValue;

                                    – Serge Shultz
                                    Jun 29 '15 at 11:07
















                                  220














                                  Another scenario is when you cast a null object into a value type. For example, the code below:



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


                                  It will throw a NullReferenceException on the cast. It seems quite obvious in the above sample, but this can happen in more "late-binding" intricate scenarios where the null object has been returned from some code you don't own, and the cast is for example generated by some automatic system.



                                  One example of this is this simple ASP.NET binding fragment with the Calendar control:



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


                                  Here, SelectedDate is in fact a property - of DateTime type - of the Calendar Web Control type, and the binding could perfectly return something null. The implicit ASP.NET Generator will create a piece of code that will be equivalent to the cast code above. And this will raise a NullReferenceException that is quite difficult to spot, because it lies in ASP.NET generated code which compiles fine...





                                  share





















                                  • 7





                                    Great catch. One-liner way to avoid: DateTime x = (DateTime) o as DateTime? ?? defaultValue;

                                    – Serge Shultz
                                    Jun 29 '15 at 11:07














                                  220












                                  220








                                  220







                                  Another scenario is when you cast a null object into a value type. For example, the code below:



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


                                  It will throw a NullReferenceException on the cast. It seems quite obvious in the above sample, but this can happen in more "late-binding" intricate scenarios where the null object has been returned from some code you don't own, and the cast is for example generated by some automatic system.



                                  One example of this is this simple ASP.NET binding fragment with the Calendar control:



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


                                  Here, SelectedDate is in fact a property - of DateTime type - of the Calendar Web Control type, and the binding could perfectly return something null. The implicit ASP.NET Generator will create a piece of code that will be equivalent to the cast code above. And this will raise a NullReferenceException that is quite difficult to spot, because it lies in ASP.NET generated code which compiles fine...





                                  share















                                  Another scenario is when you cast a null object into a value type. For example, the code below:



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


                                  It will throw a NullReferenceException on the cast. It seems quite obvious in the above sample, but this can happen in more "late-binding" intricate scenarios where the null object has been returned from some code you don't own, and the cast is for example generated by some automatic system.



                                  One example of this is this simple ASP.NET binding fragment with the Calendar control:



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


                                  Here, SelectedDate is in fact a property - of DateTime type - of the Calendar Web Control type, and the binding could perfectly return something null. The implicit ASP.NET Generator will create a piece of code that will be equivalent to the cast code above. And this will raise a NullReferenceException that is quite difficult to spot, because it lies in ASP.NET generated code which compiles fine...






                                  share













                                  share


                                  share








                                  edited Jul 18 '18 at 6:28


























                                  community wiki





                                  7 revs, 4 users 79%
                                  Simon Mourier









                                  • 7





                                    Great catch. One-liner way to avoid: DateTime x = (DateTime) o as DateTime? ?? defaultValue;

                                    – Serge Shultz
                                    Jun 29 '15 at 11:07














                                  • 7





                                    Great catch. One-liner way to avoid: DateTime x = (DateTime) o as DateTime? ?? defaultValue;

                                    – Serge Shultz
                                    Jun 29 '15 at 11:07








                                  7




                                  7





                                  Great catch. One-liner way to avoid: DateTime x = (DateTime) o as DateTime? ?? defaultValue;

                                  – Serge Shultz
                                  Jun 29 '15 at 11:07





                                  Great catch. One-liner way to avoid: DateTime x = (DateTime) o as DateTime? ?? defaultValue;

                                  – Serge Shultz
                                  Jun 29 '15 at 11:07











                                  148














                                  It means that the variable in question is pointed at nothing. I could generate this like so:



                                  SqlConnection connection = null;
                                  connection.Open();


                                  That will throw the error because while I've declared the variable "connection", it's not pointed to anything. When I try to call the member "Open", there's no reference for it to resolve, and it will throw the error.



                                  To avoid this error:




                                  1. Always initialize your objects before you try to do anything with them.

                                  2. If you're not sure whether the object is null, check it with object == null.


                                  JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error, allowing you to put in a null check. This error is the number one source of bugs, IMHO.





                                  share





















                                  • 2





                                    JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error. This is incorrect. I have a solution without that detection, yet the code occasionally results to the exception. I suspect it's occasionally undetectable - by them at least - when multithreading is involved, but I can't comment further because I didn't identify the location of my bug yet.

                                    – j riv
                                    Jan 21 '18 at 7:42













                                  • But how to solve it when the NullReferenceException comes in usign HttpContext.Current.Responce.Clear(). It is not getting solved by any of the above solution. because while creating its object object of HttpContext then a error comes "Overload resolution failed because no accessible 'New' accepts this Number of arguments.

                                    – Sunny Sandeep
                                    Feb 2 '18 at 11:14


















                                  148














                                  It means that the variable in question is pointed at nothing. I could generate this like so:



                                  SqlConnection connection = null;
                                  connection.Open();


                                  That will throw the error because while I've declared the variable "connection", it's not pointed to anything. When I try to call the member "Open", there's no reference for it to resolve, and it will throw the error.



                                  To avoid this error:




                                  1. Always initialize your objects before you try to do anything with them.

                                  2. If you're not sure whether the object is null, check it with object == null.


                                  JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error, allowing you to put in a null check. This error is the number one source of bugs, IMHO.





                                  share





















                                  • 2





                                    JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error. This is incorrect. I have a solution without that detection, yet the code occasionally results to the exception. I suspect it's occasionally undetectable - by them at least - when multithreading is involved, but I can't comment further because I didn't identify the location of my bug yet.

                                    – j riv
                                    Jan 21 '18 at 7:42













                                  • But how to solve it when the NullReferenceException comes in usign HttpContext.Current.Responce.Clear(). It is not getting solved by any of the above solution. because while creating its object object of HttpContext then a error comes "Overload resolution failed because no accessible 'New' accepts this Number of arguments.

                                    – Sunny Sandeep
                                    Feb 2 '18 at 11:14
















                                  148












                                  148








                                  148







                                  It means that the variable in question is pointed at nothing. I could generate this like so:



                                  SqlConnection connection = null;
                                  connection.Open();


                                  That will throw the error because while I've declared the variable "connection", it's not pointed to anything. When I try to call the member "Open", there's no reference for it to resolve, and it will throw the error.



                                  To avoid this error:




                                  1. Always initialize your objects before you try to do anything with them.

                                  2. If you're not sure whether the object is null, check it with object == null.


                                  JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error, allowing you to put in a null check. This error is the number one source of bugs, IMHO.





                                  share















                                  It means that the variable in question is pointed at nothing. I could generate this like so:



                                  SqlConnection connection = null;
                                  connection.Open();


                                  That will throw the error because while I've declared the variable "connection", it's not pointed to anything. When I try to call the member "Open", there's no reference for it to resolve, and it will throw the error.



                                  To avoid this error:




                                  1. Always initialize your objects before you try to do anything with them.

                                  2. If you're not sure whether the object is null, check it with object == null.


                                  JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error, allowing you to put in a null check. This error is the number one source of bugs, IMHO.






                                  share













                                  share


                                  share








                                  edited Jun 10 '15 at 10:01


























                                  community wiki





                                  Chris B. Behrens









                                  • 2





                                    JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error. This is incorrect. I have a solution without that detection, yet the code occasionally results to the exception. I suspect it's occasionally undetectable - by them at least - when multithreading is involved, but I can't comment further because I didn't identify the location of my bug yet.

                                    – j riv
                                    Jan 21 '18 at 7:42













                                  • But how to solve it when the NullReferenceException comes in usign HttpContext.Current.Responce.Clear(). It is not getting solved by any of the above solution. because while creating its object object of HttpContext then a error comes "Overload resolution failed because no accessible 'New' accepts this Number of arguments.

                                    – Sunny Sandeep
                                    Feb 2 '18 at 11:14
















                                  • 2





                                    JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error. This is incorrect. I have a solution without that detection, yet the code occasionally results to the exception. I suspect it's occasionally undetectable - by them at least - when multithreading is involved, but I can't comment further because I didn't identify the location of my bug yet.

                                    – j riv
                                    Jan 21 '18 at 7:42













                                  • But how to solve it when the NullReferenceException comes in usign HttpContext.Current.Responce.Clear(). It is not getting solved by any of the above solution. because while creating its object object of HttpContext then a error comes "Overload resolution failed because no accessible 'New' accepts this Number of arguments.

                                    – Sunny Sandeep
                                    Feb 2 '18 at 11:14










                                  2




                                  2





                                  JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error. This is incorrect. I have a solution without that detection, yet the code occasionally results to the exception. I suspect it's occasionally undetectable - by them at least - when multithreading is involved, but I can't comment further because I didn't identify the location of my bug yet.

                                  – j riv
                                  Jan 21 '18 at 7:42







                                  JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error. This is incorrect. I have a solution without that detection, yet the code occasionally results to the exception. I suspect it's occasionally undetectable - by them at least - when multithreading is involved, but I can't comment further because I didn't identify the location of my bug yet.

                                  – j riv
                                  Jan 21 '18 at 7:42















                                  But how to solve it when the NullReferenceException comes in usign HttpContext.Current.Responce.Clear(). It is not getting solved by any of the above solution. because while creating its object object of HttpContext then a error comes "Overload resolution failed because no accessible 'New' accepts this Number of arguments.

                                  – Sunny Sandeep
                                  Feb 2 '18 at 11:14







                                  But how to solve it when the NullReferenceException comes in usign HttpContext.Current.Responce.Clear(). It is not getting solved by any of the above solution. because while creating its object object of HttpContext then a error comes "Overload resolution failed because no accessible 'New' accepts this Number of arguments.

                                  – Sunny Sandeep
                                  Feb 2 '18 at 11:14













                                  139














                                  It means your code used an object reference variable that was set to null (i.e. it did not reference an actual object instance).



                                  To prevent the error, objects that could be null should be tested for null before being used.



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




                                  share






























                                    139














                                    It means your code used an object reference variable that was set to null (i.e. it did not reference an actual object instance).



                                    To prevent the error, objects that could be null should be tested for null before being used.



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




                                    share




























                                      139












                                      139








                                      139







                                      It means your code used an object reference variable that was set to null (i.e. it did not reference an actual object instance).



                                      To prevent the error, objects that could be null should be tested for null before being used.



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




                                      share















                                      It means your code used an object reference variable that was set to null (i.e. it did not reference an actual object instance).



                                      To prevent the error, objects that could be null should be tested for null before being used.



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





                                      share













                                      share


                                      share








                                      edited Jul 28 '14 at 19:17


























                                      community wiki





                                      Jonathan Wood
























                                          93














                                          Be aware that regardless of the scenario, the cause is always the same in .NET:




                                          You are trying to use a reference variable whose value is Nothing/null. When the value is Nothing/null for the reference variable, that means it is not actually holding a reference to an instance of any object that exists on the heap.



                                          You either never assigned something to the variable, never created an instance of the value assigned to the variable, or you set the variable equal to Nothing/null manually, or you called a function that set the variable to Nothing/null for you.






                                          share






























                                            93














                                            Be aware that regardless of the scenario, the cause is always the same in .NET:




                                            You are trying to use a reference variable whose value is Nothing/null. When the value is Nothing/null for the reference variable, that means it is not actually holding a reference to an instance of any object that exists on the heap.



                                            You either never assigned something to the variable, never created an instance of the value assigned to the variable, or you set the variable equal to Nothing/null manually, or you called a function that set the variable to Nothing/null for you.






                                            share




























                                              93












                                              93








                                              93







                                              Be aware that regardless of the scenario, the cause is always the same in .NET:




                                              You are trying to use a reference variable whose value is Nothing/null. When the value is Nothing/null for the reference variable, that means it is not actually holding a reference to an instance of any object that exists on the heap.



                                              You either never assigned something to the variable, never created an instance of the value assigned to the variable, or you set the variable equal to Nothing/null manually, or you called a function that set the variable to Nothing/null for you.






                                              share















                                              Be aware that regardless of the scenario, the cause is always the same in .NET:




                                              You are trying to use a reference variable whose value is Nothing/null. When the value is Nothing/null for the reference variable, that means it is not actually holding a reference to an instance of any object that exists on the heap.



                                              You either never assigned something to the variable, never created an instance of the value assigned to the variable, or you set the variable equal to Nothing/null manually, or you called a function that set the variable to Nothing/null for you.







                                              share













                                              share


                                              share








                                              edited Jun 10 '15 at 10:03


























                                              community wiki





                                              code master
























                                                  82














                                                  An example of this exception being thrown is: When you are trying to check something, that is null.



                                                  For example:



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

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


                                                  The .NET runtime will throw a NullReferenceException when you attempt to perform an action on something which hasn't been instantiated i.e. the code above.



                                                  In comparison to an ArgumentNullException which is typically thrown as a defensive measure if a method expects that what is being passed to it is not null.



                                                  More information is in C# NullReferenceException and Null Parameter.





                                                  share






























                                                    82














                                                    An example of this exception being thrown is: When you are trying to check something, that is null.



                                                    For example:



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

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


                                                    The .NET runtime will throw a NullReferenceException when you attempt to perform an action on something which hasn't been instantiated i.e. the code above.



                                                    In comparison to an ArgumentNullException which is typically thrown as a defensive measure if a method expects that what is being passed to it is not null.



                                                    More information is in C# NullReferenceException and Null Parameter.





                                                    share




























                                                      82












                                                      82








                                                      82







                                                      An example of this exception being thrown is: When you are trying to check something, that is null.



                                                      For example:



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

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


                                                      The .NET runtime will throw a NullReferenceException when you attempt to perform an action on something which hasn't been instantiated i.e. the code above.



                                                      In comparison to an ArgumentNullException which is typically thrown as a defensive measure if a method expects that what is being passed to it is not null.



                                                      More information is in C# NullReferenceException and Null Parameter.





                                                      share















                                                      An example of this exception being thrown is: When you are trying to check something, that is null.



                                                      For example:



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

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


                                                      The .NET runtime will throw a NullReferenceException when you attempt to perform an action on something which hasn't been instantiated i.e. the code above.



                                                      In comparison to an ArgumentNullException which is typically thrown as a defensive measure if a method expects that what is being passed to it is not null.



                                                      More information is in C# NullReferenceException and Null Parameter.






                                                      share













                                                      share


                                                      share








                                                      edited Jul 18 '18 at 6:32


























                                                      community wiki





                                                      4 revs, 3 users 71%
                                                      Alex KeySmith
























                                                          79














                                                          If you have not initialized a reference type, and you want to set or read one of its properties, it will throw a NullReferenceException.



                                                          Example:



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


                                                          You can simply avoid this by checking if the variable is not null:



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


                                                          To fully understand why a NullReferenceException is thrown, it is important to know the difference between value types and reference types.



                                                          So, if you're dealing with value types, NullReferenceExceptions can not occur. Though you need to keep alert when dealing with reference types!



                                                          Only reference types, as the name is suggesting, can hold references or point literally to nothing (or 'null'). Whereas value types always contain a value.



                                                          Reference types (these ones must be checked):




                                                          • dynamic

                                                          • object

                                                          • string


                                                          Value types (you can simply ignore these ones):




                                                          • Numeric types

                                                          • Integral types

                                                          • Floating-point types

                                                          • decimal

                                                          • bool

                                                          • User defined structs





                                                          share





















                                                          • 5





                                                            -1: since the question is "What is a NullReferenceException", value types are not relevant.

                                                            – John Saunders
                                                            May 16 '13 at 22:00






                                                          • 18





                                                            @John Saunders: I disagree. As a software developer it is really important to be able to distinguish between value and reference types. else people will end up checking if integers are null.

                                                            – Fabian Bigler
                                                            May 16 '13 at 22:28








                                                          • 4





                                                            True, just not in the context of this question.

                                                            – John Saunders
                                                            May 16 '13 at 22:44






                                                          • 3





                                                            Thanks for the hint. I improved it a bit and added an example at the top. I still think mentioning Reference & Value Types is useful.

                                                            – Fabian Bigler
                                                            May 16 '13 at 23:02






                                                          • 4





                                                            I think you haven't added anything that wasn't in the other answers, since the question pre-supposes a reference type.

                                                            – John Saunders
                                                            May 18 '13 at 23:24
















                                                          79














                                                          If you have not initialized a reference type, and you want to set or read one of its properties, it will throw a NullReferenceException.



                                                          Example:



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


                                                          You can simply avoid this by checking if the variable is not null:



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


                                                          To fully understand why a NullReferenceException is thrown, it is important to know the difference between value types and reference types.



                                                          So, if you're dealing with value types, NullReferenceExceptions can not occur. Though you need to keep alert when dealing with reference types!



                                                          Only reference types, as the name is suggesting, can hold references or point literally to nothing (or 'null'). Whereas value types always contain a value.



                                                          Reference types (these ones must be checked):




                                                          • dynamic

                                                          • object

                                                          • string


                                                          Value types (you can simply ignore these ones):




                                                          • Numeric types

                                                          • Integral types

                                                          • Floating-point types

                                                          • decimal

                                                          • bool

                                                          • User defined structs





                                                          share





















                                                          • 5





                                                            -1: since the question is "What is a NullReferenceException", value types are not relevant.

                                                            – John Saunders
                                                            May 16 '13 at 22:00






                                                          • 18





                                                            @John Saunders: I disagree. As a software developer it is really important to be able to distinguish between value and reference types. else people will end up checking if integers are null.

                                                            – Fabian Bigler
                                                            May 16 '13 at 22:28








                                                          • 4





                                                            True, just not in the context of this question.

                                                            – John Saunders
                                                            May 16 '13 at 22:44






                                                          • 3





                                                            Thanks for the hint. I improved it a bit and added an example at the top. I still think mentioning Reference & Value Types is useful.

                                                            – Fabian Bigler
                                                            May 16 '13 at 23:02






                                                          • 4





                                                            I think you haven't added anything that wasn't in the other answers, since the question pre-supposes a reference type.

                                                            – John Saunders
                                                            May 18 '13 at 23:24














                                                          79












                                                          79








                                                          79







                                                          If you have not initialized a reference type, and you want to set or read one of its properties, it will throw a NullReferenceException.



                                                          Example:



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


                                                          You can simply avoid this by checking if the variable is not null:



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


                                                          To fully understand why a NullReferenceException is thrown, it is important to know the difference between value types and reference types.



                                                          So, if you're dealing with value types, NullReferenceExceptions can not occur. Though you need to keep alert when dealing with reference types!



                                                          Only reference types, as the name is suggesting, can hold references or point literally to nothing (or 'null'). Whereas value types always contain a value.



                                                          Reference types (these ones must be checked):




                                                          • dynamic

                                                          • object

                                                          • string


                                                          Value types (you can simply ignore these ones):




                                                          • Numeric types

                                                          • Integral types

                                                          • Floating-point types

                                                          • decimal

                                                          • bool

                                                          • User defined structs





                                                          share















                                                          If you have not initialized a reference type, and you want to set or read one of its properties, it will throw a NullReferenceException.



                                                          Example:



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


                                                          You can simply avoid this by checking if the variable is not null:



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


                                                          To fully understand why a NullReferenceException is thrown, it is important to know the difference between value types and reference types.



                                                          So, if you're dealing with value types, NullReferenceExceptions can not occur. Though you need to keep alert when dealing with reference types!



                                                          Only reference types, as the name is suggesting, can hold references or point literally to nothing (or 'null'). Whereas value types always contain a value.



                                                          Reference types (these ones must be checked):




                                                          • dynamic

                                                          • object

                                                          • string


                                                          Value types (you can simply ignore these ones):




                                                          • Numeric types

                                                          • Integral types

                                                          • Floating-point types

                                                          • decimal

                                                          • bool

                                                          • User defined structs






                                                          share













                                                          share


                                                          share








                                                          edited Jul 18 '18 at 6:33


























                                                          community wiki





                                                          11 revs, 4 users 85%
                                                          Fabian Bigler









                                                          • 5





                                                            -1: since the question is "What is a NullReferenceException", value types are not relevant.

                                                            – John Saunders
                                                            May 16 '13 at 22:00






                                                          • 18





                                                            @John Saunders: I disagree. As a software developer it is really important to be able to distinguish between value and reference types. else people will end up checking if integers are null.

                                                            – Fabian Bigler
                                                            May 16 '13 at 22:28








                                                          • 4





                                                            True, just not in the context of this question.

                                                            – John Saunders
                                                            May 16 '13 at 22:44






                                                          • 3





                                                            Thanks for the hint. I improved it a bit and added an example at the top. I still think mentioning Reference & Value Types is useful.

                                                            – Fabian Bigler
                                                            May 16 '13 at 23:02






                                                          • 4





                                                            I think you haven't added anything that wasn't in the other answers, since the question pre-supposes a reference type.

                                                            – John Saunders
                                                            May 18 '13 at 23:24














                                                          • 5





                                                            -1: since the question is "What is a NullReferenceException", value types are not relevant.

                                                            – John Saunders
                                                            May 16 '13 at 22:00






                                                          • 18





                                                            @John Saunders: I disagree. As a software developer it is really important to be able to distinguish between value and reference types. else people will end up checking if integers are null.

                                                            – Fabian Bigler
                                                            May 16 '13 at 22:28








                                                          • 4





                                                            True, just not in the context of this question.

                                                            – John Saunders
                                                            May 16 '13 at 22:44






                                                          • 3





                                                            Thanks for the hint. I improved it a bit and added an example at the top. I still think mentioning Reference & Value Types is useful.

                                                            – Fabian Bigler
                                                            May 16 '13 at 23:02






                                                          • 4





                                                            I think you haven't added anything that wasn't in the other answers, since the question pre-supposes a reference type.

                                                            – John Saunders
                                                            May 18 '13 at 23:24








                                                          5




                                                          5





                                                          -1: since the question is "What is a NullReferenceException", value types are not relevant.

                                                          – John Saunders
                                                          May 16 '13 at 22:00





                                                          -1: since the question is "What is a NullReferenceException", value types are not relevant.

                                                          – John Saunders
                                                          May 16 '13 at 22:00




                                                          18




                                                          18





                                                          @John Saunders: I disagree. As a software developer it is really important to be able to distinguish between value and reference types. else people will end up checking if integers are null.

                                                          – Fabian Bigler
                                                          May 16 '13 at 22:28







                                                          @John Saunders: I disagree. As a software developer it is really important to be able to distinguish between value and reference types. else people will end up checking if integers are null.

                                                          – Fabian Bigler
                                                          May 16 '13 at 22:28






                                                          4




                                                          4





                                                          True, just not in the context of this question.

                                                          – John Saunders
                                                          May 16 '13 at 22:44





                                                          True, just not in the context of this question.

                                                          – John Saunders
                                                          May 16 '13 at 22:44




                                                          3




                                                          3





                                                          Thanks for the hint. I improved it a bit and added an example at the top. I still think mentioning Reference & Value Types is useful.

                                                          – Fabian Bigler
                                                          May 16 '13 at 23:02





                                                          Thanks for the hint. I improved it a bit and added an example at the top. I still think mentioning Reference & Value Types is useful.

                                                          – Fabian Bigler
                                                          May 16 '13 at 23:02




                                                          4




                                                          4





                                                          I think you haven't added anything that wasn't in the other answers, since the question pre-supposes a reference type.

                                                          – John Saunders
                                                          May 18 '13 at 23:24





                                                          I think you haven't added anything that wasn't in the other answers, since the question pre-supposes a reference type.

                                                          – John Saunders
                                                          May 18 '13 at 23:24











                                                          70














                                                          Another case where NullReferenceExceptions can happen is the (incorrect) use of the as operator:



                                                          class Book {
                                                          public string Name { get; set; }
                                                          }
                                                          class Car { }

                                                          Car mycar = new Car();
                                                          Book mybook = mycar as Book; // Incompatible conversion --> mybook = null

                                                          Console.WriteLine(mybook.Name); // NullReferenceException


                                                          Here, Book and Car are incompatible types; a Car cannot be converted/cast to a Book. When this cast fails, as returns null. Using mybook after this causes a NullReferenceException.



                                                          In general, you should use a cast or as, as follows:



                                                          If you are expecting the type conversion to always succeed (ie. you know what the object should be ahead of time), then you should use a cast:



                                                          ComicBook cb = (ComicBook)specificBook;


                                                          If you are unsure of the type, but you want to try to use it as a specific type, then use as:



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




                                                          share





















                                                          • 2





                                                            This can happen a lot when unboxing a variable. I find it happens often in event handlers after I changed the type of the UI element but forget to update the code-behind.

                                                            – Brendan
                                                            Feb 19 '14 at 0:24


















                                                          70














                                                          Another case where NullReferenceExceptions can happen is the (incorrect) use of the as operator:



                                                          class Book {
                                                          public string Name { get; set; }
                                                          }
                                                          class Car { }

                                                          Car mycar = new Car();
                                                          Book mybook = mycar as Book; // Incompatible conversion --> mybook = null

                                                          Console.WriteLine(mybook.Name); // NullReferenceException


                                                          Here, Book and Car are incompatible types; a Car cannot be converted/cast to a Book. When this cast fails, as returns null. Using mybook after this causes a NullReferenceException.



                                                          In general, you should use a cast or as, as follows:



                                                          If you are expecting the type conversion to always succeed (ie. you know what the object should be ahead of time), then you should use a cast:



                                                          ComicBook cb = (ComicBook)specificBook;


                                                          If you are unsure of the type, but you want to try to use it as a specific type, then use as:



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




                                                          share





















                                                          • 2





                                                            This can happen a lot when unboxing a variable. I find it happens often in event handlers after I changed the type of the UI element but forget to update the code-behind.

                                                            – Brendan
                                                            Feb 19 '14 at 0:24
















                                                          70












                                                          70








                                                          70







                                                          Another case where NullReferenceExceptions can happen is the (incorrect) use of the as operator:



                                                          class Book {
                                                          public string Name { get; set; }
                                                          }
                                                          class Car { }

                                                          Car mycar = new Car();
                                                          Book mybook = mycar as Book; // Incompatible conversion --> mybook = null

                                                          Console.WriteLine(mybook.Name); // NullReferenceException


                                                          Here, Book and Car are incompatible types; a Car cannot be converted/cast to a Book. When this cast fails, as returns null. Using mybook after this causes a NullReferenceException.



                                                          In general, you should use a cast or as, as follows:



                                                          If you are expecting the type conversion to always succeed (ie. you know what the object should be ahead of time), then you should use a cast:



                                                          ComicBook cb = (ComicBook)specificBook;


                                                          If you are unsure of the type, but you want to try to use it as a specific type, then use as:



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




                                                          share















                                                          Another case where NullReferenceExceptions can happen is the (incorrect) use of the as operator:



                                                          class Book {
                                                          public string Name { get; set; }
                                                          }
                                                          class Car { }

                                                          Car mycar = new Car();
                                                          Book mybook = mycar as Book; // Incompatible conversion --> mybook = null

                                                          Console.WriteLine(mybook.Name); // NullReferenceException


                                                          Here, Book and Car are incompatible types; a Car cannot be converted/cast to a Book. When this cast fails, as returns null. Using mybook after this causes a NullReferenceException.



                                                          In general, you should use a cast or as, as follows:



                                                          If you are expecting the type conversion to always succeed (ie. you know what the object should be ahead of time), then you should use a cast:



                                                          ComicBook cb = (ComicBook)specificBook;


                                                          If you are unsure of the type, but you want to try to use it as a specific type, then use as:



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





                                                          share













                                                          share


                                                          share








                                                          edited Jul 18 '18 at 6:33


























                                                          community wiki





                                                          2 revs, 2 users 89%
                                                          Jonathon Reinhart









                                                          • 2





                                                            This can happen a lot when unboxing a variable. I find it happens often in event handlers after I changed the type of the UI element but forget to update the code-behind.

                                                            – Brendan
                                                            Feb 19 '14 at 0:24
















                                                          • 2





                                                            This can happen a lot when unboxing a variable. I find it happens often in event handlers after I changed the type of the UI element but forget to update the code-behind.

                                                            – Brendan
                                                            Feb 19 '14 at 0:24










                                                          2




                                                          2





                                                          This can happen a lot when unboxing a variable. I find it happens often in event handlers after I changed the type of the UI element but forget to update the code-behind.

                                                          – Brendan
                                                          Feb 19 '14 at 0:24







                                                          This can happen a lot when unboxing a variable. I find it happens often in event handlers after I changed the type of the UI element but forget to update the code-behind.

                                                          – Brendan
                                                          Feb 19 '14 at 0:24













                                                          62














                                                          You are using the object that contains the null value reference. So it's giving a null exception. In the example the string value is null and when checking its length, the exception occurred.



                                                          Example:



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


                                                          The exception error is:




                                                          Unhandled Exception:



                                                          System.NullReferenceException: Object reference not set to an instance
                                                          of an object. at Program.Main()






                                                          share





















                                                          • 1





                                                            How profound! I never considered the 'null' constant a reference value. So this is how C# abstracts a "NullPointer" huh ? B/c as I recall in C++, a NPE can be caused by dereferencing an uninitialized pointer (ie, ref type in c#) whose default value happens to be an address that is not allocated to that process (many cases this would be 0, especially in later versions of C++ that did auto-initialization, which belongs to the OS - f with it and die beeotch (or just catch the sigkill the OS attacks your process with)).

                                                            – samis
                                                            Jul 31 '13 at 18:55


















                                                          62














                                                          You are using the object that contains the null value reference. So it's giving a null exception. In the example the string value is null and when checking its length, the exception occurred.



                                                          Example:



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


                                                          The exception error is:




                                                          Unhandled Exception:



                                                          System.NullReferenceException: Object reference not set to an instance
                                                          of an object. at Program.Main()






                                                          share





















                                                          • 1





                                                            How profound! I never considered the 'null' constant a reference value. So this is how C# abstracts a "NullPointer" huh ? B/c as I recall in C++, a NPE can be caused by dereferencing an uninitialized pointer (ie, ref type in c#) whose default value happens to be an address that is not allocated to that process (many cases this would be 0, especially in later versions of C++ that did auto-initialization, which belongs to the OS - f with it and die beeotch (or just catch the sigkill the OS attacks your process with)).

                                                            – samis
                                                            Jul 31 '13 at 18:55
















                                                          62












                                                          62








                                                          62







                                                          You are using the object that contains the null value reference. So it's giving a null exception. In the example the string value is null and when checking its length, the exception occurred.



                                                          Example:



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


                                                          The exception error is:




                                                          Unhandled Exception:



                                                          System.NullReferenceException: Object reference not set to an instance
                                                          of an object. at Program.Main()






                                                          share















                                                          You are using the object that contains the null value reference. So it's giving a null exception. In the example the string value is null and when checking its length, the exception occurred.



                                                          Example:



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


                                                          The exception error is:




                                                          Unhandled Exception:



                                                          System.NullReferenceException: Object reference not set to an instance
                                                          of an object. at Program.Main()







                                                          share













                                                          share


                                                          share








                                                          edited May 25 '15 at 13:41


























                                                          community wiki





                                                          user1814380









                                                          • 1





                                                            How profound! I never considered the 'null' constant a reference value. So this is how C# abstracts a "NullPointer" huh ? B/c as I recall in C++, a NPE can be caused by dereferencing an uninitialized pointer (ie, ref type in c#) whose default value happens to be an address that is not allocated to that process (many cases this would be 0, especially in later versions of C++ that did auto-initialization, which belongs to the OS - f with it and die beeotch (or just catch the sigkill the OS attacks your process with)).

                                                            – samis
                                                            Jul 31 '13 at 18:55
















                                                          • 1





                                                            How profound! I never considered the 'null' constant a reference value. So this is how C# abstracts a "NullPointer" huh ? B/c as I recall in C++, a NPE can be caused by dereferencing an uninitialized pointer (ie, ref type in c#) whose default value happens to be an address that is not allocated to that process (many cases this would be 0, especially in later versions of C++ that did auto-initialization, which belongs to the OS - f with it and die beeotch (or just catch the sigkill the OS attacks your process with)).

                                                            – samis
                                                            Jul 31 '13 at 18:55










                                                          1




                                                          1





                                                          How profound! I never considered the 'null' constant a reference value. So this is how C# abstracts a "NullPointer" huh ? B/c as I recall in C++, a NPE can be caused by dereferencing an uninitialized pointer (ie, ref type in c#) whose default value happens to be an address that is not allocated to that process (many cases this would be 0, especially in later versions of C++ that did auto-initialization, which belongs to the OS - f with it and die beeotch (or just catch the sigkill the OS attacks your process with)).

                                                          – samis
                                                          Jul 31 '13 at 18:55







                                                          How profound! I never considered the 'null' constant a reference value. So this is how C# abstracts a "NullPointer" huh ? B/c as I recall in C++, a NPE can be caused by dereferencing an uninitialized pointer (ie, ref type in c#) whose default value happens to be an address that is not allocated to that process (many cases this would be 0, especially in later versions of C++ that did auto-initialization, which belongs to the OS - f with it and die beeotch (or just catch the sigkill the OS attacks your process with)).

                                                          – samis
                                                          Jul 31 '13 at 18:55













                                                          51














                                                          Simon Mourier gave this example:



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


                                                          where an unboxing conversion (cast) from object (or from one of the classes System.ValueType or System.Enum, or from an interface type) to a value type (other than Nullable<>) in itself gives the NullReferenceException.



                                                          In the other direction, a boxing conversion from a Nullable<> which has HasValue equal to false to a reference type, can give a null reference which can then later lead to a NullReferenceException. The classic example is:



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


                                                          Sometimes the boxing happens in another way. For example with this non-generic extension method:



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


                                                          the following code will be problematic:



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


                                                          These cases arise because of the special rules the runtime uses when boxing Nullable<> instances.





                                                          share






























                                                            51














                                                            Simon Mourier gave this example:



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


                                                            where an unboxing conversion (cast) from object (or from one of the classes System.ValueType or System.Enum, or from an interface type) to a value type (other than Nullable<>) in itself gives the NullReferenceException.



                                                            In the other direction, a boxing conversion from a Nullable<> which has HasValue equal to false to a reference type, can give a null reference which can then later lead to a NullReferenceException. The classic example is:



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


                                                            Sometimes the boxing happens in another way. For example with this non-generic extension method:



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


                                                            the following code will be problematic:



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


                                                            These cases arise because of the special rules the runtime uses when boxing Nullable<> instances.





                                                            share




























                                                              51












                                                              51








                                                              51







                                                              Simon Mourier gave this example:



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


                                                              where an unboxing conversion (cast) from object (or from one of the classes System.ValueType or System.Enum, or from an interface type) to a value type (other than Nullable<>) in itself gives the NullReferenceException.



                                                              In the other direction, a boxing conversion from a Nullable<> which has HasValue equal to false to a reference type, can give a null reference which can then later lead to a NullReferenceException. The classic example is:



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


                                                              Sometimes the boxing happens in another way. For example with this non-generic extension method:



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


                                                              the following code will be problematic:



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


                                                              These cases arise because of the special rules the runtime uses when boxing Nullable<> instances.





                                                              share















                                                              Simon Mourier gave this example:



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


                                                              where an unboxing conversion (cast) from object (or from one of the classes System.ValueType or System.Enum, or from an interface type) to a value type (other than Nullable<>) in itself gives the NullReferenceException.



                                                              In the other direction, a boxing conversion from a Nullable<> which has HasValue equal to false to a reference type, can give a null reference which can then later lead to a NullReferenceException. The classic example is:



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


                                                              Sometimes the boxing happens in another way. For example with this non-generic extension method:



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


                                                              the following code will be problematic:



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


                                                              These cases arise because of the special rules the runtime uses when boxing Nullable<> instances.






                                                              share













                                                              share


                                                              share








                                                              edited Jul 18 '18 at 6:34


























                                                              community wiki





                                                              4 revs, 3 users 81%
                                                              Jeppe Stig Nielsen
























                                                                  51














                                                                  While what causes a NullReferenceExceptions and approaches to avoid/fix such an exception have been addressed in other answers, what many programmers haven't learned yet is how to independently debug such exceptions during development.



                                                                  In Visual Studio this is usually easy thanks to the Visual Studio Debugger.





                                                                  First, make sure that the correct error is going to be caught - see
                                                                  How do I allow breaking on 'System.NullReferenceException' in VS2010? Note1



                                                                  Then either Start with Debugging (F5) or Attach [the VS Debugger] to Running Process. On occasion it may be useful to use Debugger.Break, which will prompt to launch the debugger.



                                                                  Now, when the NullReferenceException is thrown (or unhandled) the debugger will stop (remember the rule set above?) on the line on which the exception occurred. Sometimes the error will be easy to spot.



                                                                  For instance,
                                                                  in the following line the only code that can cause the exception is if myString evaluates to null. This can be verified by looking at the Watch Window or running expressions in the Immediate Window.



                                                                  var x = myString.Trim();


                                                                  In more advanced cases, such as the following, you'll need to use one of the techniques above (Watch or Immediate Windows) to inspect the expressions to determine if str1 was null or if str2 was null.



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


                                                                  Once where the exception is throw has been located, it's usually trivial to reason backwards to find out where the null value was [incorrectly] introduced --



                                                                  Take the time required to understand the cause of the exception. Inspect for null expressions. Inspect the previous expressions which could have resulted in such null expressions. Add breakpoints and step through the program as appropriate. Use the debugger.





                                                                  1 If Break on Throws is too aggressive and the debugger stops on an NPE in the .NET or 3rd-party library, Break on User-Unhandled can be used to limit the exceptions caught. Additionally, VS2012 introduces Just My Code which I recommend enabling as well.




                                                                  If you are debugging with Just My Code enabled, the behavior is slightly different. With Just My Code enabled, the debugger ignores first-chance common language runtime (CLR) exceptions that are thrown outside of My Code and do not pass through My Code






                                                                  share






























                                                                    51














                                                                    While what causes a NullReferenceExceptions and approaches to avoid/fix such an exception have been addressed in other answers, what many programmers haven't learned yet is how to independently debug such exceptions during development.



                                                                    In Visual Studio this is usually easy thanks to the Visual Studio Debugger.





                                                                    First, make sure that the correct error is going to be caught - see
                                                                    How do I allow breaking on 'System.NullReferenceException' in VS2010? Note1



                                                                    Then either Start with Debugging (F5) or Attach [the VS Debugger] to Running Process. On occasion it may be useful to use Debugger.Break, which will prompt to launch the debugger.



                                                                    Now, when the NullReferenceException is thrown (or unhandled) the debugger will stop (remember the rule set above?) on the line on which the exception occurred. Sometimes the error will be easy to spot.



                                                                    For instance,
                                                                    in the following line the only code that can cause the exception is if myString evaluates to null. This can be verified by looking at the Watch Window or running expressions in the Immediate Window.



                                                                    var x = myString.Trim();


                                                                    In more advanced cases, such as the following, you'll need to use one of the techniques above (Watch or Immediate Windows) to inspect the expressions to determine if str1 was null or if str2 was null.



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


                                                                    Once where the exception is throw has been located, it's usually trivial to reason backwards to find out where the null value was [incorrectly] introduced --



                                                                    Take the time required to understand the cause of the exception. Inspect for null expressions. Inspect the previous expressions which could have resulted in such null expressions. Add breakpoints and step through the program as appropriate. Use the debugger.





                                                                    1 If Break on Throws is too aggressive and the debugger stops on an NPE in the .NET or 3rd-party library, Break on User-Unhandled can be used to limit the exceptions caught. Additionally, VS2012 introduces Just My Code which I recommend enabling as well.




                                                                    If you are debugging with Just My Code enabled, the behavior is slightly different. With Just My Code enabled, the debugger ignores first-chance common language runtime (CLR) exceptions that are thrown outside of My Code and do not pass through My Code






                                                                    share




























                                                                      51












                                                                      51








                                                                      51







                                                                      While what causes a NullReferenceExceptions and approaches to avoid/fix such an exception have been addressed in other answers, what many programmers haven't learned yet is how to independently debug such exceptions during development.



                                                                      In Visual Studio this is usually easy thanks to the Visual Studio Debugger.





                                                                      First, make sure that the correct error is going to be caught - see
                                                                      How do I allow breaking on 'System.NullReferenceException' in VS2010? Note1



                                                                      Then either Start with Debugging (F5) or Attach [the VS Debugger] to Running Process. On occasion it may be useful to use Debugger.Break, which will prompt to launch the debugger.



                                                                      Now, when the NullReferenceException is thrown (or unhandled) the debugger will stop (remember the rule set above?) on the line on which the exception occurred. Sometimes the error will be easy to spot.



                                                                      For instance,
                                                                      in the following line the only code that can cause the exception is if myString evaluates to null. This can be verified by looking at the Watch Window or running expressions in the Immediate Window.



                                                                      var x = myString.Trim();


                                                                      In more advanced cases, such as the following, you'll need to use one of the techniques above (Watch or Immediate Windows) to inspect the expressions to determine if str1 was null or if str2 was null.



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


                                                                      Once where the exception is throw has been located, it's usually trivial to reason backwards to find out where the null value was [incorrectly] introduced --



                                                                      Take the time required to understand the cause of the exception. Inspect for null expressions. Inspect the previous expressions which could have resulted in such null expressions. Add breakpoints and step through the program as appropriate. Use the debugger.





                                                                      1 If Break on Throws is too aggressive and the debugger stops on an NPE in the .NET or 3rd-party library, Break on User-Unhandled can be used to limit the exceptions caught. Additionally, VS2012 introduces Just My Code which I recommend enabling as well.




                                                                      If you are debugging with Just My Code enabled, the behavior is slightly different. With Just My Code enabled, the debugger ignores first-chance common language runtime (CLR) exceptions that are thrown outside of My Code and do not pass through My Code






                                                                      share















                                                                      While what causes a NullReferenceExceptions and approaches to avoid/fix such an exception have been addressed in other answers, what many programmers haven't learned yet is how to independently debug such exceptions during development.



                                                                      In Visual Studio this is usually easy thanks to the Visual Studio Debugger.





                                                                      First, make sure that the correct error is going to be caught - see
                                                                      How do I allow breaking on 'System.NullReferenceException' in VS2010? Note1



                                                                      Then either Start with Debugging (F5) or Attach [the VS Debugger] to Running Process. On occasion it may be useful to use Debugger.Break, which will prompt to launch the debugger.



                                                                      Now, when the NullReferenceException is thrown (or unhandled) the debugger will stop (remember the rule set above?) on the line on which the exception occurred. Sometimes the error will be easy to spot.



                                                                      For instance,
                                                                      in the following line the only code that can cause the exception is if myString evaluates to null. This can be verified by looking at the Watch Window or running expressions in the Immediate Window.



                                                                      var x = myString.Trim();


                                                                      In more advanced cases, such as the following, you'll need to use one of the techniques above (Watch or Immediate Windows) to inspect the expressions to determine if str1 was null or if str2 was null.



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


                                                                      Once where the exception is throw has been located, it's usually trivial to reason backwards to find out where the null value was [incorrectly] introduced --



                                                                      Take the time required to understand the cause of the exception. Inspect for null expressions. Inspect the previous expressions which could have resulted in such null expressions. Add breakpoints and step through the program as appropriate. Use the debugger.





                                                                      1 If Break on Throws is too aggressive and the debugger stops on an NPE in the .NET or 3rd-party library, Break on User-Unhandled can be used to limit the exceptions caught. Additionally, VS2012 introduces Just My Code which I recommend enabling as well.




                                                                      If you are debugging with Just My Code enabled, the behavior is slightly different. With Just My Code enabled, the debugger ignores first-chance common language runtime (CLR) exceptions that are thrown outside of My Code and do not pass through My Code







                                                                      share













                                                                      share


                                                                      share








                                                                      edited Jul 18 '18 at 6:42


























                                                                      community wiki





                                                                      10 revs, 2 users 78%
                                                                      user2864740
























                                                                          39














                                                                          Adding a case when the class name for entity used in entity framework is same as class name for a web form code-behind file.



                                                                          Suppose you have a web form Contact.aspx whose codebehind class is Contact and you have an entity name Contact.



                                                                          Then following code will throw a NullReferenceException when you call context.SaveChanges()



                                                                          Contact contact = new Contact { Name = "Abhinav"};
                                                                          var context = new DataContext();
                                                                          context.Contacts.Add(contact);
                                                                          context.SaveChanges(); // NullReferenceException at this line


                                                                          For the sake of completeness DataContext class



                                                                          public class DataContext : DbContext 
                                                                          {
                                                                          public DbSet<Contact> Contacts {get; set;}
                                                                          }


                                                                          and Contact entity class. Sometimes entity classes are partial classes so that you can extend them in other files too.



                                                                          public partial class Contact 
                                                                          {
                                                                          public string Name {get; set;}
                                                                          }


                                                                          The error occurs when both the entity and codebehind class are in same namespace.
                                                                          To fix this, rename the entity class or the codebehind class for Contact.aspx.



                                                                          Reason
                                                                          I am still not sure about the reason. But whenever any of the entity class will extend System.Web.UI.Page this error occurs.



                                                                          For discussion have a look at NullReferenceException in DbContext.saveChanges()





                                                                          share






























                                                                            39














                                                                            Adding a case when the class name for entity used in entity framework is same as class name for a web form code-behind file.



                                                                            Suppose you have a web form Contact.aspx whose codebehind class is Contact and you have an entity name Contact.



                                                                            Then following code will throw a NullReferenceException when you call context.SaveChanges()



                                                                            Contact contact = new Contact { Name = "Abhinav"};
                                                                            var context = new DataContext();
                                                                            context.Contacts.Add(contact);
                                                                            context.SaveChanges(); // NullReferenceException at this line


                                                                            For the sake of completeness DataContext class



                                                                            public class DataContext : DbContext 
                                                                            {
                                                                            public DbSet<Contact> Contacts {get; set;}
                                                                            }


                                                                            and Contact entity class. Sometimes entity classes are partial classes so that you can extend them in other files too.



                                                                            public partial class Contact 
                                                                            {
                                                                            public string Name {get; set;}
                                                                            }


                                                                            The error occurs when both the entity and codebehind class are in same namespace.
                                                                            To fix this, rename the entity class or the codebehind class for Contact.aspx.



                                                                            Reason
                                                                            I am still not sure about the reason. But whenever any of the entity class will extend System.Web.UI.Page this error occurs.



                                                                            For discussion have a look at NullReferenceException in DbContext.saveChanges()





                                                                            share




























                                                                              39












                                                                              39








                                                                              39







                                                                              Adding a case when the class name for entity used in entity framework is same as class name for a web form code-behind file.



                                                                              Suppose you have a web form Contact.aspx whose codebehind class is Contact and you have an entity name Contact.



                                                                              Then following code will throw a NullReferenceException when you call context.SaveChanges()



                                                                              Contact contact = new Contact { Name = "Abhinav"};
                                                                              var context = new DataContext();
                                                                              context.Contacts.Add(contact);
                                                                              context.SaveChanges(); // NullReferenceException at this line


                                                                              For the sake of completeness DataContext class



                                                                              public class DataContext : DbContext 
                                                                              {
                                                                              public DbSet<Contact> Contacts {get; set;}
                                                                              }


                                                                              and Contact entity class. Sometimes entity classes are partial classes so that you can extend them in other files too.



                                                                              public partial class Contact 
                                                                              {
                                                                              public string Name {get; set;}
                                                                              }


                                                                              The error occurs when both the entity and codebehind class are in same namespace.
                                                                              To fix this, rename the entity class or the codebehind class for Contact.aspx.



                                                                              Reason
                                                                              I am still not sure about the reason. But whenever any of the entity class will extend System.Web.UI.Page this error occurs.



                                                                              For discussion have a look at NullReferenceException in DbContext.saveChanges()





                                                                              share















                                                                              Adding a case when the class name for entity used in entity framework is same as class name for a web form code-behind file.



                                                                              Suppose you have a web form Contact.aspx whose codebehind class is Contact and you have an entity name Contact.



                                                                              Then following code will throw a NullReferenceException when you call context.SaveChanges()



                                                                              Contact contact = new Contact { Name = "Abhinav"};
                                                                              var context = new DataContext();
                                                                              context.Contacts.Add(contact);
                                                                              context.SaveChanges(); // NullReferenceException at this line


                                                                              For the sake of completeness DataContext class



                                                                              public class DataContext : DbContext 
                                                                              {
                                                                              public DbSet<Contact> Contacts {get; set;}
                                                                              }


                                                                              and Contact entity class. Sometimes entity classes are partial classes so that you can extend them in other files too.



                                                                              public partial class Contact 
                                                                              {
                                                                              public string Name {get; set;}
                                                                              }


                                                                              The error occurs when both the entity and codebehind class are in same namespace.
                                                                              To fix this, rename the entity class or the codebehind class for Contact.aspx.



                                                                              Reason
                                                                              I am still not sure about the reason. But whenever any of the entity class will extend System.Web.UI.Page this error occurs.



                                                                              For discussion have a look at NullReferenceException in DbContext.saveChanges()






                                                                              share













                                                                              share


                                                                              share








                                                                              edited May 23 '17 at 12:34


























                                                                              community wiki





                                                                              3 revs
                                                                              AbhinavRanjan
























                                                                                  38














                                                                                  Another general case where one might receive this exception involves mocking classes during unit testing. Regardless of the mocking framework being used, you must ensure that all appropriate levels of the class hierarchy are properly mocked. In particular, all properties of HttpContext which are referenced by the code under test must be mocked.



                                                                                  See "NullReferenceException thrown when testing custom AuthorizationAttribute" for a somewhat verbose example.





                                                                                  share






























                                                                                    38














                                                                                    Another general case where one might receive this exception involves mocking classes during unit testing. Regardless of the mocking framework being used, you must ensure that all appropriate levels of the class hierarchy are properly mocked. In particular, all properties of HttpContext which are referenced by the code under test must be mocked.



                                                                                    See "NullReferenceException thrown when testing custom AuthorizationAttribute" for a somewhat verbose example.





                                                                                    share




























                                                                                      38












                                                                                      38








                                                                                      38







                                                                                      Another general case where one might receive this exception involves mocking classes during unit testing. Regardless of the mocking framework being used, you must ensure that all appropriate levels of the class hierarchy are properly mocked. In particular, all properties of HttpContext which are referenced by the code under test must be mocked.



                                                                                      See "NullReferenceException thrown when testing custom AuthorizationAttribute" for a somewhat verbose example.





                                                                                      share















                                                                                      Another general case where one might receive this exception involves mocking classes during unit testing. Regardless of the mocking framework being used, you must ensure that all appropriate levels of the class hierarchy are properly mocked. In particular, all properties of HttpContext which are referenced by the code under test must be mocked.



                                                                                      See "NullReferenceException thrown when testing custom AuthorizationAttribute" for a somewhat verbose example.






                                                                                      share













                                                                                      share


                                                                                      share








                                                                                      edited May 23 '17 at 12:34


























                                                                                      community wiki





                                                                                      3 revs
                                                                                      John Saunders
























                                                                                          37














                                                                                          I have a different perspective to answering this. This sort of answers "what else can I do to avoid it?"



                                                                                          When working across different layers, for example in an MVC application, a controller needs services to call business operations. In such scenarios Dependency Injection Container can be used to initialize the services to avoid the NullReferenceException. So that means you don't need to worry about checking for null and just call the services from the controller as though they will always to available (and initialized) as either a singleton or a prototype.



                                                                                          public class MyController
                                                                                          {
                                                                                          private ServiceA serviceA;
                                                                                          private ServiceB serviceB;

                                                                                          public MyController(ServiceA serviceA, ServiceB serviceB)
                                                                                          {
                                                                                          this.serviceA = serviceA;
                                                                                          this.serviceB = serviceB;
                                                                                          }

                                                                                          public void MyMethod()
                                                                                          {
                                                                                          // We don't need to check null because the dependency injection container
                                                                                          // injects it, provided you took care of bootstrapping it.
                                                                                          var someObject = serviceA.DoThis();
                                                                                          }
                                                                                          }




                                                                                          share





















                                                                                          • 5





                                                                                            -1: this only handles a single scenario - that of uninitialized dependencies. This is a minority scenario for NullReferenceException. Most cases are simple misunderstanding of how objects work. Next most frequent are other situations where the developer assumed that the object would be initialized automatically.

                                                                                            – John Saunders
                                                                                            Mar 7 '14 at 0:06






                                                                                          • 3





                                                                                            All others have already been answered above.

                                                                                            – Mukus
                                                                                            Mar 7 '14 at 0:23











                                                                                          • Dependency injection is not generally used in order to avoid NullReferenceException. I don't believe that you have found a general scenario here. In any case, if you edit your answer to be more in the style of stackoverflow.com/a/15232518/76337, then I will remove the downvote.

                                                                                            – John Saunders
                                                                                            Mar 7 '14 at 0:30
















                                                                                          37














                                                                                          I have a different perspective to answering this. This sort of answers "what else can I do to avoid it?"



                                                                                          When working across different layers, for example in an MVC application, a controller needs services to call business operations. In such scenarios Dependency Injection Container can be used to initialize the services to avoid the NullReferenceException. So that means you don't need to worry about checking for null and just call the services from the controller as though they will always to available (and initialized) as either a singleton or a prototype.



                                                                                          public class MyController
                                                                                          {
                                                                                          private ServiceA serviceA;
                                                                                          private ServiceB serviceB;

                                                                                          public MyController(ServiceA serviceA, ServiceB serviceB)
                                                                                          {
                                                                                          this.serviceA = serviceA;
                                                                                          this.serviceB = serviceB;
                                                                                          }

                                                                                          public void MyMethod()
                                                                                          {
                                                                                          // We don't need to check null because the dependency injection container
                                                                                          // injects it, provided you took care of bootstrapping it.
                                                                                          var someObject = serviceA.DoThis();
                                                                                          }
                                                                                          }




                                                                                          share





















                                                                                          • 5





                                                                                            -1: this only handles a single scenario - that of uninitialized dependencies. This is a minority scenario for NullReferenceException. Most cases are simple misunderstanding of how objects work. Next most frequent are other situations where the developer assumed that the object would be initialized automatically.

                                                                                            – John Saunders
                                                                                            Mar 7 '14 at 0:06






                                                                                          • 3





                                                                                            All others have already been answered above.

                                                                                            – Mukus
                                                                                            Mar 7 '14 at 0:23











                                                                                          • Dependency injection is not generally used in order to avoid NullReferenceException. I don't believe that you have found a general scenario here. In any case, if you edit your answer to be more in the style of stackoverflow.com/a/15232518/76337, then I will remove the downvote.

                                                                                            – John Saunders
                                                                                            Mar 7 '14 at 0:30














                                                                                          37












                                                                                          37








                                                                                          37







                                                                                          I have a different perspective to answering this. This sort of answers "what else can I do to avoid it?"



                                                                                          When working across different layers, for example in an MVC application, a controller needs services to call business operations. In such scenarios Dependency Injection Container can be used to initialize the services to avoid the NullReferenceException. So that means you don't need to worry about checking for null and just call the services from the controller as though they will always to available (and initialized) as either a singleton or a prototype.



                                                                                          public class MyController
                                                                                          {
                                                                                          private ServiceA serviceA;
                                                                                          private ServiceB serviceB;

                                                                                          public MyController(ServiceA serviceA, ServiceB serviceB)
                                                                                          {
                                                                                          this.serviceA = serviceA;
                                                                                          this.serviceB = serviceB;
                                                                                          }

                                                                                          public void MyMethod()
                                                                                          {
                                                                                          // We don't need to check null because the dependency injection container
                                                                                          // injects it, provided you took care of bootstrapping it.
                                                                                          var someObject = serviceA.DoThis();
                                                                                          }
                                                                                          }




                                                                                          share















                                                                                          I have a different perspective to answering this. This sort of answers "what else can I do to avoid it?"



                                                                                          When working across different layers, for example in an MVC application, a controller needs services to call business operations. In such scenarios Dependency Injection Container can be used to initialize the services to avoid the NullReferenceException. So that means you don't need to worry about checking for null and just call the services from the controller as though they will always to available (and initialized) as either a singleton or a prototype.



                                                                                          public class MyController
                                                                                          {
                                                                                          private ServiceA serviceA;
                                                                                          private ServiceB serviceB;

                                                                                          public MyController(ServiceA serviceA, ServiceB serviceB)
                                                                                          {
                                                                                          this.serviceA = serviceA;
                                                                                          this.serviceB = serviceB;
                                                                                          }

                                                                                          public void MyMethod()
                                                                                          {
                                                                                          // We don't need to check null because the dependency injection container
                                                                                          // injects it, provided you took care of bootstrapping it.
                                                                                          var someObject = serviceA.DoThis();
                                                                                          }
                                                                                          }





                                                                                          share













                                                                                          share


                                                                                          share








                                                                                          edited Dec 29 '16 at 8:44


























                                                                                          community wiki





                                                                                          4 revs, 3 users 72%
                                                                                          Mukus









                                                                                          • 5





                                                                                            -1: this only handles a single scenario - that of uninitialized dependencies. This is a minority scenario for NullReferenceException. Most cases are simple misunderstanding of how objects work. Next most frequent are other situations where the developer assumed that the object would be initialized automatically.

                                                                                            – John Saunders
                                                                                            Mar 7 '14 at 0:06






                                                                                          • 3





                                                                                            All others have already been answered above.

                                                                                            – Mukus
                                                                                            Mar 7 '14 at 0:23











                                                                                          • Dependency injection is not generally used in order to avoid NullReferenceException. I don't believe that you have found a general scenario here. In any case, if you edit your answer to be more in the style of stackoverflow.com/a/15232518/76337, then I will remove the downvote.

                                                                                            – John Saunders
                                                                                            Mar 7 '14 at 0:30














                                                                                          • 5





                                                                                            -1: this only handles a single scenario - that of uninitialized dependencies. This is a minority scenario for NullReferenceException. Most cases are simple misunderstanding of how objects work. Next most frequent are other situations where the developer assumed that the object would be initialized automatically.

                                                                                            – John Saunders
                                                                                            Mar 7 '14 at 0:06






                                                                                          • 3





                                                                                            All others have already been answered above.

                                                                                            – Mukus
                                                                                            Mar 7 '14 at 0:23











                                                                                          • Dependency injection is not generally used in order to avoid NullReferenceException. I don't believe that you have found a general scenario here. In any case, if you edit your answer to be more in the style of stackoverflow.com/a/15232518/76337, then I will remove the downvote.

                                                                                            – John Saunders
                                                                                            Mar 7 '14 at 0:30








                                                                                          5




                                                                                          5





                                                                                          -1: this only handles a single scenario - that of uninitialized dependencies. This is a minority scenario for NullReferenceException. Most cases are simple misunderstanding of how objects work. Next most frequent are other situations where the developer assumed that the object would be initialized automatically.

                                                                                          – John Saunders
                                                                                          Mar 7 '14 at 0:06





                                                                                          -1: this only handles a single scenario - that of uninitialized dependencies. This is a minority scenario for NullReferenceException. Most cases are simple misunderstanding of how objects work. Next most frequent are other situations where the developer assumed that the object would be initialized automatically.

                                                                                          – John Saunders
                                                                                          Mar 7 '14 at 0:06




                                                                                          3




                                                                                          3





                                                                                          All others have already been answered above.

                                                                                          – Mukus
                                                                                          Mar 7 '14 at 0:23





                                                                                          All others have already been answered above.

                                                                                          – Mukus
                                                                                          Mar 7 '14 at 0:23













                                                                                          Dependency injection is not generally used in order to avoid NullReferenceException. I don't believe that you have found a general scenario here. In any case, if you edit your answer to be more in the style of stackoverflow.com/a/15232518/76337, then I will remove the downvote.

                                                                                          – John Saunders
                                                                                          Mar 7 '14 at 0:30





                                                                                          Dependency injection is not generally used in order to avoid NullReferenceException. I don't believe that you have found a general scenario here. In any case, if you edit your answer to be more in the style of stackoverflow.com/a/15232518/76337, then I will remove the downvote.

                                                                                          – John Saunders
                                                                                          Mar 7 '14 at 0:30











                                                                                          35














                                                                                          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 property X in a class and later try to invoke one of its methods and X has a null value, then this will lead to NullReferenceException:



                                                                                          public X { get; set; }

                                                                                          public void InvokeX()
                                                                                          {
                                                                                          X.DoSomething(); // if X value is null, you will get a NullReferenceException
                                                                                          }


                                                                                          But if you set "property X must never have a null value" as method precondition, then you can prevent the scenario described before:



                                                                                          //Using code contracts:
                                                                                          [ContractInvariantMethod]
                                                                                          protected void ObjectInvariant ()
                                                                                          {
                                                                                          Contract.Invariant ( X != null );
                                                                                          //...
                                                                                          }


                                                                                          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.





                                                                                          share





















                                                                                          • 1





                                                                                            I thought to add this as no one mentioned this, and as far as it exists as an approach, my intention was to enrich the topic.

                                                                                            – Nick Louloudakis
                                                                                            Dec 26 '14 at 1:03








                                                                                          • 1





                                                                                            Thank you for enriching the topic. I have given my opinion of your addition. Now others can do the same.

                                                                                            – John Saunders
                                                                                            Dec 26 '14 at 1:05






                                                                                          • 1





                                                                                            I thought this was a worthwhile addition to the topic given that this is a highly viewed thread. I've heard of code contracts before and this was a good reminder to consider using them.

                                                                                            – VoteCoffee
                                                                                            Jan 8 '15 at 2:03
















                                                                                          35














                                                                                          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 property X in a class and later try to invoke one of its methods and X has a null value, then this will lead to NullReferenceException:



                                                                                          public X { get; set; }

                                                                                          public void InvokeX()
                                                                                          {
                                                                                          X.DoSomething(); // if X value is null, you will get a NullReferenceException
                                                                                          }


                                                                                          But if you set "property X must never have a null value" as method precondition, then you can prevent the scenario described before:



                                                                                          //Using code contracts:
                                                                                          [ContractInvariantMethod]
                                                                                          protected void ObjectInvariant ()
                                                                                          {
                                                                                          Contract.Invariant ( X != null );
                                                                                          //...
                                                                                          }


                                                                                          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.





                                                                                          share





















                                                                                          • 1





                                                                                            I thought to add this as no one mentioned this, and as far as it exists as an approach, my intention was to enrich the topic.

                                                                                            – Nick Louloudakis
                                                                                            Dec 26 '14 at 1:03








                                                                                          • 1





                                                                                            Thank you for enriching the topic. I have given my opinion of your addition. Now others can do the same.

                                                                                            – John Saunders
                                                                                            Dec 26 '14 at 1:05






                                                                                          • 1





                                                                                            I thought this was a worthwhile addition to the topic given that this is a highly viewed thread. I've heard of code contracts before and this was a good reminder to consider using them.

                                                                                            – VoteCoffee
                                                                                            Jan 8 '15 at 2:03














                                                                                          35












                                                                                          35








                                                                                          35







                                                                                          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 property X in a class and later try to invoke one of its methods and X has a null value, then this will lead to NullReferenceException:



                                                                                          public X { get; set; }

                                                                                          public void InvokeX()
                                                                                          {
                                                                                          X.DoSomething(); // if X value is null, you will get a NullReferenceException
                                                                                          }


                                                                                          But if you set "property X must never have a null value" as method precondition, then you can prevent the scenario described before:



                                                                                          //Using code contracts:
                                                                                          [ContractInvariantMethod]
                                                                                          protected void ObjectInvariant ()
                                                                                          {
                                                                                          Contract.Invariant ( X != null );
                                                                                          //...
                                                                                          }


                                                                                          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.





                                                                                          share















                                                                                          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 property X in a class and later try to invoke one of its methods and X has a null value, then this will lead to NullReferenceException:



                                                                                          public X { get; set; }

                                                                                          public void InvokeX()
                                                                                          {
                                                                                          X.DoSomething(); // if X value is null, you will get a NullReferenceException
                                                                                          }


                                                                                          But if you set "property X must never have a null value" as method precondition, then you can prevent the scenario described before:



                                                                                          //Using code contracts:
                                                                                          [ContractInvariantMethod]
                                                                                          protected void ObjectInvariant ()
                                                                                          {
                                                                                          Contract.Invariant ( X != null );
                                                                                          //...
                                                                                          }


                                                                                          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.






                                                                                          share













                                                                                          share


                                                                                          share








                                                                                          edited Dec 30 '16 at 15:56


























                                                                                          community wiki





                                                                                          4 revs
                                                                                          Nick L.









                                                                                          • 1





                                                                                            I thought to add this as no one mentioned this, and as far as it exists as an approach, my intention was to enrich the topic.

                                                                                            – Nick Louloudakis
                                                                                            Dec 26 '14 at 1:03








                                                                                          • 1





                                                                                            Thank you for enriching the topic. I have given my opinion of your addition. Now others can do the same.

                                                                                            – John Saunders
                                                                                            Dec 26 '14 at 1:05






                                                                                          • 1





                                                                                            I thought this was a worthwhile addition to the topic given that this is a highly viewed thread. I've heard of code contracts before and this was a good reminder to consider using them.

                                                                                            – VoteCoffee
                                                                                            Jan 8 '15 at 2:03














                                                                                          • 1





                                                                                            I thought to add this as no one mentioned this, and as far as it exists as an approach, my intention was to enrich the topic.

                                                                                            – Nick Louloudakis
                                                                                            Dec 26 '14 at 1:03








                                                                                          • 1





                                                                                            Thank you for enriching the topic. I have given my opinion of your addition. Now others can do the same.

                                                                                            – John Saunders
                                                                                            Dec 26 '14 at 1:05






                                                                                          • 1





                                                                                            I thought this was a worthwhile addition to the topic given that this is a highly viewed thread. I've heard of code contracts before and this was a good reminder to consider using them.

                                                                                            – VoteCoffee
                                                                                            Jan 8 '15 at 2:03








                                                                                          1




                                                                                          1





                                                                                          I thought to add this as no one mentioned this, and as far as it exists as an approach, my intention was to enrich the topic.

                                                                                          – Nick Louloudakis
                                                                                          Dec 26 '14 at 1:03







                                                                                          I thought to add this as no one mentioned this, and as far as it exists as an approach, my intention was to enrich the topic.

                                                                                          – Nick Louloudakis
                                                                                          Dec 26 '14 at 1:03






                                                                                          1




                                                                                          1





                                                                                          Thank you for enriching the topic. I have given my opinion of your addition. Now others can do the same.

                                                                                          – John Saunders
                                                                                          Dec 26 '14 at 1:05





                                                                                          Thank you for enriching the topic. I have given my opinion of your addition. Now others can do the same.

                                                                                          – John Saunders
                                                                                          Dec 26 '14 at 1:05




                                                                                          1




                                                                                          1





                                                                                          I thought this was a worthwhile addition to the topic given that this is a highly viewed thread. I've heard of code contracts before and this was a good reminder to consider using them.

                                                                                          – VoteCoffee
                                                                                          Jan 8 '15 at 2:03





                                                                                          I thought this was a worthwhile addition to the topic given that this is a highly viewed thread. I've heard of code contracts before and this was a good reminder to consider using them.

                                                                                          – VoteCoffee
                                                                                          Jan 8 '15 at 2:03











                                                                                          33














                                                                                          A NullReferenceException is thrown when we are trying to access Properties of a null object or when a string value becomes empty and we are trying to access string methods.



                                                                                          For example:





                                                                                          1. When a string method of an empty string accessed:



                                                                                            string str = string.Empty;
                                                                                            str.ToLower(); // throw null reference exception



                                                                                          2. When a property of a null object accessed:



                                                                                            Public Class Person {
                                                                                            public string Name { get; set; }
                                                                                            }
                                                                                            Person objPerson;
                                                                                            objPerson.Name /// throw Null refernce Exception






                                                                                          share





















                                                                                          • 2





                                                                                            This is incorrect. String.Empty.ToLower() will not throw a null reference exception. It represents an actual string, albeit an empty one (i.e. ""). Since this has an object to call ToLower() on, it would not make sense to throw a null reference exception there.

                                                                                            – Kjartan
                                                                                            Jul 24 '15 at 6:00


















                                                                                          33














                                                                                          A NullReferenceException is thrown when we are trying to access Properties of a null object or when a string value becomes empty and we are trying to access string methods.



                                                                                          For example:





                                                                                          1. When a string method of an empty string accessed:



                                                                                            string str = string.Empty;
                                                                                            str.ToLower(); // throw null reference exception



                                                                                          2. When a property of a null object accessed:



                                                                                            Public Class Person {
                                                                                            public string Name { get; set; }
                                                                                            }
                                                                                            Person objPerson;
                                                                                            objPerson.Name /// throw Null refernce Exception






                                                                                          share





















                                                                                          • 2





                                                                                            This is incorrect. String.Empty.ToLower() will not throw a null reference exception. It represents an actual string, albeit an empty one (i.e. ""). Since this has an object to call ToLower() on, it would not make sense to throw a null reference exception there.

                                                                                            – Kjartan
                                                                                            Jul 24 '15 at 6:00
















                                                                                          33












                                                                                          33








                                                                                          33







                                                                                          A NullReferenceException is thrown when we are trying to access Properties of a null object or when a string value becomes empty and we are trying to access string methods.



                                                                                          For example:





                                                                                          1. When a string method of an empty string accessed:



                                                                                            string str = string.Empty;
                                                                                            str.ToLower(); // throw null reference exception



                                                                                          2. When a property of a null object accessed:



                                                                                            Public Class Person {
                                                                                            public string Name { get; set; }
                                                                                            }
                                                                                            Person objPerson;
                                                                                            objPerson.Name /// throw Null refernce Exception






                                                                                          share















                                                                                          A NullReferenceException is thrown when we are trying to access Properties of a null object or when a string value becomes empty and we are trying to access string methods.



                                                                                          For example:





                                                                                          1. When a string method of an empty string accessed:



                                                                                            string str = string.Empty;
                                                                                            str.ToLower(); // throw null reference exception



                                                                                          2. When a property of a null object accessed:



                                                                                            Public Class Person {
                                                                                            public string Name { get; set; }
                                                                                            }
                                                                                            Person objPerson;
                                                                                            objPerson.Name /// throw Null refernce Exception







                                                                                          share













                                                                                          share


                                                                                          share








                                                                                          edited Jun 10 '15 at 6:04


























                                                                                          community wiki





                                                                                          Hemant Bavle









                                                                                          • 2





                                                                                            This is incorrect. String.Empty.ToLower() will not throw a null reference exception. It represents an actual string, albeit an empty one (i.e. ""). Since this has an object to call ToLower() on, it would not make sense to throw a null reference exception there.

                                                                                            – Kjartan
                                                                                            Jul 24 '15 at 6:00
















                                                                                          • 2





                                                                                            This is incorrect. String.Empty.ToLower() will not throw a null reference exception. It represents an actual string, albeit an empty one (i.e. ""). Since this has an object to call ToLower() on, it would not make sense to throw a null reference exception there.

                                                                                            – Kjartan
                                                                                            Jul 24 '15 at 6:00










                                                                                          2




                                                                                          2





                                                                                          This is incorrect. String.Empty.ToLower() will not throw a null reference exception. It represents an actual string, albeit an empty one (i.e. ""). Since this has an object to call ToLower() on, it would not make sense to throw a null reference exception there.

                                                                                          – Kjartan
                                                                                          Jul 24 '15 at 6:00







                                                                                          This is incorrect. String.Empty.ToLower() will not throw a null reference exception. It represents an actual string, albeit an empty one (i.e. ""). Since this has an object to call ToLower() on, it would not make sense to throw a null reference exception there.

                                                                                          – Kjartan
                                                                                          Jul 24 '15 at 6:00













                                                                                          29














                                                                                          TL;DR: Try using Html.Partial instead of Renderpage





                                                                                          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:



                                                                                          @{
                                                                                          MyEntity M = new MyEntity();
                                                                                          }
                                                                                          @RenderPage("_MyOtherView.cshtml", M); // error in _MyOtherView, the Model was Null


                                                                                          Debugging showed the model was Null inside MyOtherView. Until I changed it to:



                                                                                          @{
                                                                                          MyEntity M = new MyEntity();
                                                                                          }
                                                                                          @Html.Partial("_MyOtherView.cshtml", M);


                                                                                          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 under Html.Partial if it's inside a differently constructed foreach loop, even though it's not really an error:



                                                                                          @inherits System.Web.Mvc.WebViewPage
                                                                                          @{
                                                                                          ViewBag.Title = "Entity Index";
                                                                                          List<MyEntity> MyEntities = new List<MyEntity>();
                                                                                          MyEntities.Add(new MyEntity());
                                                                                          MyEntities.Add(new MyEntity());
                                                                                          MyEntities.Add(new MyEntity());
                                                                                          }
                                                                                          <div>
                                                                                          @{
                                                                                          foreach(var M in MyEntities)
                                                                                          {
                                                                                          // Squiggly lines below. Hovering says: cannot convert method group 'partial' to non-delegate type Object, did you intend to envoke the Method?
                                                                                          @Html.Partial("MyOtherView.cshtml");
                                                                                          }
                                                                                          }
                                                                                          </div>


                                                                                          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:



                                                                                          @foreach(var M in MyEntities){
                                                                                          ...
                                                                                          }


                                                                                          Although I have a feeling it was because Visual Studio was misreading the ampersands and brackets.





                                                                                          share


























                                                                                          • You wanted Html.Partial, not @Html.Partial

                                                                                            – John Saunders
                                                                                            Jul 24 '15 at 13:55











                                                                                          • Also, please show which line threw the exception, and why.

                                                                                            – John Saunders
                                                                                            Jul 24 '15 at 13:56











                                                                                          • The error occurred in MyOtherView.cshtml, which I did not include here, because the Model was not being properly sent in (it was Null), so I knew the error was with how I was sending the Model in.

                                                                                            – Travis Heeter
                                                                                            Jul 27 '15 at 11:44
















                                                                                          29














                                                                                          TL;DR: Try using Html.Partial instead of Renderpage





                                                                                          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:



                                                                                          @{
                                                                                          MyEntity M = new MyEntity();
                                                                                          }
                                                                                          @RenderPage("_MyOtherView.cshtml", M); // error in _MyOtherView, the Model was Null


                                                                                          Debugging showed the model was Null inside MyOtherView. Until I changed it to:



                                                                                          @{
                                                                                          MyEntity M = new MyEntity();
                                                                                          }
                                                                                          @Html.Partial("_MyOtherView.cshtml", M);


                                                                                          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 under Html.Partial if it's inside a differently constructed foreach loop, even though it's not really an error:



                                                                                          @inherits System.Web.Mvc.WebViewPage
                                                                                          @{
                                                                                          ViewBag.Title = "Entity Index";
                                                                                          List<MyEntity> MyEntities = new List<MyEntity>();
                                                                                          MyEntities.Add(new MyEntity());
                                                                                          MyEntities.Add(new MyEntity());
                                                                                          MyEntities.Add(new MyEntity());
                                                                                          }
                                                                                          <div>
                                                                                          @{
                                                                                          foreach(var M in MyEntities)
                                                                                          {
                                                                                          // Squiggly lines below. Hovering says: cannot convert method group 'partial' to non-delegate type Object, did you intend to envoke the Method?
                                                                                          @Html.Partial("MyOtherView.cshtml");
                                                                                          }
                                                                                          }
                                                                                          </div>


                                                                                          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:



                                                                                          @foreach(var M in MyEntities){
                                                                                          ...
                                                                                          }


                                                                                          Although I have a feeling it was because Visual Studio was misreading the ampersands and brackets.





                                                                                          share


























                                                                                          • You wanted Html.Partial, not @Html.Partial

                                                                                            – John Saunders
                                                                                            Jul 24 '15 at 13:55











                                                                                          • Also, please show which line threw the exception, and why.

                                                                                            – John Saunders
                                                                                            Jul 24 '15 at 13:56











                                                                                          • The error occurred in MyOtherView.cshtml, which I did not include here, because the Model was not being properly sent in (it was Null), so I knew the error was with how I was sending the Model in.

                                                                                            – Travis Heeter
                                                                                            Jul 27 '15 at 11:44














                                                                                          29












                                                                                          29








                                                                                          29







                                                                                          TL;DR: Try using Html.Partial instead of Renderpage





                                                                                          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:



                                                                                          @{
                                                                                          MyEntity M = new MyEntity();
                                                                                          }
                                                                                          @RenderPage("_MyOtherView.cshtml", M); // error in _MyOtherView, the Model was Null


                                                                                          Debugging showed the model was Null inside MyOtherView. Until I changed it to:



                                                                                          @{
                                                                                          MyEntity M = new MyEntity();
                                                                                          }
                                                                                          @Html.Partial("_MyOtherView.cshtml", M);


                                                                                          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 under Html.Partial if it's inside a differently constructed foreach loop, even though it's not really an error:



                                                                                          @inherits System.Web.Mvc.WebViewPage
                                                                                          @{
                                                                                          ViewBag.Title = "Entity Index";
                                                                                          List<MyEntity> MyEntities = new List<MyEntity>();
                                                                                          MyEntities.Add(new MyEntity());
                                                                                          MyEntities.Add(new MyEntity());
                                                                                          MyEntities.Add(new MyEntity());
                                                                                          }
                                                                                          <div>
                                                                                          @{
                                                                                          foreach(var M in MyEntities)
                                                                                          {
                                                                                          // Squiggly lines below. Hovering says: cannot convert method group 'partial' to non-delegate type Object, did you intend to envoke the Method?
                                                                                          @Html.Partial("MyOtherView.cshtml");
                                                                                          }
                                                                                          }
                                                                                          </div>


                                                                                          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:



                                                                                          @foreach(var M in MyEntities){
                                                                                          ...
                                                                                          }


                                                                                          Although I have a feeling it was because Visual Studio was misreading the ampersands and brackets.





                                                                                          share















                                                                                          TL;DR: Try using Html.Partial instead of Renderpage





                                                                                          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:



                                                                                          @{
                                                                                          MyEntity M = new MyEntity();
                                                                                          }
                                                                                          @RenderPage("_MyOtherView.cshtml", M); // error in _MyOtherView, the Model was Null


                                                                                          Debugging showed the model was Null inside MyOtherView. Until I changed it to:



                                                                                          @{
                                                                                          MyEntity M = new MyEntity();
                                                                                          }
                                                                                          @Html.Partial("_MyOtherView.cshtml", M);


                                                                                          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 under Html.Partial if it's inside a differently constructed foreach loop, even though it's not really an error:



                                                                                          @inherits System.Web.Mvc.WebViewPage
                                                                                          @{
                                                                                          ViewBag.Title = "Entity Index";
                                                                                          List<MyEntity> MyEntities = new List<MyEntity>();
                                                                                          MyEntities.Add(new MyEntity());
                                                                                          MyEntities.Add(new MyEntity());
                                                                                          MyEntities.Add(new MyEntity());
                                                                                          }
                                                                                          <div>
                                                                                          @{
                                                                                          foreach(var M in MyEntities)
                                                                                          {
                                                                                          // Squiggly lines below. Hovering says: cannot convert method group 'partial' to non-delegate type Object, did you intend to envoke the Method?
                                                                                          @Html.Partial("MyOtherView.cshtml");
                                                                                          }
                                                                                          }
                                                                                          </div>


                                                                                          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:



                                                                                          @foreach(var M in MyEntities){
                                                                                          ...
                                                                                          }


                                                                                          Although I have a feeling it was because Visual Studio was misreading the ampersands and brackets.






                                                                                          share













                                                                                          share


                                                                                          share








                                                                                          edited May 16 '16 at 8:35


























                                                                                          community wiki





                                                                                          4 revs, 2 users 91%
                                                                                          Travis Heeter














                                                                                          • You wanted Html.Partial, not @Html.Partial

                                                                                            – John Saunders
                                                                                            Jul 24 '15 at 13:55











                                                                                          • Also, please show which line threw the exception, and why.

                                                                                            – John Saunders
                                                                                            Jul 24 '15 at 13:56











                                                                                          • The error occurred in MyOtherView.cshtml, which I did not include here, because the Model was not being properly sent in (it was Null), so I knew the error was with how I was sending the Model in.

                                                                                            – Travis Heeter
                                                                                            Jul 27 '15 at 11:44



















                                                                                          • You wanted Html.Partial, not @Html.Partial

                                                                                            – John Saunders
                                                                                            Jul 24 '15 at 13:55











                                                                                          • Also, please show which line threw the exception, and why.

                                                                                            – John Saunders
                                                                                            Jul 24 '15 at 13:56











                                                                                          • The error occurred in MyOtherView.cshtml, which I did not include here, because the Model was not being properly sent in (it was Null), so I knew the error was with how I was sending the Model in.

                                                                                            – Travis Heeter
                                                                                            Jul 27 '15 at 11:44

















                                                                                          You wanted Html.Partial, not @Html.Partial

                                                                                          – John Saunders
                                                                                          Jul 24 '15 at 13:55





                                                                                          You wanted Html.Partial, not @Html.Partial

                                                                                          – John Saunders
                                                                                          Jul 24 '15 at 13:55













                                                                                          Also, please show which line threw the exception, and why.

                                                                                          – John Saunders
                                                                                          Jul 24 '15 at 13:56





                                                                                          Also, please show which line threw the exception, and why.

                                                                                          – John Saunders
                                                                                          Jul 24 '15 at 13:56













                                                                                          The error occurred in MyOtherView.cshtml, which I did not include here, because the Model was not being properly sent in (it was Null), so I knew the error was with how I was sending the Model in.

                                                                                          – Travis Heeter
                                                                                          Jul 27 '15 at 11:44





                                                                                          The error occurred in MyOtherView.cshtml, which I did not include here, because the Model was not being properly sent in (it was Null), so I knew the error was with how I was sending the Model in.

                                                                                          – Travis Heeter
                                                                                          Jul 27 '15 at 11:44











                                                                                          21














                                                                                          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.



                                                                                          public void DoSomething(MyObject obj) {
                                                                                          if(obj == null)
                                                                                          {
                                                                                          throw new ArgumentNullException("obj", "Need a reference to obj.");
                                                                                          }
                                                                                          }


                                                                                          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:



                                                                                          public void DoSometing([NotNull] obj)


                                                                                          By doing that and making PostSharp part of your build process obj will be checked for null at runtime. See: PostSharp null check



                                                                                          Plain 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>:



                                                                                          [System.Diagnostics.DebuggerNonUserCode]
                                                                                          public struct NotNull<T> where T: class
                                                                                          {
                                                                                          private T _value;

                                                                                          public T Value
                                                                                          {
                                                                                          get
                                                                                          {
                                                                                          if (_value == null)
                                                                                          {
                                                                                          throw new Exception("null value not allowed");
                                                                                          }

                                                                                          return _value;
                                                                                          }
                                                                                          set
                                                                                          {
                                                                                          if (value == null)
                                                                                          {
                                                                                          throw new Exception("null value not allowed.");
                                                                                          }

                                                                                          _value = value;
                                                                                          }
                                                                                          }

                                                                                          public static implicit operator T(NotNull<T> notNullValue)
                                                                                          {
                                                                                          return notNullValue.Value;
                                                                                          }

                                                                                          public static implicit operator NotNull<T>(T value)
                                                                                          {
                                                                                          return new NotNull<T> { Value = value };
                                                                                          }
                                                                                          }


                                                                                          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 allow null. Here are some examples:



                                                                                          NotNull<Person> person = null; // throws exception
                                                                                          NotNull<Person> person = new Person(); // OK
                                                                                          NotNull<Person> person = GetPerson(); // throws exception if GetPerson() returns null


                                                                                          NotNull<T> is implicitly cast to and from T so you can use it just about anywhere you need it. For example, you can pass a Person object to a method that takes a NotNull<Person>:



                                                                                          Person person = new Person { Name = "John" };
                                                                                          WriteName(person);

                                                                                          public static void WriteName(NotNull<Person> person)
                                                                                          {
                                                                                          Console.WriteLine(person.Value.Name);
                                                                                          }


                                                                                          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:



                                                                                          Person person = GetPerson();

                                                                                          public static NotNull<Person> GetPerson()
                                                                                          {
                                                                                          return new Person { Name = "John" };
                                                                                          }


                                                                                          Or you can even use it when the method just returns T (in this case Person) by doing a cast. For example, the following code would just like the code above:



                                                                                          Person person = (NotNull<Person>)GetPerson();

                                                                                          public static Person GetPerson()
                                                                                          {
                                                                                          return new Person { Name = "John" };
                                                                                          }


                                                                                          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:



                                                                                          [System.Diagnostics.DebuggerNonUserCode]
                                                                                          public static class NotNullExtension
                                                                                          {
                                                                                          public static T NotNull<T>(this T @this) where T: class
                                                                                          {
                                                                                          if (@this == null)
                                                                                          {
                                                                                          throw new Exception("null value not allowed");
                                                                                          }

                                                                                          return @this;
                                                                                          }
                                                                                          }


                                                                                          And here is an example of how it could be used:



                                                                                          var person = GetPerson().NotNull();


                                                                                          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 returns null.



                                                                                          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:



                                                                                          var address = country?.State?.County?.City;


                                                                                          Imagine that country is an object of type Country that has a property called State and so on. If country, State, County, or City is null then address will benull. Therefore you only have to check whetheraddressisnull`.



                                                                                          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 so int?.



                                                                                          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).





                                                                                          share





















                                                                                          • 2





                                                                                            Never throw NullReferenceException

                                                                                            – John Saunders
                                                                                            Mar 6 '16 at 20:35











                                                                                          • @JohnSaunders dare I ask why? (Seriously though why?)

                                                                                            – Luis Perez
                                                                                            Mar 7 '16 at 15:29






                                                                                          • 2





                                                                                            NullReferenceException is meant to be thrown by the CLR. It means that a reference to a null has occurred. It does not mean that a reference to a null would occur except that you cleverly checked first.

                                                                                            – John Saunders
                                                                                            Mar 7 '16 at 15:43











                                                                                          • I see your point about how that would be confusing. I've updated it to a regular exception for this example and a custom exception in GitHub.

                                                                                            – Luis Perez
                                                                                            Mar 7 '16 at 18:41













                                                                                          • Great answer for such a basic question. It's not so bad when it is your code that is failing. It's horrible when it's coming from deep inside some a commercial third party library you are relying on, and the customer support keeps insisting that it has to be your code that is causing the problem. And your not entirely sure it's not and the whole project is ground to halt.. I actually think this might make an appropriate epitaph for my tombstone: "Object reference not set to an instance of an object."

                                                                                            – Darrel Lee
                                                                                            May 3 '16 at 4:01
















                                                                                          21














                                                                                          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.



                                                                                          public void DoSomething(MyObject obj) {
                                                                                          if(obj == null)
                                                                                          {
                                                                                          throw new ArgumentNullException("obj", "Need a reference to obj.");
                                                                                          }
                                                                                          }


                                                                                          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:



                                                                                          public void DoSometing([NotNull] obj)


                                                                                          By doing that and making PostSharp part of your build process obj will be checked for null at runtime. See: PostSharp null check



                                                                                          Plain 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>:



                                                                                          [System.Diagnostics.DebuggerNonUserCode]
                                                                                          public struct NotNull<T> where T: class
                                                                                          {
                                                                                          private T _value;

                                                                                          public T Value
                                                                                          {
                                                                                          get
                                                                                          {
                                                                                          if (_value == null)
                                                                                          {
                                                                                          throw new Exception("null value not allowed");
                                                                                          }

                                                                                          return _value;
                                                                                          }
                                                                                          set
                                                                                          {
                                                                                          if (value == null)
                                                                                          {
                                                                                          throw new Exception("null value not allowed.");
                                                                                          }

                                                                                          _value = value;
                                                                                          }
                                                                                          }

                                                                                          public static implicit operator T(NotNull<T> notNullValue)
                                                                                          {
                                                                                          return notNullValue.Value;
                                                                                          }

                                                                                          public static implicit operator NotNull<T>(T value)
                                                                                          {
                                                                                          return new NotNull<T> { Value = value };
                                                                                          }
                                                                                          }


                                                                                          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 allow null. Here are some examples:



                                                                                          NotNull<Person> person = null; // throws exception
                                                                                          NotNull<Person> person = new Person(); // OK
                                                                                          NotNull<Person> person = GetPerson(); // throws exception if GetPerson() returns null


                                                                                          NotNull<T> is implicitly cast to and from T so you can use it just about anywhere you need it. For example, you can pass a Person object to a method that takes a NotNull<Person>:



                                                                                          Person person = new Person { Name = "John" };
                                                                                          WriteName(person);

                                                                                          public static void WriteName(NotNull<Person> person)
                                                                                          {
                                                                                          Console.WriteLine(person.Value.Name);
                                                                                          }


                                                                                          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:



                                                                                          Person person = GetPerson();

                                                                                          public static NotNull<Person> GetPerson()
                                                                                          {
                                                                                          return new Person { Name = "John" };
                                                                                          }


                                                                                          Or you can even use it when the method just returns T (in this case Person) by doing a cast. For example, the following code would just like the code above:



                                                                                          Person person = (NotNull<Person>)GetPerson();

                                                                                          public static Person GetPerson()
                                                                                          {
                                                                                          return new Person { Name = "John" };
                                                                                          }


                                                                                          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:



                                                                                          [System.Diagnostics.DebuggerNonUserCode]
                                                                                          public static class NotNullExtension
                                                                                          {
                                                                                          public static T NotNull<T>(this T @this) where T: class
                                                                                          {
                                                                                          if (@this == null)
                                                                                          {
                                                                                          throw new Exception("null value not allowed");
                                                                                          }

                                                                                          return @this;
                                                                                          }
                                                                                          }


                                                                                          And here is an example of how it could be used:



                                                                                          var person = GetPerson().NotNull();


                                                                                          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 returns null.



                                                                                          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:



                                                                                          var address = country?.State?.County?.City;


                                                                                          Imagine that country is an object of type Country that has a property called State and so on. If country, State, County, or City is null then address will benull. Therefore you only have to check whetheraddressisnull`.



                                                                                          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 so int?.



                                                                                          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).





                                                                                          share





















                                                                                          • 2





                                                                                            Never throw NullReferenceException

                                                                                            – John Saunders
                                                                                            Mar 6 '16 at 20:35











                                                                                          • @JohnSaunders dare I ask why? (Seriously though why?)

                                                                                            – Luis Perez
                                                                                            Mar 7 '16 at 15:29






                                                                                          • 2





                                                                                            NullReferenceException is meant to be thrown by the CLR. It means that a reference to a null has occurred. It does not mean that a reference to a null would occur except that you cleverly checked first.

                                                                                            – John Saunders
                                                                                            Mar 7 '16 at 15:43











                                                                                          • I see your point about how that would be confusing. I've updated it to a regular exception for this example and a custom exception in GitHub.

                                                                                            – Luis Perez
                                                                                            Mar 7 '16 at 18:41













                                                                                          • Great answer for such a basic question. It's not so bad when it is your code that is failing. It's horrible when it's coming from deep inside some a commercial third party library you are relying on, and the customer support keeps insisting that it has to be your code that is causing the problem. And your not entirely sure it's not and the whole project is ground to halt.. I actually think this might make an appropriate epitaph for my tombstone: "Object reference not set to an instance of an object."

                                                                                            – Darrel Lee
                                                                                            May 3 '16 at 4:01














                                                                                          21












                                                                                          21








                                                                                          21







                                                                                          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.



                                                                                          public void DoSomething(MyObject obj) {
                                                                                          if(obj == null)
                                                                                          {
                                                                                          throw new ArgumentNullException("obj", "Need a reference to obj.");
                                                                                          }
                                                                                          }


                                                                                          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:



                                                                                          public void DoSometing([NotNull] obj)


                                                                                          By doing that and making PostSharp part of your build process obj will be checked for null at runtime. See: PostSharp null check



                                                                                          Plain 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>:



                                                                                          [System.Diagnostics.DebuggerNonUserCode]
                                                                                          public struct NotNull<T> where T: class
                                                                                          {
                                                                                          private T _value;

                                                                                          public T Value
                                                                                          {
                                                                                          get
                                                                                          {
                                                                                          if (_value == null)
                                                                                          {
                                                                                          throw new Exception("null value not allowed");
                                                                                          }

                                                                                          return _value;
                                                                                          }
                                                                                          set
                                                                                          {
                                                                                          if (value == null)
                                                                                          {
                                                                                          throw new Exception("null value not allowed.");
                                                                                          }

                                                                                          _value = value;
                                                                                          }
                                                                                          }

                                                                                          public static implicit operator T(NotNull<T> notNullValue)
                                                                                          {
                                                                                          return notNullValue.Value;
                                                                                          }

                                                                                          public static implicit operator NotNull<T>(T value)
                                                                                          {
                                                                                          return new NotNull<T> { Value = value };
                                                                                          }
                                                                                          }


                                                                                          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 allow null. Here are some examples:



                                                                                          NotNull<Person> person = null; // throws exception
                                                                                          NotNull<Person> person = new Person(); // OK
                                                                                          NotNull<Person> person = GetPerson(); // throws exception if GetPerson() returns null


                                                                                          NotNull<T> is implicitly cast to and from T so you can use it just about anywhere you need it. For example, you can pass a Person object to a method that takes a NotNull<Person>:



                                                                                          Person person = new Person { Name = "John" };
                                                                                          WriteName(person);

                                                                                          public static void WriteName(NotNull<Person> person)
                                                                                          {
                                                                                          Console.WriteLine(person.Value.Name);
                                                                                          }


                                                                                          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:



                                                                                          Person person = GetPerson();

                                                                                          public static NotNull<Person> GetPerson()
                                                                                          {
                                                                                          return new Person { Name = "John" };
                                                                                          }


                                                                                          Or you can even use it when the method just returns T (in this case Person) by doing a cast. For example, the following code would just like the code above:



                                                                                          Person person = (NotNull<Person>)GetPerson();

                                                                                          public static Person GetPerson()
                                                                                          {
                                                                                          return new Person { Name = "John" };
                                                                                          }


                                                                                          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:



                                                                                          [System.Diagnostics.DebuggerNonUserCode]
                                                                                          public static class NotNullExtension
                                                                                          {
                                                                                          public static T NotNull<T>(this T @this) where T: class
                                                                                          {
                                                                                          if (@this == null)
                                                                                          {
                                                                                          throw new Exception("null value not allowed");
                                                                                          }

                                                                                          return @this;
                                                                                          }
                                                                                          }


                                                                                          And here is an example of how it could be used:



                                                                                          var person = GetPerson().NotNull();


                                                                                          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 returns null.



                                                                                          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:



                                                                                          var address = country?.State?.County?.City;


                                                                                          Imagine that country is an object of type Country that has a property called State and so on. If country, State, County, or City is null then address will benull. Therefore you only have to check whetheraddressisnull`.



                                                                                          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 so int?.



                                                                                          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).





                                                                                          share















                                                                                          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.



                                                                                          public void DoSomething(MyObject obj) {
                                                                                          if(obj == null)
                                                                                          {
                                                                                          throw new ArgumentNullException("obj", "Need a reference to obj.");
                                                                                          }
                                                                                          }


                                                                                          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:



                                                                                          public void DoSometing([NotNull] obj)


                                                                                          By doing that and making PostSharp part of your build process obj will be checked for null at runtime. See: PostSharp null check



                                                                                          Plain 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>:



                                                                                          [System.Diagnostics.DebuggerNonUserCode]
                                                                                          public struct NotNull<T> where T: class
                                                                                          {
                                                                                          private T _value;

                                                                                          public T Value
                                                                                          {
                                                                                          get
                                                                                          {
                                                                                          if (_value == null)
                                                                                          {
                                                                                          throw new Exception("null value not allowed");
                                                                                          }

                                                                                          return _value;
                                                                                          }
                                                                                          set
                                                                                          {
                                                                                          if (value == null)
                                                                                          {
                                                                                          throw new Exception("null value not allowed.");
                                                                                          }

                                                                                          _value = value;
                                                                                          }
                                                                                          }

                                                                                          public static implicit operator T(NotNull<T> notNullValue)
                                                                                          {
                                                                                          return notNullValue.Value;
                                                                                          }

                                                                                          public static implicit operator NotNull<T>(T value)
                                                                                          {
                                                                                          return new NotNull<T> { Value = value };
                                                                                          }
                                                                                          }


                                                                                          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 allow null. Here are some examples:



                                                                                          NotNull<Person> person = null; // throws exception
                                                                                          NotNull<Person> person = new Person(); // OK
                                                                                          NotNull<Person> person = GetPerson(); // throws exception if GetPerson() returns null


                                                                                          NotNull<T> is implicitly cast to and from T so you can use it just about anywhere you need it. For example, you can pass a Person object to a method that takes a NotNull<Person>:



                                                                                          Person person = new Person { Name = "John" };
                                                                                          WriteName(person);

                                                                                          public static void WriteName(NotNull<Person> person)
                                                                                          {
                                                                                          Console.WriteLine(person.Value.Name);
                                                                                          }


                                                                                          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:



                                                                                          Person person = GetPerson();

                                                                                          public static NotNull<Person> GetPerson()
                                                                                          {
                                                                                          return new Person { Name = "John" };
                                                                                          }


                                                                                          Or you can even use it when the method just returns T (in this case Person) by doing a cast. For example, the following code would just like the code above:



                                                                                          Person person = (NotNull<Person>)GetPerson();

                                                                                          public static Person GetPerson()
                                                                                          {
                                                                                          return new Person { Name = "John" };
                                                                                          }


                                                                                          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:



                                                                                          [System.Diagnostics.DebuggerNonUserCode]
                                                                                          public static class NotNullExtension
                                                                                          {
                                                                                          public static T NotNull<T>(this T @this) where T: class
                                                                                          {
                                                                                          if (@this == null)
                                                                                          {
                                                                                          throw new Exception("null value not allowed");
                                                                                          }

                                                                                          return @this;
                                                                                          }
                                                                                          }


                                                                                          And here is an example of how it could be used:



                                                                                          var person = GetPerson().NotNull();


                                                                                          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 returns null.



                                                                                          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:



                                                                                          var address = country?.State?.County?.City;


                                                                                          Imagine that country is an object of type Country that has a property called State and so on. If country, State, County, or City is null then address will benull. Therefore you only have to check whetheraddressisnull`.



                                                                                          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 so int?.



                                                                                          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).






                                                                                          share













                                                                                          share


                                                                                          share








                                                                                          edited Oct 29 '17 at 9:06


























                                                                                          community wiki





                                                                                          4 revs, 2 users 98%
                                                                                          Luis Perez









                                                                                          • 2





                                                                                            Never throw NullReferenceException

                                                                                            – John Saunders
                                                                                            Mar 6 '16 at 20:35











                                                                                          • @JohnSaunders dare I ask why? (Seriously though why?)

                                                                                            – Luis Perez
                                                                                            Mar 7 '16 at 15:29






                                                                                          • 2





                                                                                            NullReferenceException is meant to be thrown by the CLR. It means that a reference to a null has occurred. It does not mean that a reference to a null would occur except that you cleverly checked first.

                                                                                            – John Saunders
                                                                                            Mar 7 '16 at 15:43











                                                                                          • I see your point about how that would be confusing. I've updated it to a regular exception for this example and a custom exception in GitHub.

                                                                                            – Luis Perez
                                                                                            Mar 7 '16 at 18:41













                                                                                          • Great answer for such a basic question. It's not so bad when it is your code that is failing. It's horrible when it's coming from deep inside some a commercial third party library you are relying on, and the customer support keeps insisting that it has to be your code that is causing the problem. And your not entirely sure it's not and the whole project is ground to halt.. I actually think this might make an appropriate epitaph for my tombstone: "Object reference not set to an instance of an object."

                                                                                            – Darrel Lee
                                                                                            May 3 '16 at 4:01














                                                                                          • 2





                                                                                            Never throw NullReferenceException

                                                                                            – John Saunders
                                                                                            Mar 6 '16 at 20:35











                                                                                          • @JohnSaunders dare I ask why? (Seriously though why?)

                                                                                            – Luis Perez
                                                                                            Mar 7 '16 at 15:29






                                                                                          • 2





                                                                                            NullReferenceException is meant to be thrown by the CLR. It means that a reference to a null has occurred. It does not mean that a reference to a null would occur except that you cleverly checked first.

                                                                                            – John Saunders
                                                                                            Mar 7 '16 at 15:43











                                                                                          • I see your point about how that would be confusing. I've updated it to a regular exception for this example and a custom exception in GitHub.

                                                                                            – Luis Perez
                                                                                            Mar 7 '16 at 18:41













                                                                                          • Great answer for such a basic question. It's not so bad when it is your code that is failing. It's horrible when it's coming from deep inside some a commercial third party library you are relying on, and the customer support keeps insisting that it has to be your code that is causing the problem. And your not entirely sure it's not and the whole project is ground to halt.. I actually think this might make an appropriate epitaph for my tombstone: "Object reference not set to an instance of an object."

                                                                                            – Darrel Lee
                                                                                            May 3 '16 at 4:01








                                                                                          2




                                                                                          2





                                                                                          Never throw NullReferenceException

                                                                                          – John Saunders
                                                                                          Mar 6 '16 at 20:35





                                                                                          Never throw NullReferenceException

                                                                                          – John Saunders
                                                                                          Mar 6 '16 at 20:35













                                                                                          @JohnSaunders dare I ask why? (Seriously though why?)

                                                                                          – Luis Perez
                                                                                          Mar 7 '16 at 15:29





                                                                                          @JohnSaunders dare I ask why? (Seriously though why?)

                                                                                          – Luis Perez
                                                                                          Mar 7 '16 at 15:29




                                                                                          2




                                                                                          2





                                                                                          NullReferenceException is meant to be thrown by the CLR. It means that a reference to a null has occurred. It does not mean that a reference to a null would occur except that you cleverly checked first.

                                                                                          – John Saunders
                                                                                          Mar 7 '16 at 15:43





                                                                                          NullReferenceException is meant to be thrown by the CLR. It means that a reference to a null has occurred. It does not mean that a reference to a null would occur except that you cleverly checked first.

                                                                                          – John Saunders
                                                                                          Mar 7 '16 at 15:43













                                                                                          I see your point about how that would be confusing. I've updated it to a regular exception for this example and a custom exception in GitHub.

                                                                                          – Luis Perez
                                                                                          Mar 7 '16 at 18:41







                                                                                          I see your point about how that would be confusing. I've updated it to a regular exception for this example and a custom exception in GitHub.

                                                                                          – Luis Perez
                                                                                          Mar 7 '16 at 18:41















                                                                                          Great answer for such a basic question. It's not so bad when it is your code that is failing. It's horrible when it's coming from deep inside some a commercial third party library you are relying on, and the customer support keeps insisting that it has to be your code that is causing the problem. And your not entirely sure it's not and the whole project is ground to halt.. I actually think this might make an appropriate epitaph for my tombstone: "Object reference not set to an instance of an object."

                                                                                          – Darrel Lee
                                                                                          May 3 '16 at 4:01





                                                                                          Great answer for such a basic question. It's not so bad when it is your code that is failing. It's horrible when it's coming from deep inside some a commercial third party library you are relying on, and the customer support keeps insisting that it has to be your code that is causing the problem. And your not entirely sure it's not and the whole project is ground to halt.. I actually think this might make an appropriate epitaph for my tombstone: "Object reference not set to an instance of an object."

                                                                                          – Darrel Lee
                                                                                          May 3 '16 at 4:01











                                                                                          9














                                                                                          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



                                                                                            var name = p?.Spouse?.FirstName;


                                                                                          is equivalent to:



                                                                                              if (p != null)
                                                                                          {
                                                                                          if (p.Spouse != null)
                                                                                          {
                                                                                          name = p.Spouse.FirstName;
                                                                                          }
                                                                                          }


                                                                                          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





                                                                                          share






























                                                                                            9














                                                                                            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



                                                                                              var name = p?.Spouse?.FirstName;


                                                                                            is equivalent to:



                                                                                                if (p != null)
                                                                                            {
                                                                                            if (p.Spouse != null)
                                                                                            {
                                                                                            name = p.Spouse.FirstName;
                                                                                            }
                                                                                            }


                                                                                            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





                                                                                            share




























                                                                                              9












                                                                                              9








                                                                                              9







                                                                                              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



                                                                                                var name = p?.Spouse?.FirstName;


                                                                                              is equivalent to:



                                                                                                  if (p != null)
                                                                                              {
                                                                                              if (p.Spouse != null)
                                                                                              {
                                                                                              name = p.Spouse.FirstName;
                                                                                              }
                                                                                              }


                                                                                              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





                                                                                              share















                                                                                              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



                                                                                                var name = p?.Spouse?.FirstName;


                                                                                              is equivalent to:



                                                                                                  if (p != null)
                                                                                              {
                                                                                              if (p.Spouse != null)
                                                                                              {
                                                                                              name = p.Spouse.FirstName;
                                                                                              }
                                                                                              }


                                                                                              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






                                                                                              share













                                                                                              share


                                                                                              share








                                                                                              edited Nov 29 '17 at 23:19


























                                                                                              community wiki





                                                                                              2 revs
                                                                                              M.Hassan
























                                                                                                  8














                                                                                                  The error line "Object reference not set to an instance of an object.
                                                                                                  " states that you have not assigned instance object to a object reference and still you are accessing properies/methods of that object.



                                                                                                  for example: let say you have a class called myClass and it contains one property prop1.



                                                                                                  public Class myClass
                                                                                                  {
                                                                                                  public int prop1 {get;set;}
                                                                                                  }


                                                                                                  Now you are accessing this prop1 in some other class just like below:



                                                                                                  public class Demo
                                                                                                  {
                                                                                                  public void testMethod()
                                                                                                  {
                                                                                                  myClass ref = null;
                                                                                                  ref.prop1 = 1; //This line throws error
                                                                                                  }
                                                                                                  }


                                                                                                  above line throws error because reference of class myClass is declared but not instantiated or an instance of object is not assigned to referecne of that class.



                                                                                                  To fix this you have to instantiate (assign object to reference of that class).



                                                                                                  public class Demo
                                                                                                  {
                                                                                                  public void testMethod()
                                                                                                  {
                                                                                                  myClass ref = null;
                                                                                                  ref = new myClass();
                                                                                                  ref.prop1 = 1;
                                                                                                  }
                                                                                                  }




                                                                                                  share





















                                                                                                  • 2





                                                                                                    This answer has already been answered.

                                                                                                    – John Saunders
                                                                                                    Mar 8 '17 at 11:31
















                                                                                                  8














                                                                                                  The error line "Object reference not set to an instance of an object.
                                                                                                  " states that you have not assigned instance object to a object reference and still you are accessing properies/methods of that object.



                                                                                                  for example: let say you have a class called myClass and it contains one property prop1.



                                                                                                  public Class myClass
                                                                                                  {
                                                                                                  public int prop1 {get;set;}
                                                                                                  }


                                                                                                  Now you are accessing this prop1 in some other class just like below:



                                                                                                  public class Demo
                                                                                                  {
                                                                                                  public void testMethod()
                                                                                                  {
                                                                                                  myClass ref = null;
                                                                                                  ref.prop1 = 1; //This line throws error
                                                                                                  }
                                                                                                  }


                                                                                                  above line throws error because reference of class myClass is declared but not instantiated or an instance of object is not assigned to referecne of that class.



                                                                                                  To fix this you have to instantiate (assign object to reference of that class).



                                                                                                  public class Demo
                                                                                                  {
                                                                                                  public void testMethod()
                                                                                                  {
                                                                                                  myClass ref = null;
                                                                                                  ref = new myClass();
                                                                                                  ref.prop1 = 1;
                                                                                                  }
                                                                                                  }




                                                                                                  share





















                                                                                                  • 2





                                                                                                    This answer has already been answered.

                                                                                                    – John Saunders
                                                                                                    Mar 8 '17 at 11:31














                                                                                                  8












                                                                                                  8








                                                                                                  8







                                                                                                  The error line "Object reference not set to an instance of an object.
                                                                                                  " states that you have not assigned instance object to a object reference and still you are accessing properies/methods of that object.



                                                                                                  for example: let say you have a class called myClass and it contains one property prop1.



                                                                                                  public Class myClass
                                                                                                  {
                                                                                                  public int prop1 {get;set;}
                                                                                                  }


                                                                                                  Now you are accessing this prop1 in some other class just like below:



                                                                                                  public class Demo
                                                                                                  {
                                                                                                  public void testMethod()
                                                                                                  {
                                                                                                  myClass ref = null;
                                                                                                  ref.prop1 = 1; //This line throws error
                                                                                                  }
                                                                                                  }


                                                                                                  above line throws error because reference of class myClass is declared but not instantiated or an instance of object is not assigned to referecne of that class.



                                                                                                  To fix this you have to instantiate (assign object to reference of that class).



                                                                                                  public class Demo
                                                                                                  {
                                                                                                  public void testMethod()
                                                                                                  {
                                                                                                  myClass ref = null;
                                                                                                  ref = new myClass();
                                                                                                  ref.prop1 = 1;
                                                                                                  }
                                                                                                  }




                                                                                                  share















                                                                                                  The error line "Object reference not set to an instance of an object.
                                                                                                  " states that you have not assigned instance object to a object reference and still you are accessing properies/methods of that object.



                                                                                                  for example: let say you have a class called myClass and it contains one property prop1.



                                                                                                  public Class myClass
                                                                                                  {
                                                                                                  public int prop1 {get;set;}
                                                                                                  }


                                                                                                  Now you are accessing this prop1 in some other class just like below:



                                                                                                  public class Demo
                                                                                                  {
                                                                                                  public void testMethod()
                                                                                                  {
                                                                                                  myClass ref = null;
                                                                                                  ref.prop1 = 1; //This line throws error
                                                                                                  }
                                                                                                  }


                                                                                                  above line throws error because reference of class myClass is declared but not instantiated or an instance of object is not assigned to referecne of that class.



                                                                                                  To fix this you have to instantiate (assign object to reference of that class).



                                                                                                  public class Demo
                                                                                                  {
                                                                                                  public void testMethod()
                                                                                                  {
                                                                                                  myClass ref = null;
                                                                                                  ref = new myClass();
                                                                                                  ref.prop1 = 1;
                                                                                                  }
                                                                                                  }





                                                                                                  share













                                                                                                  share


                                                                                                  share








                                                                                                  answered Mar 8 '17 at 10:58


























                                                                                                  community wiki





                                                                                                  Jaimin Dave









                                                                                                  • 2





                                                                                                    This answer has already been answered.

                                                                                                    – John Saunders
                                                                                                    Mar 8 '17 at 11:31














                                                                                                  • 2





                                                                                                    This answer has already been answered.

                                                                                                    – John Saunders
                                                                                                    Mar 8 '17 at 11:31








                                                                                                  2




                                                                                                  2





                                                                                                  This answer has already been answered.

                                                                                                  – John Saunders
                                                                                                  Mar 8 '17 at 11:31





                                                                                                  This answer has already been answered.

                                                                                                  – John Saunders
                                                                                                  Mar 8 '17 at 11:31











                                                                                                  8














                                                                                                  Interestingly, none of the answers on this page mention the two edge cases, hope no one minds if I add them:



                                                                                                  Edge case #1: concurrent access to a Dictionary



                                                                                                  Generic dictionaries in .NET are not thread-safe and they sometimes might throw a NullReference or even (more frequent) a KeyNotFoundException when you try to access a key from two concurrent threads. The exception is quite misleading in this case.



                                                                                                  Edge case #2: unsafe code



                                                                                                  If a NullReferenceException is thrown by unsafe code, you might look at your pointer variables, and check them for IntPtr.Zero or something. Which is the same thing ("null pointer exception"), but in unsafe code, variables are often cast to value-types/arrays, etc., and you bang your head against the wall, wondering how a value-type can throw this exception.



                                                                                                  (Another reason for non-using unsafe code unless you need it, by the way)





                                                                                                  share





















                                                                                                  • 5





                                                                                                    Your dictionary example is not an edge case. If the object is not thread safe, then using it from multiple threads produces random results. Your unsafe code example differs from null in which way?

                                                                                                    – John Saunders
                                                                                                    Mar 24 '17 at 2:03
















                                                                                                  8














                                                                                                  Interestingly, none of the answers on this page mention the two edge cases, hope no one minds if I add them:



                                                                                                  Edge case #1: concurrent access to a Dictionary



                                                                                                  Generic dictionaries in .NET are not thread-safe and they sometimes might throw a NullReference or even (more frequent) a KeyNotFoundException when you try to access a key from two concurrent threads. The exception is quite misleading in this case.



                                                                                                  Edge case #2: unsafe code



                                                                                                  If a NullReferenceException is thrown by unsafe code, you might look at your pointer variables, and check them for IntPtr.Zero or something. Which is the same thing ("null pointer exception"), but in unsafe code, variables are often cast to value-types/arrays, etc., and you bang your head against the wall, wondering how a value-type can throw this exception.



                                                                                                  (Another reason for non-using unsafe code unless you need it, by the way)





                                                                                                  share





















                                                                                                  • 5





                                                                                                    Your dictionary example is not an edge case. If the object is not thread safe, then using it from multiple threads produces random results. Your unsafe code example differs from null in which way?

                                                                                                    – John Saunders
                                                                                                    Mar 24 '17 at 2:03














                                                                                                  8












                                                                                                  8








                                                                                                  8







                                                                                                  Interestingly, none of the answers on this page mention the two edge cases, hope no one minds if I add them:



                                                                                                  Edge case #1: concurrent access to a Dictionary



                                                                                                  Generic dictionaries in .NET are not thread-safe and they sometimes might throw a NullReference or even (more frequent) a KeyNotFoundException when you try to access a key from two concurrent threads. The exception is quite misleading in this case.



                                                                                                  Edge case #2: unsafe code



                                                                                                  If a NullReferenceException is thrown by unsafe code, you might look at your pointer variables, and check them for IntPtr.Zero or something. Which is the same thing ("null pointer exception"), but in unsafe code, variables are often cast to value-types/arrays, etc., and you bang your head against the wall, wondering how a value-type can throw this exception.



                                                                                                  (Another reason for non-using unsafe code unless you need it, by the way)





                                                                                                  share















                                                                                                  Interestingly, none of the answers on this page mention the two edge cases, hope no one minds if I add them:



                                                                                                  Edge case #1: concurrent access to a Dictionary



                                                                                                  Generic dictionaries in .NET are not thread-safe and they sometimes might throw a NullReference or even (more frequent) a KeyNotFoundException when you try to access a key from two concurrent threads. The exception is quite misleading in this case.



                                                                                                  Edge case #2: unsafe code



                                                                                                  If a NullReferenceException is thrown by unsafe code, you might look at your pointer variables, and check them for IntPtr.Zero or something. Which is the same thing ("null pointer exception"), but in unsafe code, variables are often cast to value-types/arrays, etc., and you bang your head against the wall, wondering how a value-type can throw this exception.



                                                                                                  (Another reason for non-using unsafe code unless you need it, by the way)






                                                                                                  share













                                                                                                  share


                                                                                                  share








                                                                                                  edited Apr 19 '17 at 0:08


























                                                                                                  community wiki





                                                                                                  2 revs, 2 users 88%
                                                                                                  jazzcat









                                                                                                  • 5





                                                                                                    Your dictionary example is not an edge case. If the object is not thread safe, then using it from multiple threads produces random results. Your unsafe code example differs from null in which way?

                                                                                                    – John Saunders
                                                                                                    Mar 24 '17 at 2:03














                                                                                                  • 5





                                                                                                    Your dictionary example is not an edge case. If the object is not thread safe, then using it from multiple threads produces random results. Your unsafe code example differs from null in which way?

                                                                                                    – John Saunders
                                                                                                    Mar 24 '17 at 2:03








                                                                                                  5




                                                                                                  5





                                                                                                  Your dictionary example is not an edge case. If the object is not thread safe, then using it from multiple threads produces random results. Your unsafe code example differs from null in which way?

                                                                                                  – John Saunders
                                                                                                  Mar 24 '17 at 2:03





                                                                                                  Your dictionary example is not an edge case. If the object is not thread safe, then using it from multiple threads produces random results. Your unsafe code example differs from null in which way?

                                                                                                  – John Saunders
                                                                                                  Mar 24 '17 at 2:03











                                                                                                  3














                                                                                                  NullReferenceException or Object reference not set to an instance of an object occurs when an object of the class you are trying to use is not instantiated.
                                                                                                  For example:



                                                                                                  Assume that you have a class named Student.



                                                                                                  public class Student
                                                                                                  {
                                                                                                  private string FirstName;
                                                                                                  private string LastName;
                                                                                                  public string GetFullName()
                                                                                                  {
                                                                                                  return FirstName + LastName;
                                                                                                  }
                                                                                                  }


                                                                                                  Now, consider another class where you are trying to retrieve the student's full name.



                                                                                                  public class StudentInfo
                                                                                                  {
                                                                                                  public string GetStudentName()
                                                                                                  {
                                                                                                  Student s;
                                                                                                  string fullname = s.GetFullName();
                                                                                                  return fullname;
                                                                                                  }
                                                                                                  }


                                                                                                  As seen in the above code, the statement
                                                                                                  Student s - only declares the variable of type Student, note that the Student class is not instantiated at this point.
                                                                                                  Hence, when the statement s.GetFullName() gets executed, it will throw the NullReferenceException.





                                                                                                  share






























                                                                                                    3














                                                                                                    NullReferenceException or Object reference not set to an instance of an object occurs when an object of the class you are trying to use is not instantiated.
                                                                                                    For example:



                                                                                                    Assume that you have a class named Student.



                                                                                                    public class Student
                                                                                                    {
                                                                                                    private string FirstName;
                                                                                                    private string LastName;
                                                                                                    public string GetFullName()
                                                                                                    {
                                                                                                    return FirstName + LastName;
                                                                                                    }
                                                                                                    }


                                                                                                    Now, consider another class where you are trying to retrieve the student's full name.



                                                                                                    public class StudentInfo
                                                                                                    {
                                                                                                    public string GetStudentName()
                                                                                                    {
                                                                                                    Student s;
                                                                                                    string fullname = s.GetFullName();
                                                                                                    return fullname;
                                                                                                    }
                                                                                                    }


                                                                                                    As seen in the above code, the statement
                                                                                                    Student s - only declares the variable of type Student, note that the Student class is not instantiated at this point.
                                                                                                    Hence, when the statement s.GetFullName() gets executed, it will throw the NullReferenceException.





                                                                                                    share




























                                                                                                      3












                                                                                                      3








                                                                                                      3







                                                                                                      NullReferenceException or Object reference not set to an instance of an object occurs when an object of the class you are trying to use is not instantiated.
                                                                                                      For example:



                                                                                                      Assume that you have a class named Student.



                                                                                                      public class Student
                                                                                                      {
                                                                                                      private string FirstName;
                                                                                                      private string LastName;
                                                                                                      public string GetFullName()
                                                                                                      {
                                                                                                      return FirstName + LastName;
                                                                                                      }
                                                                                                      }


                                                                                                      Now, consider another class where you are trying to retrieve the student's full name.



                                                                                                      public class StudentInfo
                                                                                                      {
                                                                                                      public string GetStudentName()
                                                                                                      {
                                                                                                      Student s;
                                                                                                      string fullname = s.GetFullName();
                                                                                                      return fullname;
                                                                                                      }
                                                                                                      }


                                                                                                      As seen in the above code, the statement
                                                                                                      Student s - only declares the variable of type Student, note that the Student class is not instantiated at this point.
                                                                                                      Hence, when the statement s.GetFullName() gets executed, it will throw the NullReferenceException.





                                                                                                      share















                                                                                                      NullReferenceException or Object reference not set to an instance of an object occurs when an object of the class you are trying to use is not instantiated.
                                                                                                      For example:



                                                                                                      Assume that you have a class named Student.



                                                                                                      public class Student
                                                                                                      {
                                                                                                      private string FirstName;
                                                                                                      private string LastName;
                                                                                                      public string GetFullName()
                                                                                                      {
                                                                                                      return FirstName + LastName;
                                                                                                      }
                                                                                                      }


                                                                                                      Now, consider another class where you are trying to retrieve the student's full name.



                                                                                                      public class StudentInfo
                                                                                                      {
                                                                                                      public string GetStudentName()
                                                                                                      {
                                                                                                      Student s;
                                                                                                      string fullname = s.GetFullName();
                                                                                                      return fullname;
                                                                                                      }
                                                                                                      }


                                                                                                      As seen in the above code, the statement
                                                                                                      Student s - only declares the variable of type Student, note that the Student class is not instantiated at this point.
                                                                                                      Hence, when the statement s.GetFullName() gets executed, it will throw the NullReferenceException.






                                                                                                      share













                                                                                                      share


                                                                                                      share








                                                                                                      answered Jul 28 '16 at 10:52


























                                                                                                      community wiki





                                                                                                      Nick
























                                                                                                          2














                                                                                                          Well, in simple terms:



                                                                                                          You are trying to access an object that isn't created or currently not in memory.



                                                                                                          So how to tackle this:




                                                                                                          1. Debug and let the debugger break... It will directly take you to the variable that is broken... Now your task is to simply fix this.. Using the new keyword in the appropriate place.



                                                                                                          2. If it is caused on some database commands because the object isn't present then all you need to do is do a null check and handle it:



                                                                                                            if (i == null) {
                                                                                                            // Handle this
                                                                                                            }


                                                                                                          3. The hardest one .. if the GC collected the object already... This generally occurs if you are trying to find an object using strings... That is, finding it by name of the object then it may happen that the GC might already cleaned it up... This is hard to find and will become quite a problem... A better way to tackle this is do null checks wherever necessary during the development process. This will save you a lot of time.



                                                                                                          By finding by name I mean some framework allow you to FIndObjects using strings and the code might look like this: FindObject("ObjectName");





                                                                                                          share





















                                                                                                          • 3





                                                                                                            If you have a reference to an object, then the GC never cleans it up

                                                                                                            – John Saunders
                                                                                                            Dec 24 '15 at 7:51






                                                                                                          • 2





                                                                                                            if you use things like FindObject("Name of Object") there is no way GC will know before hand that you are going to refernece that object .. this is what is was trying to explaing .. these occur at runtime

                                                                                                            – Akash Gutha
                                                                                                            Dec 24 '15 at 8:11






                                                                                                          • 2





                                                                                                            There are some frameworks that Provide this functionality in C# such as Unity . the question has nothing related to BCl. Search the Internet before Criticizing there are a ton of functions like them and for ur kind information i even use it daily. Now please tell me how does the answer doesn't make anysense.

                                                                                                            – Akash Gutha
                                                                                                            Dec 24 '15 at 12:35






                                                                                                          • 2





                                                                                                            docs.unity3d.com/ScriptReference/… check the link and correct urself mr.expert :p

                                                                                                            – Akash Gutha
                                                                                                            Dec 24 '15 at 12:54











                                                                                                          • The examples I saw in your link assign the results of GameObject.Find to a member field. That's a reference and the GC will not collect it until the containing object is collected.

                                                                                                            – John Saunders
                                                                                                            May 25 '16 at 18:00
















                                                                                                          2














                                                                                                          Well, in simple terms:



                                                                                                          You are trying to access an object that isn't created or currently not in memory.



                                                                                                          So how to tackle this:




                                                                                                          1. Debug and let the debugger break... It will directly take you to the variable that is broken... Now your task is to simply fix this.. Using the new keyword in the appropriate place.



                                                                                                          2. If it is caused on some database commands because the object isn't present then all you need to do is do a null check and handle it:



                                                                                                            if (i == null) {
                                                                                                            // Handle this
                                                                                                            }


                                                                                                          3. The hardest one .. if the GC collected the object already... This generally occurs if you are trying to find an object using strings... That is, finding it by name of the object then it may happen that the GC might already cleaned it up... This is hard to find and will become quite a problem... A better way to tackle this is do null checks wherever necessary during the development process. This will save you a lot of time.



                                                                                                          By finding by name I mean some framework allow you to FIndObjects using strings and the code might look like this: FindObject("ObjectName");





                                                                                                          share





















                                                                                                          • 3





                                                                                                            If you have a reference to an object, then the GC never cleans it up

                                                                                                            – John Saunders
                                                                                                            Dec 24 '15 at 7:51






                                                                                                          • 2





                                                                                                            if you use things like FindObject("Name of Object") there is no way GC will know before hand that you are going to refernece that object .. this is what is was trying to explaing .. these occur at runtime

                                                                                                            – Akash Gutha
                                                                                                            Dec 24 '15 at 8:11






                                                                                                          • 2





                                                                                                            There are some frameworks that Provide this functionality in C# such as Unity . the question has nothing related to BCl. Search the Internet before Criticizing there are a ton of functions like them and for ur kind information i even use it daily. Now please tell me how does the answer doesn't make anysense.

                                                                                                            – Akash Gutha
                                                                                                            Dec 24 '15 at 12:35






                                                                                                          • 2





                                                                                                            docs.unity3d.com/ScriptReference/… check the link and correct urself mr.expert :p

                                                                                                            – Akash Gutha
                                                                                                            Dec 24 '15 at 12:54











                                                                                                          • The examples I saw in your link assign the results of GameObject.Find to a member field. That's a reference and the GC will not collect it until the containing object is collected.

                                                                                                            – John Saunders
                                                                                                            May 25 '16 at 18:00














                                                                                                          2












                                                                                                          2








                                                                                                          2







                                                                                                          Well, in simple terms:



                                                                                                          You are trying to access an object that isn't created or currently not in memory.



                                                                                                          So how to tackle this:




                                                                                                          1. Debug and let the debugger break... It will directly take you to the variable that is broken... Now your task is to simply fix this.. Using the new keyword in the appropriate place.



                                                                                                          2. If it is caused on some database commands because the object isn't present then all you need to do is do a null check and handle it:



                                                                                                            if (i == null) {
                                                                                                            // Handle this
                                                                                                            }


                                                                                                          3. The hardest one .. if the GC collected the object already... This generally occurs if you are trying to find an object using strings... That is, finding it by name of the object then it may happen that the GC might already cleaned it up... This is hard to find and will become quite a problem... A better way to tackle this is do null checks wherever necessary during the development process. This will save you a lot of time.



                                                                                                          By finding by name I mean some framework allow you to FIndObjects using strings and the code might look like this: FindObject("ObjectName");





                                                                                                          share















                                                                                                          Well, in simple terms:



                                                                                                          You are trying to access an object that isn't created or currently not in memory.



                                                                                                          So how to tackle this:




                                                                                                          1. Debug and let the debugger break... It will directly take you to the variable that is broken... Now your task is to simply fix this.. Using the new keyword in the appropriate place.



                                                                                                          2. If it is caused on some database commands because the object isn't present then all you need to do is do a null check and handle it:



                                                                                                            if (i == null) {
                                                                                                            // Handle this
                                                                                                            }


                                                                                                          3. The hardest one .. if the GC collected the object already... This generally occurs if you are trying to find an object using strings... That is, finding it by name of the object then it may happen that the GC might already cleaned it up... This is hard to find and will become quite a problem... A better way to tackle this is do null checks wherever necessary during the development process. This will save you a lot of time.



                                                                                                          By finding by name I mean some framework allow you to FIndObjects using strings and the code might look like this: FindObject("ObjectName");






                                                                                                          share













                                                                                                          share


                                                                                                          share








                                                                                                          edited May 16 '16 at 8:34


























                                                                                                          community wiki





                                                                                                          3 revs, 2 users 70%
                                                                                                          Akash Chowdary









                                                                                                          • 3





                                                                                                            If you have a reference to an object, then the GC never cleans it up

                                                                                                            – John Saunders
                                                                                                            Dec 24 '15 at 7:51






                                                                                                          • 2





                                                                                                            if you use things like FindObject("Name of Object") there is no way GC will know before hand that you are going to refernece that object .. this is what is was trying to explaing .. these occur at runtime

                                                                                                            – Akash Gutha
                                                                                                            Dec 24 '15 at 8:11






                                                                                                          • 2





                                                                                                            There are some frameworks that Provide this functionality in C# such as Unity . the question has nothing related to BCl. Search the Internet before Criticizing there are a ton of functions like them and for ur kind information i even use it daily. Now please tell me how does the answer doesn't make anysense.

                                                                                                            – Akash Gutha
                                                                                                            Dec 24 '15 at 12:35






                                                                                                          • 2





                                                                                                            docs.unity3d.com/ScriptReference/… check the link and correct urself mr.expert :p

                                                                                                            – Akash Gutha
                                                                                                            Dec 24 '15 at 12:54











                                                                                                          • The examples I saw in your link assign the results of GameObject.Find to a member field. That's a reference and the GC will not collect it until the containing object is collected.

                                                                                                            – John Saunders
                                                                                                            May 25 '16 at 18:00














                                                                                                          • 3





                                                                                                            If you have a reference to an object, then the GC never cleans it up

                                                                                                            – John Saunders
                                                                                                            Dec 24 '15 at 7:51






                                                                                                          • 2





                                                                                                            if you use things like FindObject("Name of Object") there is no way GC will know before hand that you are going to refernece that object .. this is what is was trying to explaing .. these occur at runtime

                                                                                                            – Akash Gutha
                                                                                                            Dec 24 '15 at 8:11






                                                                                                          • 2





                                                                                                            There are some frameworks that Provide this functionality in C# such as Unity . the question has nothing related to BCl. Search the Internet before Criticizing there are a ton of functions like them and for ur kind information i even use it daily. Now please tell me how does the answer doesn't make anysense.

                                                                                                            – Akash Gutha
                                                                                                            Dec 24 '15 at 12:35






                                                                                                          • 2





                                                                                                            docs.unity3d.com/ScriptReference/… check the link and correct urself mr.expert :p

                                                                                                            – Akash Gutha
                                                                                                            Dec 24 '15 at 12:54











                                                                                                          • The examples I saw in your link assign the results of GameObject.Find to a member field. That's a reference and the GC will not collect it until the containing object is collected.

                                                                                                            – John Saunders
                                                                                                            May 25 '16 at 18:00








                                                                                                          3




                                                                                                          3





                                                                                                          If you have a reference to an object, then the GC never cleans it up

                                                                                                          – John Saunders
                                                                                                          Dec 24 '15 at 7:51





                                                                                                          If you have a reference to an object, then the GC never cleans it up

                                                                                                          – John Saunders
                                                                                                          Dec 24 '15 at 7:51




                                                                                                          2




                                                                                                          2





                                                                                                          if you use things like FindObject("Name of Object") there is no way GC will know before hand that you are going to refernece that object .. this is what is was trying to explaing .. these occur at runtime

                                                                                                          – Akash Gutha
                                                                                                          Dec 24 '15 at 8:11





                                                                                                          if you use things like FindObject("Name of Object") there is no way GC will know before hand that you are going to refernece that object .. this is what is was trying to explaing .. these occur at runtime

                                                                                                          – Akash Gutha
                                                                                                          Dec 24 '15 at 8:11




                                                                                                          2




                                                                                                          2





                                                                                                          There are some frameworks that Provide this functionality in C# such as Unity . the question has nothing related to BCl. Search the Internet before Criticizing there are a ton of functions like them and for ur kind information i even use it daily. Now please tell me how does the answer doesn't make anysense.

                                                                                                          – Akash Gutha
                                                                                                          Dec 24 '15 at 12:35





                                                                                                          There are some frameworks that Provide this functionality in C# such as Unity . the question has nothing related to BCl. Search the Internet before Criticizing there are a ton of functions like them and for ur kind information i even use it daily. Now please tell me how does the answer doesn't make anysense.

                                                                                                          – Akash Gutha
                                                                                                          Dec 24 '15 at 12:35




                                                                                                          2




                                                                                                          2





                                                                                                          docs.unity3d.com/ScriptReference/… check the link and correct urself mr.expert :p

                                                                                                          – Akash Gutha
                                                                                                          Dec 24 '15 at 12:54





                                                                                                          docs.unity3d.com/ScriptReference/… check the link and correct urself mr.expert :p

                                                                                                          – Akash Gutha
                                                                                                          Dec 24 '15 at 12:54













                                                                                                          The examples I saw in your link assign the results of GameObject.Find to a member field. That's a reference and the GC will not collect it until the containing object is collected.

                                                                                                          – John Saunders
                                                                                                          May 25 '16 at 18:00





                                                                                                          The examples I saw in your link assign the results of GameObject.Find to a member field. That's a reference and the GC will not collect it until the containing object is collected.

                                                                                                          – John Saunders
                                                                                                          May 25 '16 at 18:00











                                                                                                          0














                                                                                                          If we consider common scenarios where this exception can be thrown, accessing properties withing object at the top.



                                                                                                          Ex:



                                                                                                          string postalcode=Customer.Address.PostalCode; 
                                                                                                          //if customer or address is null , this will through exeption


                                                                                                          in here , if address is null , then you will get NullReferenceException.



                                                                                                          So, as a practice we should always use null check, before accessing properties in such objects (specially in generic)



                                                                                                          string postalcode=Customer?.Address?.PostalCode;
                                                                                                          //if customer or address is null , this will return null, without through a exception




                                                                                                          share





















                                                                                                          • 3





                                                                                                            This has already been answered several times.

                                                                                                            – John Saunders
                                                                                                            Aug 26 '17 at 3:24
















                                                                                                          0














                                                                                                          If we consider common scenarios where this exception can be thrown, accessing properties withing object at the top.



                                                                                                          Ex:



                                                                                                          string postalcode=Customer.Address.PostalCode; 
                                                                                                          //if customer or address is null , this will through exeption


                                                                                                          in here , if address is null , then you will get NullReferenceException.



                                                                                                          So, as a practice we should always use null check, before accessing properties in such objects (specially in generic)



                                                                                                          string postalcode=Customer?.Address?.PostalCode;
                                                                                                          //if customer or address is null , this will return null, without through a exception




                                                                                                          share





















                                                                                                          • 3





                                                                                                            This has already been answered several times.

                                                                                                            – John Saunders
                                                                                                            Aug 26 '17 at 3:24














                                                                                                          0












                                                                                                          0








                                                                                                          0







                                                                                                          If we consider common scenarios where this exception can be thrown, accessing properties withing object at the top.



                                                                                                          Ex:



                                                                                                          string postalcode=Customer.Address.PostalCode; 
                                                                                                          //if customer or address is null , this will through exeption


                                                                                                          in here , if address is null , then you will get NullReferenceException.



                                                                                                          So, as a practice we should always use null check, before accessing properties in such objects (specially in generic)



                                                                                                          string postalcode=Customer?.Address?.PostalCode;
                                                                                                          //if customer or address is null , this will return null, without through a exception




                                                                                                          share















                                                                                                          If we consider common scenarios where this exception can be thrown, accessing properties withing object at the top.



                                                                                                          Ex:



                                                                                                          string postalcode=Customer.Address.PostalCode; 
                                                                                                          //if customer or address is null , this will through exeption


                                                                                                          in here , if address is null , then you will get NullReferenceException.



                                                                                                          So, as a practice we should always use null check, before accessing properties in such objects (specially in generic)



                                                                                                          string postalcode=Customer?.Address?.PostalCode;
                                                                                                          //if customer or address is null , this will return null, without through a exception





                                                                                                          share













                                                                                                          share


                                                                                                          share








                                                                                                          answered Aug 24 '17 at 5:55


























                                                                                                          community wiki





                                                                                                          Hiran









                                                                                                          • 3





                                                                                                            This has already been answered several times.

                                                                                                            – John Saunders
                                                                                                            Aug 26 '17 at 3:24














                                                                                                          • 3





                                                                                                            This has already been answered several times.

                                                                                                            – John Saunders
                                                                                                            Aug 26 '17 at 3:24








                                                                                                          3




                                                                                                          3





                                                                                                          This has already been answered several times.

                                                                                                          – John Saunders
                                                                                                          Aug 26 '17 at 3:24





                                                                                                          This has already been answered several times.

                                                                                                          – John Saunders
                                                                                                          Aug 26 '17 at 3:24











                                                                                                          0














                                                                                                          Literally the easiest way to fix a NullReferenceExeption has two ways.
                                                                                                          If you have a GameObject for example with a script attached and a variable named rb (rigidbody) this variable will start null when you start your game.

                                                                                                          This is why you get a NullReferenceExeption because the computer does not have data stored in that variable.



                                                                                                          I'll be using a RigidBody variable as an example.

                                                                                                          We can add data really easily actually in a few ways:




                                                                                                          1. Add a RigidBody to your object with AddComponent > Physics > Rigidbody

                                                                                                            Then go into your script and type rb = GetComponent<Rigidbody>();

                                                                                                            This line of code works best under your Start() or Awake() functions.

                                                                                                          2. You can add a component programmatically and assign the variable at the same time with one line of code: rb = AddComponent<RigidBody>();


                                                                                                          Further Notes: If you want unity to add a component to your object and you might have forgotten to add one, you can type [RequireComponent(typeof(RigidBody))] above your class declaration (the space below all of your usings).

                                                                                                          Enjoy and have fun making games!





                                                                                                          share






























                                                                                                            0














                                                                                                            Literally the easiest way to fix a NullReferenceExeption has two ways.
                                                                                                            If you have a GameObject for example with a script attached and a variable named rb (rigidbody) this variable will start null when you start your game.

                                                                                                            This is why you get a NullReferenceExeption because the computer does not have data stored in that variable.



                                                                                                            I'll be using a RigidBody variable as an example.

                                                                                                            We can add data really easily actually in a few ways:




                                                                                                            1. Add a RigidBody to your object with AddComponent > Physics > Rigidbody

                                                                                                              Then go into your script and type rb = GetComponent<Rigidbody>();

                                                                                                              This line of code works best under your Start() or Awake() functions.

                                                                                                            2. You can add a component programmatically and assign the variable at the same time with one line of code: rb = AddComponent<RigidBody>();


                                                                                                            Further Notes: If you want unity to add a component to your object and you might have forgotten to add one, you can type [RequireComponent(typeof(RigidBody))] above your class declaration (the space below all of your usings).

                                                                                                            Enjoy and have fun making games!





                                                                                                            share




























                                                                                                              0












                                                                                                              0








                                                                                                              0







                                                                                                              Literally the easiest way to fix a NullReferenceExeption has two ways.
                                                                                                              If you have a GameObject for example with a script attached and a variable named rb (rigidbody) this variable will start null when you start your game.

                                                                                                              This is why you get a NullReferenceExeption because the computer does not have data stored in that variable.



                                                                                                              I'll be using a RigidBody variable as an example.

                                                                                                              We can add data really easily actually in a few ways:




                                                                                                              1. Add a RigidBody to your object with AddComponent > Physics > Rigidbody

                                                                                                                Then go into your script and type rb = GetComponent<Rigidbody>();

                                                                                                                This line of code works best under your Start() or Awake() functions.

                                                                                                              2. You can add a component programmatically and assign the variable at the same time with one line of code: rb = AddComponent<RigidBody>();


                                                                                                              Further Notes: If you want unity to add a component to your object and you might have forgotten to add one, you can type [RequireComponent(typeof(RigidBody))] above your class declaration (the space below all of your usings).

                                                                                                              Enjoy and have fun making games!





                                                                                                              share















                                                                                                              Literally the easiest way to fix a NullReferenceExeption has two ways.
                                                                                                              If you have a GameObject for example with a script attached and a variable named rb (rigidbody) this variable will start null when you start your game.

                                                                                                              This is why you get a NullReferenceExeption because the computer does not have data stored in that variable.



                                                                                                              I'll be using a RigidBody variable as an example.

                                                                                                              We can add data really easily actually in a few ways:




                                                                                                              1. Add a RigidBody to your object with AddComponent > Physics > Rigidbody

                                                                                                                Then go into your script and type rb = GetComponent<Rigidbody>();

                                                                                                                This line of code works best under your Start() or Awake() functions.

                                                                                                              2. You can add a component programmatically and assign the variable at the same time with one line of code: rb = AddComponent<RigidBody>();


                                                                                                              Further Notes: If you want unity to add a component to your object and you might have forgotten to add one, you can type [RequireComponent(typeof(RigidBody))] above your class declaration (the space below all of your usings).

                                                                                                              Enjoy and have fun making games!






                                                                                                              share













                                                                                                              share


                                                                                                              share








                                                                                                              edited Nov 14 '17 at 20:45


























                                                                                                              community wiki





                                                                                                              2 revs, 2 users 96%
                                                                                                              CausticLasagne
























                                                                                                                  -1














                                                                                                                  If one is getting this message during saving or compiling the build, just close all the files and then open any file to compile and save.



                                                                                                                  For me the reason was that I had rename the file and old file was still open.





                                                                                                                  share






























                                                                                                                    -1














                                                                                                                    If one is getting this message during saving or compiling the build, just close all the files and then open any file to compile and save.



                                                                                                                    For me the reason was that I had rename the file and old file was still open.





                                                                                                                    share




























                                                                                                                      -1












                                                                                                                      -1








                                                                                                                      -1







                                                                                                                      If one is getting this message during saving or compiling the build, just close all the files and then open any file to compile and save.



                                                                                                                      For me the reason was that I had rename the file and old file was still open.





                                                                                                                      share















                                                                                                                      If one is getting this message during saving or compiling the build, just close all the files and then open any file to compile and save.



                                                                                                                      For me the reason was that I had rename the file and old file was still open.






                                                                                                                      share













                                                                                                                      share


                                                                                                                      share








                                                                                                                      answered Oct 14 '17 at 18:48


























                                                                                                                      community wiki





                                                                                                                      Harshal Doshi Jain
























                                                                                                                          -1














                                                                                                                          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.





                                                                                                                          share


























                                                                                                                          • new never returns null

                                                                                                                            – John Saunders
                                                                                                                            Oct 27 '17 at 13:00











                                                                                                                          • @John did you mean to accept this answer?

                                                                                                                            – CodeCaster
                                                                                                                            Oct 27 '17 at 14:48











                                                                                                                          • @code no. Thanks for the catch. Phone app.

                                                                                                                            – John Saunders
                                                                                                                            Oct 27 '17 at 14:49
















                                                                                                                          -1














                                                                                                                          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.





                                                                                                                          share


























                                                                                                                          • new never returns null

                                                                                                                            – John Saunders
                                                                                                                            Oct 27 '17 at 13:00











                                                                                                                          • @John did you mean to accept this answer?

                                                                                                                            – CodeCaster
                                                                                                                            Oct 27 '17 at 14:48











                                                                                                                          • @code no. Thanks for the catch. Phone app.

                                                                                                                            – John Saunders
                                                                                                                            Oct 27 '17 at 14:49














                                                                                                                          -1












                                                                                                                          -1








                                                                                                                          -1







                                                                                                                          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.





                                                                                                                          share















                                                                                                                          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.






                                                                                                                          share













                                                                                                                          share


                                                                                                                          share








                                                                                                                          edited Oct 27 '17 at 13:56


























                                                                                                                          community wiki





                                                                                                                          2 revs
                                                                                                                          MilanG














                                                                                                                          • new never returns null

                                                                                                                            – John Saunders
                                                                                                                            Oct 27 '17 at 13:00











                                                                                                                          • @John did you mean to accept this answer?

                                                                                                                            – CodeCaster
                                                                                                                            Oct 27 '17 at 14:48











                                                                                                                          • @code no. Thanks for the catch. Phone app.

                                                                                                                            – John Saunders
                                                                                                                            Oct 27 '17 at 14:49



















                                                                                                                          • new never returns null

                                                                                                                            – John Saunders
                                                                                                                            Oct 27 '17 at 13:00











                                                                                                                          • @John did you mean to accept this answer?

                                                                                                                            – CodeCaster
                                                                                                                            Oct 27 '17 at 14:48











                                                                                                                          • @code no. Thanks for the catch. Phone app.

                                                                                                                            – John Saunders
                                                                                                                            Oct 27 '17 at 14:49

















                                                                                                                          new never returns null

                                                                                                                          – John Saunders
                                                                                                                          Oct 27 '17 at 13:00





                                                                                                                          new never returns null

                                                                                                                          – John Saunders
                                                                                                                          Oct 27 '17 at 13:00













                                                                                                                          @John did you mean to accept this answer?

                                                                                                                          – CodeCaster
                                                                                                                          Oct 27 '17 at 14:48





                                                                                                                          @John did you mean to accept this answer?

                                                                                                                          – CodeCaster
                                                                                                                          Oct 27 '17 at 14:48













                                                                                                                          @code no. Thanks for the catch. Phone app.

                                                                                                                          – John Saunders
                                                                                                                          Oct 27 '17 at 14:49





                                                                                                                          @code no. Thanks for the catch. Phone app.

                                                                                                                          – John Saunders
                                                                                                                          Oct 27 '17 at 14:49











                                                                                                                          -2














                                                                                                                          This is basically is a Null reference exception. As Microsoft states-




                                                                                                                          A NullReferenceException exception is thrown when you try to access a
                                                                                                                          member of a type whose value is null.




                                                                                                                          What does that mean?



                                                                                                                          That means if any member which doesn’t hold any value and we are making that member to perform certain task then the system will undoubtedly toss a message and say-



                                                                                                                          “Hey wait, that member has no values so it can’t perform the task which you are handing it over.”



                                                                                                                          The exception itself says that something is being referred but whose value is not being set. So this denotes that it only occurs while using reference types as Value types are non-nullable.



                                                                                                                          NullReferenceException won't occur if we are using Value type members.



                                                                                                                          class Program
                                                                                                                          {
                                                                                                                          static void Main(string args)
                                                                                                                          {
                                                                                                                          string str = null;
                                                                                                                          Console.WriteLine(str.Length);
                                                                                                                          Console.ReadLine();
                                                                                                                          }
                                                                                                                          }


                                                                                                                          The above code shows simple string which is assigned with a null value.



                                                                                                                          Now, when I try to print the length of the string str, I do get An unhandled exception of type ‘System.NullReferenceException’ occurred message because member str is pointing to null and there can’t be any length of null.



                                                                                                                          NullReferenceException’ also occurs when we forget to instantiate a reference type.



                                                                                                                          Suppose I have a class and member method in it. I have not instantiated my class but only named my class. Now if I try to use the method, the compiler will throw an error or issue a warning (depending on the compiler).



                                                                                                                          class Program
                                                                                                                          {
                                                                                                                          static void Main(string args)
                                                                                                                          {
                                                                                                                          MyClass1 obj;
                                                                                                                          obj.foo(); //Use of unassigned local variable 'obj'
                                                                                                                          }
                                                                                                                          }

                                                                                                                          public class MyClass1
                                                                                                                          {
                                                                                                                          internal void foo()
                                                                                                                          {
                                                                                                                          Console.WriteLine("hello from foo");

                                                                                                                          }
                                                                                                                          }


                                                                                                                          Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing. Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing.



                                                                                                                          Why it occurs?




                                                                                                                          • NullReferenceException arises due to our fault for not checking the object’s value. We often leave the object values unchecked in the code development.


                                                                                                                          • It also arises when we forget to instantiate our objects. Using methods, properties, collections etc. which can return or set null values can also be the cause of this exception.



                                                                                                                          How can it be avoided?



                                                                                                                          There are various ways and methods to avoid this renowned exception:




                                                                                                                          1. Explicit Checking: We should adhere to the tradition of checking the objects, properties, methods, arrays, and collections whether they are null. This can be simply implemented using conditional statements like if-else if-else etc.


                                                                                                                          2. Exception handling: One of the important ways of managing this exception. Using simple try-catch-finally blocks we can control this exception and also maintain a log of it. This can be very useful when your application is on production stage.


                                                                                                                          3. Null operators: Null Coalescing operator and null conditional operators can also be used in handy while setting values to objects, variables, properties and fields.


                                                                                                                          4. Debugger: For developers, we have the big weapon of Debugging with us. If have we face NullReferenceException during the development face we can use the debugger to get to the source of the exception.


                                                                                                                          5. In-built method: System methods such as GetValueOrDefault(),IsNullOrWhiteSpace() and IsNullorEmpty() checks for nulls and assign the default value if there is a null value.



                                                                                                                          There are many good answers already here. You can also check more detailed description with examples on my blog.



                                                                                                                          Hope this helps too!





                                                                                                                          share


























                                                                                                                          • You basically copied half of that blog post and added nothing new that existing answers don't address.

                                                                                                                            – CodeCaster
                                                                                                                            Jul 18 '17 at 14:41











                                                                                                                          • @codecaster Is it said copying when you rewrite a summary from your own blog. I know there is nothing new in my answer and nothing new that previous answers dont have but I wish to contribute in more sophisticated way and let others understand the way I understood. Will be glad even if it helps a single person. In good faith.

                                                                                                                            – Wasim
                                                                                                                            Jul 18 '17 at 18:01
















                                                                                                                          -2














                                                                                                                          This is basically is a Null reference exception. As Microsoft states-




                                                                                                                          A NullReferenceException exception is thrown when you try to access a
                                                                                                                          member of a type whose value is null.




                                                                                                                          What does that mean?



                                                                                                                          That means if any member which doesn’t hold any value and we are making that member to perform certain task then the system will undoubtedly toss a message and say-



                                                                                                                          “Hey wait, that member has no values so it can’t perform the task which you are handing it over.”



                                                                                                                          The exception itself says that something is being referred but whose value is not being set. So this denotes that it only occurs while using reference types as Value types are non-nullable.



                                                                                                                          NullReferenceException won't occur if we are using Value type members.



                                                                                                                          class Program
                                                                                                                          {
                                                                                                                          static void Main(string args)
                                                                                                                          {
                                                                                                                          string str = null;
                                                                                                                          Console.WriteLine(str.Length);
                                                                                                                          Console.ReadLine();
                                                                                                                          }
                                                                                                                          }


                                                                                                                          The above code shows simple string which is assigned with a null value.



                                                                                                                          Now, when I try to print the length of the string str, I do get An unhandled exception of type ‘System.NullReferenceException’ occurred message because member str is pointing to null and there can’t be any length of null.



                                                                                                                          NullReferenceException’ also occurs when we forget to instantiate a reference type.



                                                                                                                          Suppose I have a class and member method in it. I have not instantiated my class but only named my class. Now if I try to use the method, the compiler will throw an error or issue a warning (depending on the compiler).



                                                                                                                          class Program
                                                                                                                          {
                                                                                                                          static void Main(string args)
                                                                                                                          {
                                                                                                                          MyClass1 obj;
                                                                                                                          obj.foo(); //Use of unassigned local variable 'obj'
                                                                                                                          }
                                                                                                                          }

                                                                                                                          public class MyClass1
                                                                                                                          {
                                                                                                                          internal void foo()
                                                                                                                          {
                                                                                                                          Console.WriteLine("hello from foo");

                                                                                                                          }
                                                                                                                          }


                                                                                                                          Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing. Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing.



                                                                                                                          Why it occurs?




                                                                                                                          • NullReferenceException arises due to our fault for not checking the object’s value. We often leave the object values unchecked in the code development.


                                                                                                                          • It also arises when we forget to instantiate our objects. Using methods, properties, collections etc. which can return or set null values can also be the cause of this exception.



                                                                                                                          How can it be avoided?



                                                                                                                          There are various ways and methods to avoid this renowned exception:




                                                                                                                          1. Explicit Checking: We should adhere to the tradition of checking the objects, properties, methods, arrays, and collections whether they are null. This can be simply implemented using conditional statements like if-else if-else etc.


                                                                                                                          2. Exception handling: One of the important ways of managing this exception. Using simple try-catch-finally blocks we can control this exception and also maintain a log of it. This can be very useful when your application is on production stage.


                                                                                                                          3. Null operators: Null Coalescing operator and null conditional operators can also be used in handy while setting values to objects, variables, properties and fields.


                                                                                                                          4. Debugger: For developers, we have the big weapon of Debugging with us. If have we face NullReferenceException during the development face we can use the debugger to get to the source of the exception.


                                                                                                                          5. In-built method: System methods such as GetValueOrDefault(),IsNullOrWhiteSpace() and IsNullorEmpty() checks for nulls and assign the default value if there is a null value.



                                                                                                                          There are many good answers already here. You can also check more detailed description with examples on my blog.



                                                                                                                          Hope this helps too!





                                                                                                                          share


























                                                                                                                          • You basically copied half of that blog post and added nothing new that existing answers don't address.

                                                                                                                            – CodeCaster
                                                                                                                            Jul 18 '17 at 14:41











                                                                                                                          • @codecaster Is it said copying when you rewrite a summary from your own blog. I know there is nothing new in my answer and nothing new that previous answers dont have but I wish to contribute in more sophisticated way and let others understand the way I understood. Will be glad even if it helps a single person. In good faith.

                                                                                                                            – Wasim
                                                                                                                            Jul 18 '17 at 18:01














                                                                                                                          -2












                                                                                                                          -2








                                                                                                                          -2







                                                                                                                          This is basically is a Null reference exception. As Microsoft states-




                                                                                                                          A NullReferenceException exception is thrown when you try to access a
                                                                                                                          member of a type whose value is null.




                                                                                                                          What does that mean?



                                                                                                                          That means if any member which doesn’t hold any value and we are making that member to perform certain task then the system will undoubtedly toss a message and say-



                                                                                                                          “Hey wait, that member has no values so it can’t perform the task which you are handing it over.”



                                                                                                                          The exception itself says that something is being referred but whose value is not being set. So this denotes that it only occurs while using reference types as Value types are non-nullable.



                                                                                                                          NullReferenceException won't occur if we are using Value type members.



                                                                                                                          class Program
                                                                                                                          {
                                                                                                                          static void Main(string args)
                                                                                                                          {
                                                                                                                          string str = null;
                                                                                                                          Console.WriteLine(str.Length);
                                                                                                                          Console.ReadLine();
                                                                                                                          }
                                                                                                                          }


                                                                                                                          The above code shows simple string which is assigned with a null value.



                                                                                                                          Now, when I try to print the length of the string str, I do get An unhandled exception of type ‘System.NullReferenceException’ occurred message because member str is pointing to null and there can’t be any length of null.



                                                                                                                          NullReferenceException’ also occurs when we forget to instantiate a reference type.



                                                                                                                          Suppose I have a class and member method in it. I have not instantiated my class but only named my class. Now if I try to use the method, the compiler will throw an error or issue a warning (depending on the compiler).



                                                                                                                          class Program
                                                                                                                          {
                                                                                                                          static void Main(string args)
                                                                                                                          {
                                                                                                                          MyClass1 obj;
                                                                                                                          obj.foo(); //Use of unassigned local variable 'obj'
                                                                                                                          }
                                                                                                                          }

                                                                                                                          public class MyClass1
                                                                                                                          {
                                                                                                                          internal void foo()
                                                                                                                          {
                                                                                                                          Console.WriteLine("hello from foo");

                                                                                                                          }
                                                                                                                          }


                                                                                                                          Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing. Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing.



                                                                                                                          Why it occurs?




                                                                                                                          • NullReferenceException arises due to our fault for not checking the object’s value. We often leave the object values unchecked in the code development.


                                                                                                                          • It also arises when we forget to instantiate our objects. Using methods, properties, collections etc. which can return or set null values can also be the cause of this exception.



                                                                                                                          How can it be avoided?



                                                                                                                          There are various ways and methods to avoid this renowned exception:




                                                                                                                          1. Explicit Checking: We should adhere to the tradition of checking the objects, properties, methods, arrays, and collections whether they are null. This can be simply implemented using conditional statements like if-else if-else etc.


                                                                                                                          2. Exception handling: One of the important ways of managing this exception. Using simple try-catch-finally blocks we can control this exception and also maintain a log of it. This can be very useful when your application is on production stage.


                                                                                                                          3. Null operators: Null Coalescing operator and null conditional operators can also be used in handy while setting values to objects, variables, properties and fields.


                                                                                                                          4. Debugger: For developers, we have the big weapon of Debugging with us. If have we face NullReferenceException during the development face we can use the debugger to get to the source of the exception.


                                                                                                                          5. In-built method: System methods such as GetValueOrDefault(),IsNullOrWhiteSpace() and IsNullorEmpty() checks for nulls and assign the default value if there is a null value.



                                                                                                                          There are many good answers already here. You can also check more detailed description with examples on my blog.



                                                                                                                          Hope this helps too!





                                                                                                                          share















                                                                                                                          This is basically is a Null reference exception. As Microsoft states-




                                                                                                                          A NullReferenceException exception is thrown when you try to access a
                                                                                                                          member of a type whose value is null.




                                                                                                                          What does that mean?



                                                                                                                          That means if any member which doesn’t hold any value and we are making that member to perform certain task then the system will undoubtedly toss a message and say-



                                                                                                                          “Hey wait, that member has no values so it can’t perform the task which you are handing it over.”



                                                                                                                          The exception itself says that something is being referred but whose value is not being set. So this denotes that it only occurs while using reference types as Value types are non-nullable.



                                                                                                                          NullReferenceException won't occur if we are using Value type members.



                                                                                                                          class Program
                                                                                                                          {
                                                                                                                          static void Main(string args)
                                                                                                                          {
                                                                                                                          string str = null;
                                                                                                                          Console.WriteLine(str.Length);
                                                                                                                          Console.ReadLine();
                                                                                                                          }
                                                                                                                          }


                                                                                                                          The above code shows simple string which is assigned with a null value.



                                                                                                                          Now, when I try to print the length of the string str, I do get An unhandled exception of type ‘System.NullReferenceException’ occurred message because member str is pointing to null and there can’t be any length of null.



                                                                                                                          NullReferenceException’ also occurs when we forget to instantiate a reference type.



                                                                                                                          Suppose I have a class and member method in it. I have not instantiated my class but only named my class. Now if I try to use the method, the compiler will throw an error or issue a warning (depending on the compiler).



                                                                                                                          class Program
                                                                                                                          {
                                                                                                                          static void Main(string args)
                                                                                                                          {
                                                                                                                          MyClass1 obj;
                                                                                                                          obj.foo(); //Use of unassigned local variable 'obj'
                                                                                                                          }
                                                                                                                          }

                                                                                                                          public class MyClass1
                                                                                                                          {
                                                                                                                          internal void foo()
                                                                                                                          {
                                                                                                                          Console.WriteLine("hello from foo");

                                                                                                                          }
                                                                                                                          }


                                                                                                                          Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing. Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing.



                                                                                                                          Why it occurs?




                                                                                                                          • NullReferenceException arises due to our fault for not checking the object’s value. We often leave the object values unchecked in the code development.


                                                                                                                          • It also arises when we forget to instantiate our objects. Using methods, properties, collections etc. which can return or set null values can also be the cause of this exception.



                                                                                                                          How can it be avoided?



                                                                                                                          There are various ways and methods to avoid this renowned exception:




                                                                                                                          1. Explicit Checking: We should adhere to the tradition of checking the objects, properties, methods, arrays, and collections whether they are null. This can be simply implemented using conditional statements like if-else if-else etc.


                                                                                                                          2. Exception handling: One of the important ways of managing this exception. Using simple try-catch-finally blocks we can control this exception and also maintain a log of it. This can be very useful when your application is on production stage.


                                                                                                                          3. Null operators: Null Coalescing operator and null conditional operators can also be used in handy while setting values to objects, variables, properties and fields.


                                                                                                                          4. Debugger: For developers, we have the big weapon of Debugging with us. If have we face NullReferenceException during the development face we can use the debugger to get to the source of the exception.


                                                                                                                          5. In-built method: System methods such as GetValueOrDefault(),IsNullOrWhiteSpace() and IsNullorEmpty() checks for nulls and assign the default value if there is a null value.



                                                                                                                          There are many good answers already here. You can also check more detailed description with examples on my blog.



                                                                                                                          Hope this helps too!






                                                                                                                          share













                                                                                                                          share


                                                                                                                          share








                                                                                                                          edited Oct 18 '17 at 7:31


























                                                                                                                          community wiki





                                                                                                                          3 revs, 2 users 97%
                                                                                                                          Wasim














                                                                                                                          • You basically copied half of that blog post and added nothing new that existing answers don't address.

                                                                                                                            – CodeCaster
                                                                                                                            Jul 18 '17 at 14:41











                                                                                                                          • @codecaster Is it said copying when you rewrite a summary from your own blog. I know there is nothing new in my answer and nothing new that previous answers dont have but I wish to contribute in more sophisticated way and let others understand the way I understood. Will be glad even if it helps a single person. In good faith.

                                                                                                                            – Wasim
                                                                                                                            Jul 18 '17 at 18:01



















                                                                                                                          • You basically copied half of that blog post and added nothing new that existing answers don't address.

                                                                                                                            – CodeCaster
                                                                                                                            Jul 18 '17 at 14:41











                                                                                                                          • @codecaster Is it said copying when you rewrite a summary from your own blog. I know there is nothing new in my answer and nothing new that previous answers dont have but I wish to contribute in more sophisticated way and let others understand the way I understood. Will be glad even if it helps a single person. In good faith.

                                                                                                                            – Wasim
                                                                                                                            Jul 18 '17 at 18:01

















                                                                                                                          You basically copied half of that blog post and added nothing new that existing answers don't address.

                                                                                                                          – CodeCaster
                                                                                                                          Jul 18 '17 at 14:41





                                                                                                                          You basically copied half of that blog post and added nothing new that existing answers don't address.

                                                                                                                          – CodeCaster
                                                                                                                          Jul 18 '17 at 14:41













                                                                                                                          @codecaster Is it said copying when you rewrite a summary from your own blog. I know there is nothing new in my answer and nothing new that previous answers dont have but I wish to contribute in more sophisticated way and let others understand the way I understood. Will be glad even if it helps a single person. In good faith.

                                                                                                                          – Wasim
                                                                                                                          Jul 18 '17 at 18:01





                                                                                                                          @codecaster Is it said copying when you rewrite a summary from your own blog. I know there is nothing new in my answer and nothing new that previous answers dont have but I wish to contribute in more sophisticated way and let others understand the way I understood. Will be glad even if it helps a single person. In good faith.

                                                                                                                          – Wasim
                                                                                                                          Jul 18 '17 at 18:01











                                                                                                                          -5















                                                                                                                          You are trying to access an object that isn't created or currently not in memory.




                                                                                                                          When I get this error, I add the following code to any event handler that triggers the error.



                                                                                                                          if (!IsLoaded) return;


                                                                                                                          The ! means "not", FYI. So if the object in question isn't loaded, the program terminates the execution and prevents the crash.





                                                                                                                          share





















                                                                                                                          • 6





                                                                                                                            This makes no sense at all.

                                                                                                                            – DavidG
                                                                                                                            Jul 5 '17 at 12:37











                                                                                                                          • It works for me when an event handler fires but the object it's looking for hasn't been created yet. Last time it was a TextChanged event for a textbox.

                                                                                                                            – Nathan
                                                                                                                            Jul 5 '17 at 20:13
















                                                                                                                          -5















                                                                                                                          You are trying to access an object that isn't created or currently not in memory.




                                                                                                                          When I get this error, I add the following code to any event handler that triggers the error.



                                                                                                                          if (!IsLoaded) return;


                                                                                                                          The ! means "not", FYI. So if the object in question isn't loaded, the program terminates the execution and prevents the crash.





                                                                                                                          share





















                                                                                                                          • 6





                                                                                                                            This makes no sense at all.

                                                                                                                            – DavidG
                                                                                                                            Jul 5 '17 at 12:37











                                                                                                                          • It works for me when an event handler fires but the object it's looking for hasn't been created yet. Last time it was a TextChanged event for a textbox.

                                                                                                                            – Nathan
                                                                                                                            Jul 5 '17 at 20:13














                                                                                                                          -5












                                                                                                                          -5








                                                                                                                          -5








                                                                                                                          You are trying to access an object that isn't created or currently not in memory.




                                                                                                                          When I get this error, I add the following code to any event handler that triggers the error.



                                                                                                                          if (!IsLoaded) return;


                                                                                                                          The ! means "not", FYI. So if the object in question isn't loaded, the program terminates the execution and prevents the crash.





                                                                                                                          share
















                                                                                                                          You are trying to access an object that isn't created or currently not in memory.




                                                                                                                          When I get this error, I add the following code to any event handler that triggers the error.



                                                                                                                          if (!IsLoaded) return;


                                                                                                                          The ! means "not", FYI. So if the object in question isn't loaded, the program terminates the execution and prevents the crash.






                                                                                                                          share













                                                                                                                          share


                                                                                                                          share








                                                                                                                          answered Jul 5 '17 at 3:18


























                                                                                                                          community wiki





                                                                                                                          Nathan









                                                                                                                          • 6





                                                                                                                            This makes no sense at all.

                                                                                                                            – DavidG
                                                                                                                            Jul 5 '17 at 12:37











                                                                                                                          • It works for me when an event handler fires but the object it's looking for hasn't been created yet. Last time it was a TextChanged event for a textbox.

                                                                                                                            – Nathan
                                                                                                                            Jul 5 '17 at 20:13














                                                                                                                          • 6





                                                                                                                            This makes no sense at all.

                                                                                                                            – DavidG
                                                                                                                            Jul 5 '17 at 12:37











                                                                                                                          • It works for me when an event handler fires but the object it's looking for hasn't been created yet. Last time it was a TextChanged event for a textbox.

                                                                                                                            – Nathan
                                                                                                                            Jul 5 '17 at 20:13








                                                                                                                          6




                                                                                                                          6





                                                                                                                          This makes no sense at all.

                                                                                                                          – DavidG
                                                                                                                          Jul 5 '17 at 12:37





                                                                                                                          This makes no sense at all.

                                                                                                                          – DavidG
                                                                                                                          Jul 5 '17 at 12:37













                                                                                                                          It works for me when an event handler fires but the object it's looking for hasn't been created yet. Last time it was a TextChanged event for a textbox.

                                                                                                                          – Nathan
                                                                                                                          Jul 5 '17 at 20:13





                                                                                                                          It works for me when an event handler fires but the object it's looking for hasn't been created yet. Last time it was a TextChanged event for a textbox.

                                                                                                                          – Nathan
                                                                                                                          Jul 5 '17 at 20:13










                                                                                                                          1 2
                                                                                                                          next




                                                                                                                          protected by ken2k Dec 4 '13 at 10:11



                                                                                                                          Thank you for your interest in this question.
                                                                                                                          Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                                                          Would you like to answer one of these unanswered questions instead?



                                                                                                                          Popular posts from this blog

                                                                                                                          Coverage of Google Street View

                                                                                                                          Full-time equivalent

                                                                                                                          Surfing