Skip to main content

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

  1. Object variables that are uninitialized and hence point to nothing. In this case, if you access members 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; (which is a shortcut for Nullable<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 in 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 non-nullable variable int b; then you should do assignments like if (a.HasValue) { b = a.Value; } or shorter if (a != null) { b = a; }.
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.
                                       // thereThere is no Person to get an Age from.
    }
}

Here comboBox1 is created before label1. If comboBox1_SelectionChanged attempts to reference `label1label1, it will not yet have been created.

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

  1. Object variables that are uninitialized and hence point to nothing. In this case, if you access members 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; (which is a shortcut for Nullable<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 non-nullable variable int b; then you should do assignments like if (a.HasValue) { b = a.Value; } or shorter if (a != null) { b = a; }.
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.
    }
}

Here comboBox1 is created before label1. If comboBox1_SelectionChanged attempts to reference `label1, it will not yet have been created.

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.

  1. Object variables that are uninitialized and hence point to nothing. In this case, if you access members 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; (which is a shortcut for Nullable<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 in 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 non-nullable variable int b; then you should do assignments like if (a.HasValue) { b = a.Value; } or shorter if (a != null) { b = a; }.
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.
    }
}

Here comboBox1 is created before label1. If comboBox1_SelectionChanged attempts to reference label1, it will not yet have been created.

Minor typo
Source Link
tripleee
  • 191.7k
  • 37
  • 318
  • 367

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

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.

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.

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.

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

While the new keyword is used, it only creates a new instance of Book, but not a new instance of Person, so the Author property is still null.

