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
23
votes
6 answers

Initializing list property without "new List" causes NullReferenceException

using System; using System.Collections.Generic; class Parent { public Child Child { get; set; } } class Child { public List Strings { get; set; } } static class Program { static void Main() { // bad object initialization …
Ajai
  • 328
  • 3
  • 10
23
votes
4 answers

Why does this extension method throw a NullReferenceException in VB.NET?

From previous experience I had been under the impression that it's perfectly legal (though perhaps not advisable) to call extension methods on a null instance. So in C#, this code compiles and runs: // code in static class static bool IsNull(this…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
23
votes
1 answer

How do I break down a chain of member access expressions?

The Short Version (TL;DR): Suppose I have an expression that's just a chain of member access operators: Expression> e = x => x.foo.bar.baz; You can think of this expression as a composition of sub-expressions, each comprising one…
22
votes
0 answers

SAP Sybase SQL Anywhere NullReference Exception when openening and closing many connections in a service

Currently I've the problem that SAP Sybase SQL Anywhere randomly throws NullReferenceExceptions in a service which executes a lot of sql queries. The connections are always created in a using block and opened & closed correctly. There are not many…
BendEg
  • 20,098
  • 17
  • 57
  • 131
22
votes
8 answers

Visual Studio Cannot open cshtml Files

I have a problem, which I cannot solve. I can't open every cshtml-File in a Visual Studio c# mvc project (whatever which project i tried). I get the following error Msg (I try to translate to English): Object reference not set to an instance to an…
Maermot
  • 245
  • 1
  • 3
  • 8
22
votes
2 answers

GetHashCode on null fields?

How do I deal with null fields in GetHashCode function? Module Module1 Sub Main() Dim c As New Contact Dim hash = c.GetHashCode End Sub Public Class Contact : Implements IEquatable(Of Contact) Public Name As String Public…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
21
votes
4 answers

How Can a Stack Trace Point to the Wrong Line (the "return" Statement) - 40 Lines Off

I have twice now seen a NullReferenceException logged from a Production ASP.NET MVC 4 web application - and logged on the wrong line. Not wrong by a line or two (like you would get with a PDB mismatch), but wrong by the length of the entire…
John Saunders
  • 160,644
  • 26
  • 247
  • 397
21
votes
4 answers

VS2012: Property Pages isn't opening: Object reference not set to an instance of an object

I'm on Visual Studio Premium 2012, Version 11.0.50727.1 RTMREL, Windows 7 Enterprise (SP1). When I right click on the Solution, and click Properties, I get a popup window showing the the null reference exception (Object reference not set to an…
20
votes
1 answer

Getting the variable name for NullReferenceException

Stack traces for NullReferenceException is very uninformative, they just include the method name and the call stack. Any variable in a method can be null and it's hard to debug when the bug isn't reproducible on the dev machine. Do you know a way to…
Elmo
  • 6,409
  • 16
  • 72
  • 140
19
votes
10 answers

If null.Equals(null) why do I get a NullReferenceException

I have the following line of code: var selectedDomainID = lkuDomainType.EditValue.Equals(null) ? string.Empty : lkuDomainType.EditValue; Sometimes this generates a NullReferenceException. What I don't understand is why. Isn't the whole…
Refracted Paladin
  • 12,096
  • 33
  • 123
  • 233
19
votes
1 answer

Why does ResourceManager.GetResourceSet return null on the first request after a build? (C#)

I'm working on a large-ish web application built in C# (asp.net). I've got a simple aspx page that serves localized strings to the client browser for use in javascript controls. To get the strings, I do the following: ResourceManager _resources =…
Cole F
  • 191
  • 1
  • 1
  • 4
19
votes
3 answers

ASPNET WebForms Server Control is NULL in Page_Load

I have a very strange situation where my Label control is NULL in Page_Load. Should it even be possible? I have a Web Form: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MyNamespace.Templates.Pages.WebForm1"…
Svein Terje Gaup
  • 1,424
  • 15
  • 29
18
votes
3 answers

Null reference exception when generating a url with UrlHelper.Action method

For some reason, when certain bots visit the site, generating a url with the UrlHelper.Action method raises a null exception from System.Web.HttpServerVarsCollection.Get. I've done some debugging and the call stack originiates with an attempt to get…
Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
18
votes
4 answers

Sum() causes exception instead of returning 0 when no rows

I have this code (ok, I don't, but something similar :p) var dogs = Dogs.Select(ø => new Row { Name = ø.Name, WeightOfNiceCats = ø.Owner .Cats .Where(æ => !æ.Annoying) .Sum(æ…
Svish
  • 152,914
  • 173
  • 462
  • 620
17
votes
1 answer

How do I allow breaking on 'System.NullReferenceException' in VS2010?

I have a VS 2010 C# .NET 4 project. The issue is that the program is not breaking on 'NullReferenceException' errors during debugging. The output window will display the following: A first chance exception of type 'System.NullReferenceException'…
Mike Webb
  • 8,855
  • 18
  • 78
  • 111