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

LINQ InsertOnSubmit: NullReferenceException

I have this code: using DC = MV6DataContext; using MV6; // Business Logic Layer // ... public DC.MV6DataContext dc = new DC.MV6DataContext(ConnectionString); IP ip = new…
tsilb
  • 7,977
  • 13
  • 71
  • 98
16
votes
3 answers

c# code seems to get optimized in an invalid way such that an object value becomes null

I have the following code that exhibits a strange problem: var all = new FeatureService().FindAll(); System.Diagnostics.Debug.Assert(all != null, "FindAll must not return null"); System.Diagnostics.Debug.WriteLine(all.ToString()); // throws…
gxclarke
  • 1,953
  • 3
  • 21
  • 42
16
votes
3 answers

Can't detect whether Session variable exists

I'm trying to determine if a Session variable exists, but I'm getting the error: System.NullReferenceException: Object reference not set to an instance of an object. Code: // Check if the "company_path" exists in the Session context if…
Luke
  • 22,826
  • 31
  • 110
  • 193
15
votes
3 answers

await with null propagation System.NullReferenceException

I have the following code: await _user?.DisposeAsync(); Visual Studio highlights this code, saying 'Possible NullReferenceException' by the way, without await Visual Studio doesn't show this warning Why NullReferenceException is possible here?
15
votes
13 answers

How do I enforce null checking?

I'm working on a large project where, even with 10s of 1000s of automated tests and 100% code coverage, we're getting a ridiculous number of errors. About 95% of errors we get are NullReferenceExceptions. Is there any way to enforce null-checking at…
Juliet
  • 80,494
  • 45
  • 196
  • 228
14
votes
3 answers

How to check if a Datatable is Null or Nothing

How can I check if a DataTable has never been set, meaning it will be Null or Nothing? I don't mean an empty DataTable. For example: Dim dt As DataTable = TryCast(Session("dt"), DataTable) If dt.Rows.Count <> 0 Then 'Do something ! End If If…
Laurence
  • 7,633
  • 21
  • 78
  • 129
13
votes
4 answers

SignInManager.PasswordSignInAsync Throwing Null Object Exception

AccountController.cs houses the AccountController class. The Login method of that class is failing. Specifically, var result = await SignInManager.PasswordSignInAsync( model.Email, model.Password, model.RememberMe, shouldLockout: true); within…
Jeff Maner
  • 1,179
  • 9
  • 23
12
votes
1 answer

NLog GetCurrentClassLogger() NullReferenceException using StructureMap (Full Trust)

It seems like NLog can't use reflection for GetCurrentClassLogger(), even though my MVC 3 app is deployed in a Full Trust environment on IIS7. I'm using StructureMap 2.6.1 and the problem seems to appear sporadically between deploys. I can't figure…
Petrus Theron
  • 27,855
  • 36
  • 153
  • 287
12
votes
6 answers

ReSharper: how to remove "Possible 'System.NullReferenceException'" warning

Here is a piece of code: IUser user = managerUser.GetUserById(UserId); if ( user==null ) throw new Exception(...); Quote quote = new Quote(user.FullName, user.Email); Everything is fine here. But if I replace "if" line with the following…
Budda
  • 18,015
  • 33
  • 124
  • 206
12
votes
1 answer

NullReferenceException when creating ObjectContext in Using statement

Time once again to appeal to greater minds. I'm experiencing a very strange phenomenon. As the title states, I'm getting a NullReferenceException when trying to create an EF ObjectContext, but I only get the exception if I create the context within…
12
votes
4 answers

GetAdornerLayer mysteriously returning null

I've been using the same bit of code for several versions of my app with no problems, but I'm now mysteriously receiving NullRerefenceExceptions with the following: this.Loaded += delegate { deleteBrush = new DeleteBrushAdorner( background ); …
devios1
  • 36,899
  • 45
  • 162
  • 260
12
votes
2 answers

Can't understand why I'm getting this nullReference exception in ASP MVC when loading a page

I have a web application that is not giving me any problems on my computer, but when I deploy it to the server I get this error. It doesn't give me any source code lines to check and I don't have any idea why it may be happening Exception Details:…
paddingtonMike
  • 1,441
  • 1
  • 21
  • 37
12
votes
5 answers

When would SqlCommand.ExecuteReader() return null?

When using calling the SqlCommand.ExecuteReader() method, ReSharper tells me I have a possible NullReference exception when I use the SqlDataReader object afterwards. So with the following code: using (SqlConnection connection = GetConnection()) { …
Ray
  • 45,695
  • 27
  • 126
  • 169
11
votes
3 answers

Always see NullReference exception in designer with all silverlight business applications in visual studio 2010

Starting with the most simple case. If I press "File->new project->silverlight business application" it opens up to MainPage.xaml saying the message attached at the bottom of this post. It also does the same thing to any silverlight business…
11
votes
4 answers

In C#, should one check references passed to methods against null?

Well, a few months ago I asked a similar question about C and C++, but I've been paying more attention to C# lately due to the whole "Windows Phone" thing. So, in C#, should one bother to check against NULL at method boundaries? I think this is…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552