Fixed some grammar issues. Feel free to rollback if it is not ideal or contrary to the original meaning
Source Link
Gaurav Mall
  • 2.4k
  • 1
  • 21
  • 35
  1. Object variables whichthat are uninitialized and hence point to nothing. In this case, if you access members 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; (which is a shortcut for Nullable<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 non-nullable variable int b; then you should do assignments like if (a.HasValue) { b = a.Value; } or shorter if (a != null) { b = a; }.

Methods call you expect to returncall expecting an instance can return null, for example when the object being sought cannot be found. You can choose to return a default value when this is the case:

When you know during development that a method maybe cancould, but never should return null, you can use Debug.Assert() to break as soon as possible when it does occur:

Introduced in C# 8 there, null context'scontexts and nullable reference types perform static analysis on variables and providesprovide a compiler warning if a value can be potentially null or have been set to null. The nullable reference types allowsallow types to be explicitly allowed to be null.

  • enable: The nullable annotation context is enabled. The nullable warning context is enabled. Variables of a reference type, string, for example, are non-nullable. All nullability warnings are enabled.
  • disable: The nullable annotation context is disabled. The nullable warning context is disabled. Variables of a reference type are oblivious, just like earlier versions of C#. All nullability warnings are disabled.
  • safeonly: The nullable annotation context is enabled. The nullable warning context is safeonly. Variables of a reference type are nonnullablenon-nullable. All safety nullability warnings are enabled.
  • warnings: The nullable annotation context is disabled. The nullable warning context is enabled. Variables of a reference type are oblivious. All nullability warnings are enabled.
  • safeonlywarnings: The nullable annotation context is disabled. The nullable warning context is safeonly. Variables of a reference type are oblivious. All safety nullability warnings are enabled.

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.

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 whichthat 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".

  1. Object variables which are uninitialized and hence point to nothing. In this case, if you access members 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; (which is a shortcut for Nullable<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 non-nullable variable int b; then you should do assignments like if (a.HasValue) { b = a.Value; } or shorter if (a != null) { b = a; }.

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:

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:

Introduced in C# 8 there null context's and nullable reference types perform static analysis on variables and provides a compiler warning if a value can be potentially null or have been set to null. The nullable reference types allows types to be explicitly allowed to be null.

  • enable: The nullable annotation context is enabled. The nullable warning context is enabled. Variables of a reference type, string for example, are non-nullable. All nullability warnings are enabled.
  • disable: The nullable annotation context is disabled. The nullable warning context is disabled. Variables of a reference type are oblivious, just like earlier versions of C#. All nullability warnings are disabled.
  • safeonly: The nullable annotation context is enabled. The nullable warning context is safeonly. Variables of a reference type are nonnullable. All safety nullability warnings are enabled.
  • warnings: The nullable annotation context is disabled. The nullable warning context is enabled. Variables of a reference type are oblivious. All nullability warnings are enabled.
  • safeonlywarnings: The nullable annotation context is disabled. The nullable warning context is safeonly. Variables of a reference type are oblivious. All safety nullability warnings are enabled.

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.

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

  1. Object variables that are uninitialized and hence point to nothing. In this case, if you access members 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; (which is a shortcut for Nullable<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 non-nullable variable int b; then you should do assignments like if (a.HasValue) { b = a.Value; } or shorter if (a != null) { b = a; }.

Methods you call expecting an instance can return null, for example when the object being sought cannot be found. You can choose to return a default value when this is the case:

When you know during development that a method could, but never should return null, you can use Debug.Assert() to break as soon as possible when it does occur:

Introduced in C# 8, null contexts and nullable reference types perform static analysis on variables and provide a compiler warning if a value can be potentially null or have been set to null. The nullable reference types allow types to be explicitly allowed to be null.

  • enable: The nullable annotation context is enabled. The nullable warning context is enabled. Variables of a reference type, string, for example, are non-nullable. All nullability warnings are enabled.
  • disable: The nullable annotation context is disabled. The nullable warning context is disabled. Variables of a reference type are oblivious, just like earlier versions of C#. All nullability warnings are disabled.
  • safeonly: The nullable annotation context is enabled. The nullable warning context is safeonly. Variables of a reference type are non-nullable. All safety nullability warnings are enabled.
  • warnings: The nullable annotation context is disabled. The nullable warning context is enabled. Variables of a reference type are oblivious. All nullability warnings are enabled.
  • safeonlywarnings: The nullable annotation context is disabled. The nullable warning context is safeonly. Variables of a reference type are oblivious. All safety nullability warnings are enabled.

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.

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 that 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".

"null dereference exception" => `NullReferenceException`
Source Link
nalka
  • 2.8k
  • 2
  • 23
  • 40
Loading
Note on events in VB.NET
Source Link
user20416
  • 15.5k
  • 9
  • 76
  • 149
Loading
fix broken heading by adding a space
Source Link
etarhan
  • 4.2k
  • 2
  • 18
  • 29
Loading
added 17 characters in body
Source Link
Rand Random
  • 7.5k
  • 10
  • 48
  • 98
Loading
The edit fixed the "and and", but replaced it with awkward language. Subsequent editors can fight over the extra comma I added if they want to.
Source Link
John Saunders
  • 161.9k
  • 26
  • 252
  • 403
Loading
Rollback to Revision 50 - Edit approval overridden by post owner or moderator
Source Link
John Saunders
  • 161.9k
  • 26
  • 252
  • 403
Loading
Changed "and and" to "and you have to" in the debugging section
Source Link
Loading
format code , some grammar spelling fixes
Source Link
LopDev
  • 839
  • 12
  • 27
Loading
Added information about the C# 8 nullable reference types and nullable contexts
Source Link
Glenn Watson
  • 2.9k
  • 1
  • 22
  • 31
Loading
Changed link to https
Source Link
Kolappan N
  • 4k
  • 3
  • 40
  • 45
Loading
Improve formatting
Source Link
habib
  • 2.5k
  • 5
  • 28
  • 44
Loading
Added some more info to the bottom line section.
Source Link
Matt
  • 27.4k
  • 19
  • 131
  • 202
Loading
added 2384 characters in body
Source Link
Eric Lippert
  • 663.2k
  • 185
  • 1.3k
  • 2.1k
Loading
added 2538 characters in body
Source Link
Eric Lippert
  • 663.2k
  • 185
  • 1.3k
  • 2.1k
Loading
added 422 characters in body
Source Link
Matt
  • 27.4k
  • 19
  • 131
  • 202
Loading
Reverting changes from revision 33. (The code sample doesn't even compile and doesn't demonstrate a valid problem.)
Source Link
Bradley Grainger
  • 28.3k
  • 4
  • 95
  • 117
Loading
Rollback to Revision 38
Source Link
Bradley Grainger
  • 28.3k
  • 4
  • 95
  • 117
Loading
Rollback to Revision 37
Source Link
Bradley Grainger
  • 28.3k
  • 4
  • 95
  • 117
Loading
Rollback to Revision 33
Source Link
Bradley Grainger
  • 28.3k
  • 4
  • 95
  • 117
Loading
Added section about nested object/collection initializers
Source Link
grek40
  • 13.5k
  • 1
  • 29
  • 54
Loading
added 28 characters in body
Source Link
user6269864
user6269864
Loading