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
8
votes
3 answers

Consuming JSON in WCF service method

In a larger project I am having trouble getting a WCF service method to consume a JSON parameter. So I produced a smaller test case and the behaviour is echoed. If I debug the service I can see the parameter value is null at the service call.…
Simon Rigby
  • 1,786
  • 4
  • 17
  • 28
8
votes
3 answers

How to handle NullReferenceException in a foreach?

foreach (string s in myField.getChilds()) { if (s == null) //handle null else //handle normal value } When I run my program i get a NullReferenceException because getChilds may return null. How can I make my program to…
raz3r
  • 3,071
  • 8
  • 44
  • 66
8
votes
7 answers

Should I check if argument is null if I'm going to use it immediately?

I'm so used to check if a method's argument is null (then proceed to throw an exception) that I almost don't even think about it anymore. If the argument is a reference type, it's there: if(arg == null) throw new…
Trauer
  • 1,981
  • 2
  • 18
  • 40
8
votes
2 answers

Resharper warns about a null string (System.NullReferenceException)

Just want to be sure that I haven't been coding for too long ... But, this seems highly unlikely : https://i.stack.imgur.com/mTlwX.png I create the var, check for null, return if it is, so there's no way I can see it as being null at that point…
Noctis
  • 11,507
  • 3
  • 43
  • 82
8
votes
4 answers

NullReferenceException trying to read connection string

I have two projects. One is web & the other is windows forms. Web project connects to database but the windows project throws an exception NullReferenceException reading the connection string. I am using the same classes to connect both projects.…
Neo
  • 81
  • 1
  • 1
  • 4
8
votes
2 answers

Roles.GetRolesForUser throws exception Object reference not set to an instance of an object

I have an ASP.NET application that uses the Roles.GetRolesForUser method. The call work fines within the application, but when i use the same call in a referenced library, it throws an exception. The exception message is: Object reference not set…
jason
  • 91
  • 1
  • 4
8
votes
4 answers

Why ArgumentNullException? Why not System.NullReferenceException?

See line of code below: DataTable [] _tables = null; // Throws System.NullReferenceException _tables.GetType(); // Throws System.ArgumentNullException _tables.Count(); In this lines of code I have _tables reference and trying to access its system…
Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
8
votes
5 answers

Why does setting ComboBox.SelectedValue to null cause a ArgumentNullException?

Why does setting the SelectedValue of a ComboBox to null cause an ArgumentNullException? The Exception only occurs if the ComboBox is actually part of a Form. I can set SelectedValue to all kinds of values or types that don't make sense, but I can't…
comecme
  • 6,086
  • 10
  • 39
  • 67
8
votes
2 answers

Why is ReSharper suggesting a possible NullReferenceException on a dynamic type?

If I write the following code, ReSharper is warning me for a possible NullReferenceException. However I'm explicitly checking for null in the statement above. Is there something about dynamic I do not know about (is it assuming it might be backed…
Rick
  • 3,361
  • 1
  • 22
  • 29
8
votes
3 answers

NullReferenceException in creating Excel worksheet

i want to fill an excel file and so i use ExcelPackage: Office Open XML Format . but i have an error. my code: string fileName = "DBE_BAKIM_FORMU" + ".xlsx"; FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath("~/") + fileName); using…
7
votes
6 answers

Reasons to throw a NullReferenceException explicitly?

I'm cleaning up some legacy code and I have found methods which explicitly throw a NullReferenceException (e.g.: when checking if some properties of a class are null, or checking configuration). As this type of exception is thrown by CLR in case of…
Marcin Seredynski
  • 7,057
  • 3
  • 22
  • 29
7
votes
1 answer

NullReferenceException during C++ callback to C# function

developers! I have very strange problem. My project has DLL writen in C++ and a GUI writen in C#. And I have implemented callback for some interoperability. I planed that C++ dll will call C# code in some circumstances. It works... but not long and…
Anton Semenov
  • 6,227
  • 5
  • 41
  • 69
7
votes
1 answer

EF6 NullReferenceException with any ToList()

Using C#, .NET 4.5.2, Entity Framework 6.1.3 and System.Linq I have encountered a confusing exception. The exception itself does not seem to contain useful information to determined why it is being raised. The following line of code when executed…
7
votes
3 answers

NullReferenceException vs ArgumentNullException

I was reading this post where the answerer mentioned he prefered ArgumentNullException over NullReferenceException. MSDN mention that for NullReferenceException: The exception that is thrown when there is an attempt to dereference a null object…
Chax
  • 1,041
  • 2
  • 14
  • 36
7
votes
5 answers

MySQL with Entity Framework - what am I doing wrong?

I am completely new to Entity Framework and even ADO.NET in general (don't typically do much work with databases). I downloaded and installed MySQL Connector/NET 6.3.5. I created a new C# project in Visual Studio 2010. I added a new ADO.NET Entity…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447