Questions tagged [nullreferenceexception]

The .NET exception that is thrown when there is an attempt to reference (or use) a null or uninitialized object.

A NullReferenceException occurs when you try to reference an object in your code that does not exist. For example, you may have tried to use an object without using the New keyword first, or tried to use an object whose value is set to null (Nothing in Visual Basic).

Troubleshooting Exceptions: System.NullReferenceException

Also, see "What is a NullReferenceException in .NET and how do I fix it?" for some hints.

2780 questions
6
votes
3 answers

Record Equals or GetHashCode throws NullReferenceException

I have a number of records that look like this: [] type Rec1 = { [] mutable field1 : int; [] mutable field2 : string; } [] type Rec2 = { [] mutable field3 : Rec1; …
Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
6
votes
1 answer

NullReferenceException in System.Runtime.CompilerServices.AsyncServices.b__1()

I have a .NET 4.0 web project and use Microsoft.Bcl.Async to support async/await feature. I've noticed that in production environment IIS worker process continiously crashes due to NullReferenceException which is thrown at…
Albert
  • 63
  • 1
  • 4
6
votes
3 answers

Why does calling an Action, that is null, throw NullReferenceException?

Why do I have to check if an Action is not null to avoid getting a NullReferenceException? Isn't it logical if there is no action, then it's okay to just do nothing and proceed? I don't understand why it has to throw an exception. Action is a class,…
user1306322
  • 8,561
  • 18
  • 61
  • 122
6
votes
2 answers

How to handle a NullReference Exception c#

I am trying to handle a NullReference Exception, but i am confused how to handle that. Here is my sample code where a NullReference exception is raised: private Customer GetCustomer(string unformatedTaxId) { …
user2619542
  • 223
  • 1
  • 5
  • 11
6
votes
1 answer

How can DateTimeFormatInfo.CurrentInfo be null

I have the following code in my C# application. DateTimeFormatInfo.CurrentInfo.DayNames ReSharper 7.1.1 is highlighting the fact that the DateTimeFormatInfo.CurrentInfo could cause a null reference exception. Under what circumstances would this…
6
votes
3 answers

NullReferenceException while objects involved are valid

NullReferenceException is being thrown on a line on which all involved objects are valid. StackTrace shows the line # is 432. The code is Here, Flags and tempFlags both are datatables. Data types of columns of both datatables are primitive…
bjan
  • 2,000
  • 7
  • 32
  • 64
6
votes
3 answers

Are a combobox's items null when empty?

Resharper wanted me to change this code: foreach (var item in cmbxColor1.Items) { cmbxColor2.Items.Add(item); . . . ...because it said, "Possible 'System.NullReferencesException'" So it should be this: foreach (var item in…
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
6
votes
2 answers

Wrapping List.Add() gives - Object reference not set to an instance of an object

I want to wrap List class with my custom class. As for now I have something like this ; public class PriorityListOfNodes { private List list_; private IComparer sorter_; public List List_ { …
Patryk
  • 22,602
  • 44
  • 128
  • 244
6
votes
2 answers

LINQ NullReferenceException on DefaultIfEmpty

I'm looking for a solution to the problem of having the DefaultIfEmpty() extension method not picking up null values when used in a LINQ outer join. Code as follows: var SummaryLossesWithNets = (from g in SummaryLosses …
Richard Todd
  • 2,406
  • 5
  • 32
  • 40
6
votes
5 answers

Getting NullReferenceException in impossible situation (when checking for null)

I have a very simple check right at the beginning of one of my methods as follows: public void MyMethod(MyClass thing) { if(thing == null) throw new ArgumentNullException("thing"); //Do other stufff.... } But I'm getting…
mutex
  • 7,536
  • 8
  • 45
  • 66
6
votes
3 answers

What is the CLR implementation behind raising/generating a null reference exception?

We do come across this particular and one of the most common exception in our coding/development life day or another day. My Question is NOT about WHY (I am aware it raises when we try to access properties of a reference variable which actually…
Sumeet
  • 905
  • 1
  • 14
  • 32
5
votes
1 answer

How to debug NullReferenceException in PresentationFramework?

My .NET 4 application uses the WPF TreeView which I have enhanced to enable keyboard navigation and lazy loading, for example. My problem is that, very rarely, the exception shown below is thrown. This happened to me only once during months of…
Helge Klein
  • 8,829
  • 8
  • 51
  • 71
5
votes
6 answers

A nicer way to handle null references in a object hierarcy

I’m looking for a nice way to handle a null reference in object hierarchy. ie: if(null == Object1.Object2.Object3.Property) This example will throw a Null Reference exception if say Object2 is null. In my case I don't care what is null, just that…
squig
  • 755
  • 7
  • 18
5
votes
2 answers

Extension Method and Member Method : why each is implemented differently by compilers (internally)?

Consider this code: A a = null; a.f(); //Will it throw NullReferenceException? Will the above throw NullReferenceException? The answer is : it depends on what f() is. If it's a member method, then yes, it will throw exception. If it's an…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
5
votes
2 answers

ToString() returns null?

I am trying to determine cause of 'Null reference exception' on published remote site. So, I can't debug it directly and can operate only with logs. So my question is: Is it possible, that .ToString() method of any of built-in .NET types returns…
Sergey Metlov
  • 25,747
  • 28
  • 93
  • 153