Questions tagged [argumentnullexception]

[MSDN] The exception that is thrown when a null reference is passed to a method that does not accept it as a valid argument.

MSDN ArgumentNullException is thrown when a method is invoked and at least one of the passed arguments is null but should never be null.

ArgumentNullException behaves identically to ArgumentException. It is provided so that application code can differentiate between exceptions caused by null arguments and exceptions caused by arguments that are not null. For errors caused by arguments that are not null, see ArgumentOutOfRangeException.

ArgumentNullException uses the HRESULT E_POINTER, which has the value 0x80004003.

For a list of initial property values for an instance of ArgumentNullException, see the ArgumentNullException constructors.

166 questions
120
votes
3 answers

Why does this string extension method not throw an exception?

I've got a C# string extension method that should return an IEnumerable of all the indexes of a substring within a string. It works perfectly for its intended purpose and the expected results are returned (as proven by one of my tests, although…
ArtOfCode
  • 5,702
  • 5
  • 37
  • 56
59
votes
1 answer

Why do I get an exception when passing "null" constant but not when passing a "null" string reference?

If I run this code: Console.WriteLine( String.Format( "{0}", null ) ); I get a ArgumentNullException but if I run this code: String str = null; Console.WriteLine( String.Format( "{0}", str ) ); it runs just fine and the output is an empty…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
48
votes
5 answers

How to initialize IEnumerable that be empty and allow to Concat to it?
I tried this code for adding b to books: IEnumerable books =null; foreach (Book b in context.Books.AsEnumerable()) if (someConditions) books = books.Concat(new[] {b}); but gives me this error on last line of…
Majid
  • 13,853
  • 15
  • 77
  • 113
21
votes
16 answers

Is there any reason to throw a DivideByZeroException?

Are there any cases when it's a good idea to throw errors that can be avoided? I'm thinking specifically of the DivideByZeroException and ArgumentNullException For example: double numerator = 10; double denominator = getDenominator(); if(…
Armstrongest
  • 15,181
  • 13
  • 67
  • 106
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
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
3 answers

How do I Unit Test Actions without Mocking that use UpdateModel?

I have been working my way through Scott Guthrie's excellent post on ASP.NET MVC Beta 1. In it he shows the improvements made to the UpdateModel method and how they improve unit testing. I have recreated a similar project however anytime I run a…
8
votes
2 answers

ArgumentNullException message without parameter name

I am throwing ArgumentNullException in part of my code in C#. I want to catch and display the error message of my exception. But without the parameter name. I want to pass the parameter name to the exception constructor. throw new…
filipv
  • 314
  • 1
  • 3
  • 15
8
votes
2 answers

ArgumentNullException on changing frame

So I'm trying to change frames in a windows 8 app. I tried following the tutorial at this page, but I keep getting the same error. I'm getting an ArgumentNullException on the line: frameState[_pageKey] = pageState; in the LayoutAwarePage.cs…
Loyalar
  • 2,131
  • 4
  • 24
  • 44
6
votes
3 answers

ToList() - ArgumentNullException Handling

Hi I have a class named Activity, On a form I create its object array as, Activity[] _actList; And then do this, List termsList = _actList.ToList(); since _actiList is null its throwing the ArgumentNullException. So…
Sunitha
  • 241
  • 1
  • 5
  • 17
6
votes
5 answers

Design by contract/C# 4.0/avoiding ArgumentNullException

I'm terribly tired of checking all my arguments for null, and throwing ArgumenutNullExceptions when they are. As I understand it, C# 4.0 enables some design by contract constructs. Will it be possible to specify that a method will not accept null…
core
  • 32,451
  • 45
  • 138
  • 193
6
votes
2 answers

ArgumentNullException for nested members

Let's say I have a method: public void SayHello(User user) { if (user == null) throw new ArgumentNullException(nameof(user)); Console.Write(string.Format("Hello from {0}", user.Name)); } It's clear that I should use…
Andrei
  • 42,814
  • 35
  • 154
  • 218
4
votes
7 answers

Create class instance from string

I have a C# method which creates a new instance of a class from a string, however, I get an error when running the code. obj = (ClassX)Activator.CreateInstance(Type.GetType("classPrefix_" + className)); ArgumentNullException was unhandled Value…
user142162
4
votes
3 answers

string in C# and String^ in C++/CLI... How to pass "null"?

I got some C++ library. And also I got C++/CLI wrapper for it, to make it possible call methods from this library in C# code. Lets say I would like to use some call in C# like this: string date = MyWrapper.GetValue("SystemSettings", "BuildDate",…
user2706838
  • 1,061
  • 2
  • 13
  • 21
3
votes
3 answers

Navigate to new view on button click in ASP.NET Core 3.1 MVC

I am trying to navigate the user to a new page with a button click. I am having issues with rendering the new view. Any time I do click on the button, I either get an error for a dropdown on my page, or I get the home page view but with my desired…
1
2 3
11 12