Reputation: 75
using System;
namespace ConsoleApp1{
class Player
{
public string Name { get; set; }
}
class Program
{
public static void PrintName(Player p)
{
System.Console.WriteLine(p.Name);
}
public static void Main(string[] args)
{
Player player = new Player();
PrintName(player);
}
}
}
Hello, I'm trying to solve this question but I found that if I get Name
value before setting its value, I will get null
value, but it's not an exception.
Is the answer to this question NullReferenceException
?
Thanks for you all guys :)
Upvotes: 1
Views: 406
Reputation: 3526
You question clearly asks what type of exception might occur. That way of phrasing implies that there are different ways the method PrintName
might be utilized. And one of the ways is to pass null into p
.
So basically your task is to assume all possible use cases of the method and determine what exceptions could be in each case. And you guessed right — it's NullReferenceException
.
The idea behind exceptions is to show that program is in a state that it does not know how to continue work or that continuation will cause severe issues. So programmers should always check data (in your case — input params) to verify that particular method can work with input, otherwise the calling side should be notified about errors and work should be stopped — by throwing an exception, for example.
Upvotes: 2
Reputation: 82474
So, as other answers have mentioned, there is a potential NullReferenceException
in this method - if the argument passed to it is a reference to null
, that's the exception the run time will throw.
What I want to address is this:
(b) Modify the above method to handle this specific exception
So - how should you modify the method?
Let's start with the wrong solution and work our way up:
The wrong solution would be to wrap the Console.WriteLine
statement in a try...catch
, and catching the NullReferenceException
.
This is wrong because you really, really should avoid catching NullReferenceException
s.
In fact, you should strive to write your code in such a way that a NullReferenceException
will not be a possibility.
Basically, a NullReferenceException
is (probably the most common and easiest to overlook but still) - a boneheaded exception:
public static void PrintName(Player p)
{
try
{
System.Console.WriteLine(p.Name);
}
// Don't do that!
catch(NullReferenceException ex)
{
System.Console.WriteLine("player is null!");
}
}
A better solution (though probably not in this case) would be to validate your input, and throw an ArgumentNullException
if the p
argument is null. (btw, terrible name for an argument, but that's a different story).
This way, you wont get a null reference exception but an ArgumentNullException
- which is a "valid" exception to catch - however, this is useful when writing public APIs, when you don't know in advance who will use your code and how.
Clearly, this is not the case here, and that would also quality as a boneheaded exception (or at least a vexing exception):
public static void PrintName(Player p)
{
// Better, but still a bad option in this case.
if(p is null) throw new ArgumentNullException(nameof(p));
System.Console.WriteLine(p.Name);
}
You could check if the reference is not null and only then call Console.WriteLine
- and that's a slightly better solution (Don't worry, will get there soon) - but then, how will your user know that the player is null? that gives no indication - simply nothing happens if p
is null (Bonus reading: The perfect non-null test):
public static void PrintName(Player p)
{
// This is closer - but how would the user know that p is null?
if(p is object)
{
System.Console.WriteLine(p.Name);
}
}
IMHO, the best course of action here is to use a combination of the null conditional operator (?.) and the null coalescing operator (??):
public static void PrintName(Player p)
{
// This is probably the best solution in this case.
System.Console.WriteLine(p?.Name ?? "player is null!");
}
This way, if p
is null, you'll print out "player is null!"
, so you avoid the possible NullReferenceException
, and your code is still only a single line.
Upvotes: 2
Reputation: 16049
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null.
In your case, if you pass null instead of instance of Player
class, you will get NullReferenceException
exception.
using System;
namespace ConsoleApp1{
class Player
{
public string Name { get; set; }
}
class Program
{
public static void PrintName(Player p)
{
System.Console.WriteLine(p.Name); //This line will throw null exception at run time, because p is null
}
public static void Main(string[] args)
{
PrintName(null); // Possible way to get NullReferenceException
}
}
}
Upvotes: 0