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
1 answer

ReSharper: Possible NullReferenceException with Enumerator?

ReSharper notifies me about a possible System.NullReferenceException for the following code: IEnumerator edgeEnumerator = edgeData.GetEnumerator(); while (edgeEnumerator.MoveNext()) { ConvId fromConvId =…
Norbert
  • 4,239
  • 7
  • 37
  • 59
6
votes
3 answers

Can you capture the name of the object that throws a NullReferenceException?

Is there a way to find out what specific object caused a NullReferenceException? I've read the page about troubleshooting NullReferenceExceptions and it talks about inspecting variables in the debugger and looking at the exception message. What if…
Lews Therin
  • 3,707
  • 2
  • 27
  • 53
6
votes
4 answers

Object reference not set to an instance of object when using a List in C#

I have the following code snippet that produces a compilation error: public List batchaddresses; public MapFiles(string [] addresses) { for (int i = 0; i < addresses.Count(); i++) { …
JOE SKEET
  • 7,950
  • 14
  • 48
  • 64
6
votes
1 answer

Html.OpenIdSelectorScripts helper method throwing NullReferenceException

I keep getting this error when trying to navigate to my LogOn page: System.NullReferenceException: Object reference not set to an instance of an object The line of code that is throwing the error on is: <%= Html.OpenIdSelectorScripts(this,…
efleming
  • 1,269
  • 2
  • 10
  • 10
6
votes
1 answer

Using tasks getting "Object reference not set to an instance of an object"

first of all, I already searched through SO in many NullReferenceException questions. here and here I am getting an error "Object reference not set to an instance of an object" when I try to call Task.WaitAll(tasks); I am sure that I am initializing…
pandemic
  • 1,135
  • 1
  • 22
  • 39
6
votes
2 answers

Xamarin Android.FormsAppCompatActivity NullReferenceException on Startup

I stripped down my application as much as possible to find this problem. Still it appears: 11-12 15:56:27.908 I/MonoDroid(17629): System.NullReferenceException: Object reference not set to an instance of an object. 11-12 15:56:27.908…
Stefan R.
  • 438
  • 1
  • 5
  • 22
6
votes
4 answers

MVC5 / C# - Cannot perform runtime binding on a null reference

I'm trying to figure out what's causing a Cannot perform runtime binding on a null reference error from this code: var query = "SELECT Id, UserName, List_Order, LoggedIn " + "FROM AspNetUsers" + "WHERE LoggedIn = 1" + …
6
votes
2 answers

IEnumerable not null, but throwing a NullReferenceException on iteration

I have an IEnumerable that I run a foreach on. It's throwing a null reference exception in certain cases on the foreach line, it says ienumerable threw an exception of type 'System.NullReferenceException if (ienumerable != null) { foreach…
Erica Stockwell-Alpert
  • 4,624
  • 10
  • 63
  • 130
6
votes
1 answer

Android - null object reference when iterating List

I'm trying to copy a List 'usrs' which is created in an Inner class to a different list 'team_memebers'. After copying I try to iterate 'team_memebers' in the FOR loop, but I get a 'null object reference' error. The 'users' list contains the…
David Faizulaev
  • 4,651
  • 21
  • 74
  • 124
6
votes
5 answers

Why does the '{' throw a NullReferenceException in a static method?

This one is sort of esoteric. I ran into a NullReferenceException while trying to open a form (in the winforms designer) in a winforms project in visual studio 2008. The stack trace points to the fourth line of the following code: public static…
Zachary Yates
  • 12,966
  • 7
  • 55
  • 87
6
votes
1 answer

Null checking in binding expression

Does WPF binding expression syntax have a null-checking mechanism? So for example My TextBox shows the Address field of the first element of an array of People objects, like this: Text="{Binding AllPeople[0].Address}" AllPeople can at times be…
dotNET
  • 33,414
  • 24
  • 162
  • 251
6
votes
6 answers

I seem to have fallen into some massive, massive trouble with NullReferenceExceptions

Recently I'm developing a software that parses and displays XML information from a website. Simple enough right? I'm getting LOADS of NullReferenceExceptions. For example, this method: private void SetUserFriends(List list) { int x =…
Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254
6
votes
5 answers

WPF - Saving font to disk, then instantiating GlyphTypeface for font throws exception. Why?

I am mucking about with WPF glyphs and documents and have run into a null reference exception in the .Net 4 framework. I extract and save true-type fonts to disk as .ttf files, then try to create Glyphs based on the fonts. The first time I save a…
AndrewS
  • 3,450
  • 1
  • 21
  • 26
6
votes
2 answers

How to invoke a delegate with a null parameter?

I get a null exception if I try to pass a null parameter to a delegate during an invoke. Here's what the code looks like: public void RequestPhoto() { WCF.Service.BeginGetUserPhoto(Contact.UserID, new…
Rodney Burton
  • 427
  • 8
  • 18
6
votes
2 answers

TryGetValue - pass uninititialized value OK?

Take the following code for exhibit A: string sql; if (!GetQueries.TryGetValue(type.TypeHandle, out sql)) Documentation for Dictionary says that if the Key isn't found, the reference type will be set to null. OK, that's fine. If the key is found,…
Robert Achmann
  • 1,986
  • 3
  • 40
  • 66