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
5
votes
4 answers

Object reference not set to an instance of an object

I have a class Cell: public class Cell { public enum cellState { WATER, SCAN, SHIPUNIT, SHOT, HIT } public Cell() { currentCell = cellState.WATER; …
iTEgg
  • 8,212
  • 20
  • 73
  • 107
5
votes
3 answers

Why 'Object reference not set to an instance of an object' is not more descriptive?

As a developer, we often encounter that exception: NullReferenceException with the well known error message: Object reference not set to an instance of an object Is it not possible for the .NET framework to return something a little bit more…
joerage
  • 4,863
  • 4
  • 38
  • 48
5
votes
4 answers

NullReferenceException on closing if brace

I am using MVC with Razor views. In this particular view, I am passing a single instance of the class Bed. Bed has a property string Infection. Now in this instance, I have a boolean HasInfection defined in the view that I am using elsewhere to…
Phillip Copley
  • 4,238
  • 4
  • 21
  • 38
5
votes
2 answers

Why the following C# code throws NullReferenceException on nullable DateTime initialization?

Here's some simple code: static void Main(string[] args) { var coll = new List {"test", "test2", "test3"}; var filteredColl = coll.Select(x => x).ToList(); if (!filteredColl.Any()) { …
Mikael Koskinen
  • 12,306
  • 5
  • 48
  • 63
5
votes
2 answers

NullReferenceException when creating a thread

I was looking at this thread on creating a simple thread pool. There, I came across @MilanGardian's response for .NET 3.5 which was elegant and served my purpose: using System; using System.Collections.Generic; using System.Threading; namespace…
Legend
  • 113,822
  • 119
  • 272
  • 400
5
votes
2 answers

Custom SpecFlow TestGeneratorProvider registration causes NullReferenceException at test generation

I'm trying to add my CodedUI test generator plugin in SpecFlow 1.9. I'm trying to use the new plugin registration, since the old type of registration causes all kinds of issues in our solution, since we prefer not to put the custom Generator…
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
5
votes
1 answer

ASP.NET application throws System.NullReferenceException from Session.Remove implementation

We are getting random System.NullReferenceException in our ASP.NET web application. We are using inproc session state. Stacktrace: System.NullReferenceException: Object reference not set to an instance of an object. at…
5
votes
4 answers

Visual Studio null reference warning - why no error?

I've noticed something peculiar about Visual Studio. First, try typing this (C#) somewhere in a function: class Foo { public void Bar() { string s; int i = s.Length; } } Now, right away it will mark the s in…
Tesserex
  • 17,166
  • 5
  • 66
  • 106
5
votes
2 answers

System.NullReferenceException only in Release Build

I'm getting an "System.NullReferenceException: Object reference not set to an instance of an object." Error when I launch a release build of my web application. It tells me to do a debug build to get more information, but when I launch the debug…
Neaox
  • 1,933
  • 3
  • 18
  • 29
5
votes
8 answers

Error when I click publish | "Object reference not set to an instance of an object"

I'm trying to publish the files of a project I just created in Visual Studio 2012. When I click publish, the following window is displayed Already created a new web project (same type, asp.net mvc 4) and tried to make the same change and…
5
votes
2 answers

Interaction between two user controls

I'm on the verge of madness ... In the application I'm actually building, I'm dealing with two dynamically-added controls that need to interact with each other, but I've reduced the problem to an as-simple-as-I-can-make-it example with the controls…
Blumer
  • 5,005
  • 2
  • 33
  • 47
4
votes
2 answers

object reference not set to an instance of an object #3

I am getting the object reference error in just start of the method. For Ex.: 259: public ActionResult ShowAddress(FormCollection formCollection) 260: { In the above sample i am getting the error Line number 260.
Prasad
  • 58,881
  • 64
  • 151
  • 199
4
votes
1 answer

Entity Framework NullReferenceException during model creation

I am currently working on a project as a noob when it comes to WCF\EF, using entities\tables created by another developer. I am usually pretty quick at catching up on new things, and I have the small basics down on how to use the datacontext to do…
4
votes
2 answers

Filehelpers NullReferenceException when trying to write a null decimal value

When using the FileHelpers library I am getting a NullReferenceException when trying to write a .csv file. I have narrowed the problem down. Whenever I have a null decimal? it throws this exception. It works fine on reading, just not writing. I…
Erick
  • 853
  • 8
  • 17
4
votes
1 answer

Ironpython: Debugging a null reference exception

I have previously asked this question when I was seeing a null pointer exception. In that case it turned out that what I was seeing was in fact a bug in IronPython. Now I recently came across this error again when using a third party library…
Wilduck
  • 13,822
  • 10
  • 58
  • 90