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
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,530
  • 8
  • 60
  • 103
33
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
  • 11,113
  • 66
  • 219
  • 392
33
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
  • 810
  • 6
  • 12
32
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…
yigitt
  • 778
  • 3
  • 9
  • 16
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,829
  • 1
  • 20
  • 21
31
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
  • 31,198
  • 62
  • 198
  • 313
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
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
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
  • 3,263
  • 6
  • 39
  • 47
28
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
  • 6,119
  • 6
  • 33
  • 45
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,630
  • 7
  • 39
  • 52
26
votes
2 answers

How to test for an empty generic.dictionary collection?

How do I test a generic dictionary object to see whether it is empty? I want to run some code as follows: while (reportGraphs.MoveNext()) { reportGraph = (ReportGraph)reportGraphs.Current.Value; report.ContainsGraphs = true; …
DEH
  • 1,647
  • 3
  • 25
  • 45
26
votes
18 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
  • 20,110
  • 21
  • 77
  • 129
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
  • 4,671
  • 16
  • 47
  • 76
25
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…
sgtfrankieboy
  • 1,020
  • 3
  • 13
  • 30