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

Checking session if empty or not

I want to check that session is null or empty i.e. some thing like this: if(Session["emp_num"] != null) { if (!string.IsNullOrEmpty(Session["emp_num"].ToString())) { //The code } } Or just …
Anyname Donotcare
  • 10,000
  • 54
  • 200
  • 355
32
votes
5 answers

When can a null check throw a NullReferenceException

I know this might seem impossible at first and it seemed that way to me at first as well, but recently I have seen exactly this kind of code throw a NullReferenceException, so it is definitely possible. Unfortunately, there are pretty much no…
The Red Fox
  • 781
  • 4
  • 12
32
votes
2 answers

Impossible NullReferenceException?

I'm investigating an exception that a colleague just got while running an application through Visual Studio 2010: System.NullReferenceException was unhandled by user code Message=Object reference not set to an instance of an object. …
Eamon
  • 1,569
  • 1
  • 18
  • 21
30
votes
4 answers

When exactly do nullable types throw exceptions?

Consider the following code: int? x = null; Console.Write ("Hashcode: "); Console.WriteLine(x.GetHashCode()); Console.Write("Type: "); Console.WriteLine(x.GetType()); When executed, it writes that Hashcode is 0, but fails with…
30
votes
2 answers

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 this.MyObject?.MyMethod() I would've expected that…
30
votes
3 answers

NullReferenceException when setting AutoSizeMode to AllCells in DataGridView

I am manually binding an entity framework code first table to a datagridview. When I set the AutoSizeMode to AllCells and add an instance to the table I get a NullReferenceException during Add. The code runs like…
29
votes
3 answers

Error checking for NULL in VBScript

I have the following VBScript in a Classic ASP page: function getMagicLink(fromWhere, provider) dim url url = "magic.asp?fromwhere=" & fromWhere If Not provider is Nothing Then ' Error occurs here url = url & "&provider=" &…
Vivian River
  • 28,530
  • 54
  • 179
  • 298
27
votes
5 answers

How can a readonly static field be null?

So here's an excerpt from one of my classes: [ThreadStatic] readonly static private AccountManager _instance = new AccountManager(); private AccountManager() { } static public AccountManager Instance { get {…
gerrod
  • 4,135
  • 5
  • 27
  • 36
27
votes
7 answers

findViewById returns NULL when using Fragment

I'm new to Android developing and of course on Fragments. I want to access the controls of my fragment in main activity but 'findViewById' returns null. without fragment the code works fine. Here's part of my code: The fragment:
mammadalius
  • 2,955
  • 5
  • 34
  • 44
26
votes
2 answers

NullReferenceException when doing InsertOnSubmit in LINQ to SQL

In my database I have a table called StaffMembers when I bring this into my .net Project as through linq-to-sql an entity class StaffMember is created Now I have also created a partial class StaffMember in my project also, to add extra properties…
soldieraman
  • 2,560
  • 7
  • 37
  • 52
25
votes
1 answer

System.NullReferenceException in App_Web_*.dll

I am having an odd issue. My MVC application seems to be working perfectly fine except for one view page. The view page in question (Organization/Edit) gets a 'NullReferenceException' on every code item on the page. Whether it is Html.TextBoxFor()…
MaylorTaylor
  • 3,614
  • 13
  • 41
  • 67
25
votes
16 answers

avoiding null reference exceptions

Apparently the vast majority of errors in code are null reference exceptions. Are there any general techniques to avoid encountering null reference errors? Unless I am mistaken, I am aware that in languages such as F# is it not possible to have a…
Nippysaurus
  • 19,402
  • 18
  • 71
  • 124
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
  • 119,009
  • 50
  • 280
  • 431
23
votes
1 answer

MVC5 Razor NullReferenceException in Model

For some reason I'm getting a NullReferenceException whenever I try to access my model. Here is the code from my controller: public async Task Bar(string fooSlug, int barId) { var foo = await mediaService.GetFoo(fooSlug); var…
22
votes
1 answer

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; TestClass t2 = (TestClass)t1; maybe in the…
UfukSURMEN
  • 609
  • 2
  • 7
  • 15