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.

2616 questions
1874
votes
28 answers

What is a NullReferenceException, and how do I fix it?

I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do to fix this error?
John Saunders
  • 157,405
  • 24
  • 229
  • 388
254
votes
21 answers

Checking if an object is null in C#

I would like to prevent further processing on an object if it is null. In the following code I check if the object is null by either: if (!data.Equals(null)) and if (data != null) However, I receive a NullReferenceException at dataList.Add(data).…
developer
  • 6,414
  • 14
  • 45
  • 56
218
votes
8 answers

What does "Object reference not set to an instance of an object" mean?

I am receiving this error and I'm not sure what it means? Object reference not set to an instance of an object.
mohammad reza
  • 2,892
  • 6
  • 27
  • 38
195
votes
2 answers

Why would finding a type's initializer throw a NullReferenceException?

This has got me stumped. I was trying to optimize some tests for Noda Time, where we have some type initializer checking. I thought I'd find out whether a type has a type initializer (static constructor or static variables with initializers) before…
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
143
votes
19 answers

Value cannot be null. Parameter name: source

This is probably the biggest waste of time problem I have spent hours on solving for a long time. var db = new hublisherEntities(); establishment_brands est = new establishment_brands(); est.brand_id = 1; est.establishment_id = 1; est.price =…
danielovich
  • 7,859
  • 7
  • 23
  • 28
108
votes
19 answers

C# elegant way to check if a property's property is null

In C#, say that you want to pull a value off of PropertyC in this example and ObjectA, PropertyA and PropertyB can all be null. ObjectA.PropertyA.PropertyB.PropertyC How can I get PropertyC safely with the least amount of code? Right now I would…
Jon Kragh
  • 4,169
  • 5
  • 24
  • 26
62
votes
4 answers

Why is casting a dynamic of type object to object throwing a null reference exception?

I have the following function: public static T TryGetArrayValue(object[] array_, int index_) { ... //some checking goes up here not relevant to question dynamic boxed = array_[index_]; return (T)boxed; } When I call it in the…
bedo
  • 856
  • 8
  • 15
58
votes
6 answers

How did I get this NullReferenceException error here right after the constructor?

I've had an asp.net website running live on our intranet for a couple of weeks now. I just got an email from my application_error emailer method with an unhandled exception. Here it is (I've cleaned up some of the paths to make it better…
RodH257
  • 3,292
  • 5
  • 32
  • 46
50
votes
16 answers

Why is [Owin] throwing a null exception on new project?

I have a rather strange issue i'm not sure how to fix or if i can even fix it. I've done some research into the issue but can't find an answer to what's causing it. I'm following a rather simple guide at…
micah hawman
  • 509
  • 1
  • 4
  • 3
50
votes
2 answers

How to solve Object reference not set to an instance of an object.?

In my asp.net program.I set one protected list.And i add a value in list.But it shows Object reference not set to an instance of an object error protected List list; protected void Page_Load(object sender, EventArgs e) { …
r.vengadesh
  • 1,555
  • 3
  • 17
  • 33
42
votes
3 answers

Using VB.NET IIF I get NullReferenceException

I am doing a little debugging, and so I want to log the eventArgs value I have a simple line that basically does: logLine = "e.Value: " + IIf(e.Value Is Nothing, "", e.Value.ToString()) The way I understand the IIF function, if the e.Value is…
Nathan Koop
  • 23,022
  • 23
  • 86
  • 121
40
votes
5 answers

httpcontext.current.server.mappath Object reference not set to an instance of an object

I am using the following code within a class: string filePath = HttpContext.Current.Server.MapPath("~/email/teste.html"); The file teste.html is in the folder But when it will open the file the following error is being generated: Object reference…
soamazing
  • 1,396
  • 7
  • 21
  • 30
40
votes
3 answers

How to use global var across files in a package?

I have the following file structure: models/db.go type DB struct { *sql.DB } var db *DB func init() { dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", DB_USER, DB_PASSWORD, DB_NAME) db, err :=…
lionelmessi
  • 948
  • 1
  • 9
  • 16
35
votes
11 answers

"Object reference not set to an instance of an object" when building my cloud project

When I build my solution with a bunch of cloud projects, I see one or more "Error: Object reference not set to an instance of an object" messages in the output. When I try to run one of the cloud projects, I get the popup "There were build errors.…
Matthijs Wessels
  • 6,234
  • 6
  • 54
  • 97
33
votes
3 answers

Why does Entity Framework return null List<> instead of empty ones?

I'm pretty new in the ASP .NET MVC world. Maybe, that's the reason I can't explain to myself the cause of what is, for me, an annoying problem. I have one class with One-To-Many relashionship. class MyClass{ public List otherClasses…
1
2 3
99 100