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
11
votes
3 answers

Why don't object reference error exceptions in .net tell me which object was null?

Maybe asking the question betrays my lack of knowledge about the process, but then again, there's no better reason to ask! Tracking these down can be frustrating because stack traces can help me know where to start looking but not which object was…
jrsconfitto
  • 1,134
  • 1
  • 11
  • 33
11
votes
1 answer

NullReferenceException on DropDownList.Items.FindByValue()

I hope someone can help me solve a little mystery.... This code is in production, and working there. The issue is occuring on my localhost (I'm testing a change I made before I release to staging). This was working until 2 days ago and I have no…
CDR12
  • 481
  • 6
  • 21
10
votes
8 answers

Can Visual Studio tell me which reference threw a NullReferenceException?

I'm writing unit tests for an MVC web app, and I've been getting null reference exceptions because the mocked-up test objects are only partly initialized. I know which line is throwing the exceptions, and it looks something like this: return…
10
votes
3 answers

Catch NullReferenceException or test for Nothing first?

We have a property whose job is to look up a description. If the lookup fails it should show an empty string. So we can code the property like this: If foo.bar Is Not Nothing Then Return foo.bar.Description Else Return String.Empty End If But…
hawbsl
  • 15,313
  • 25
  • 73
  • 114
10
votes
2 answers

Null Dapper.net query still returning Null Reference Exception with FirstOrDefault()

I would like to return the max Id from a table using Dapper.net var x = connection.Query("SELECT max(val) FROM info").FirstOrDefault(); This works - unless no row exists then I get an Object reference not set to an instance of an…
niico
  • 11,206
  • 23
  • 78
  • 161
10
votes
1 answer

Regex.MatchData returning null: why not Option[String]?

Is there any particular reason why Regex.MatchData.group(i: Int): java.lang.String returns null rather than Option[String]? Is there a "Scala Way" to handle nulls in Scala?
BefittingTheorem
  • 10,459
  • 15
  • 69
  • 96
10
votes
3 answers

ReSharper Possible Null Exception when null is already checked

This is ReSharper 7 with Visual Studio 2012. With the sample below // This code works fine and as expected and ReShrper is happy with it if (!string.IsNullOrWhiteSpace(extension) && extension.Length == 3) { // do something } // ReSharper…
Adam
  • 3,872
  • 6
  • 36
  • 66
10
votes
6 answers

How to check for null in nested references

Looking for some best-practice guidance. Let's say I have a line of code like this: Color color = someOrder.Customer.LastOrder.Product.Color; where Customer, LastOrder, Product, and Color could be null under normal conditions. I'd like color to be…
Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
9
votes
4 answers

Why does this statement throw a null reference exception?

This will throw a null reference exception when InnerException is null. String s = " inner exception: " + e.InnerException == null ? "None" : e.InnerException.Message; but this won't: String s = " inner exception: " + (e.InnerException == null ?…
Erix
  • 7,059
  • 2
  • 35
  • 61
9
votes
5 answers

Disposing the members that implement IDisposable

In my Dispose methods (like the one below), everytime i want to call someObj.Dispose() i also have a check for someObj!=null. Is that because of bad design on my part? Is their a cleaner way to ascertain that Dispose of all the members…
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
9
votes
3 answers

Application.Current is Null for Unit Tests

I have some methods in the code base that rely on Application.Current.Dispatcher.Invoke... to make sure things run on the GUI thread. I am currently trying to write unit tests for these methods but (as expected) Application.Current is null so I'm…
KrisTrip
  • 4,943
  • 12
  • 55
  • 74
8
votes
3 answers

How to fix "Object reference not set to an instance of an object" error in Orchard CMS

I'm building an orchard CMS site. Initially, I downloaded the zipped version, but VS complained of the error: "Object reference not set to an instance of an object" in project Markdown.csproj, line 1. This made no sense to me, so I tried installing…
snort
  • 2,285
  • 3
  • 19
  • 21
8
votes
3 answers

crystal report object reference not set to an instance of an object

I have crystal reports that keeps saying this error: {"Object reference not set to an instance of an object."} Stacktrace: at CrystalDecisions.Windows.Forms.PageControl.OnMouseMove(MouseEventArgs e) at…
user742102
  • 1,335
  • 8
  • 32
  • 51
8
votes
5 answers

Why does trying to access a property of null cause an exception in some languages?

The thing that really bothers me the most about some programming languages (e.g. C#, Javascript) is that trying to access a property of null causes an error or exception to occur. For example, in the following code snippet, foo = bar.baz; if bar is…
Peter Olson
  • 139,199
  • 49
  • 202
  • 242
8
votes
3 answers

Calling methods on a null reference in the context of a using clause is OK?

I was looking at the mvc-mini-profiler designed by the Stack Overflow team on Google Code and one thing on the getting started page struck me as particularly strange: var profiler = MiniProfiler.Current; // it's ok if this is null using…
Jake Petroules
  • 23,472
  • 35
  • 144
  • 225