2,773 questions
1864
votes
26
answers
2.5m
views
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?
324
votes
22
answers
1.4m
views
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 ...
270
votes
8
answers
2.4m
views
What does "Object reference not set to an instance of an object" mean? [duplicate]
I am receiving this error and I'm not sure what it means?
Object reference not set to an instance of an object.
220
votes
22
answers
702k
views
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....
198
votes
2
answers
9k
views
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 ...
127
votes
20
answers
191k
views
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 ...
70
votes
2
answers
717k
views
How to solve Object reference not set to an instance of an object.? [duplicate]
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<string> list;
protected void ...
67
votes
2
answers
10k
views
C# Error with null-conditional operator and await
I'm experiencing an interesting System.NullReferenceException whilst using the new null-conditional operator in C#. The following code gives me a NullReferenceException if "MyObject" is null:
await ...
63
votes
6
answers
35k
views
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 ...
62
votes
4
answers
4k
views
Why is casting a dynamic of type object to object throwing a null reference exception?
I have the following function:
public static T TryGetArrayValue<T>(object[] array_, int index_)
{
... //some checking goes up here not relevant to question
dynamic boxed = array_[...
55
votes
3
answers
71k
views
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,...
51
votes
16
answers
18k
views
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 ...
49
votes
3
answers
25k
views
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 ...
44
votes
5
answers
67k
views
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 ...
37
votes
1
answer
42k
views
Cast null value to a type
If we cast some null variable to a type, I expect the compiler to throw some exception, but it doesn't. Why?
I mean
string sample1 = null as string;
string sample2 = (string)null;
object t1 = null;
...