NullReferenceException Object reference not set to instance of an object - cSharp Coder

Latest

cSharp Coder

Sharp Future

Wednesday, May 19, 2021

NullReferenceException Object reference not set to instance of an object

 In the very simple example below, the string “foo” is set to null.

string foo = null;
foo.ToUpper();

  To fix/avoid this error:

  • Build null checking into the code and set default values.
  • Use Debug.Assert to catch the problem before the exception occurs.
string foo = null;
if(foo!=null) {foo.ToUpper();}


No comments:

Post a Comment