This is a C# Program to illustrate NullRefernce exception.
This C# Program Illustrates NullRefernce Exception.
Here it indicates that you are trying to access member fields, or function types, on an object reference that points to null.
Here is source code of the C# Program to Illustrate NullRefernce Exception. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Illustrate NullRefernce Exception */ using System; class Program { static void Main() { try { string value = null; if (value.Length == 0) { Console.WriteLine(value); } } catch(NullReferenceException e) { Console.WriteLine(e.Message); } Console.Read(); } }
This C# program is used to illustrate NullReference exception. Initialize an value of ‘value’ variable as null. If condition statement is used to check the length of the value of ‘value’ variable is equal to 0, if the condition is true then execute the statement.
Indicates that trying to access member fields, or function types, on an object reference that point to null. Using try and catch, an error message is displayed when the error occurs.
Object Reference not set to an Instance of an Object
Sanfoundry Global Education & Learning Series – 1000 C# Programs.
- Get Free Certificate of Merit in C# Programming
- Participate in C# Programming Certification Contest
- Become a Top Ranker in C# Programming
- Take C# Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice MCA MCQs
- Practice Computer Science MCQs
- Buy MCA Books
- Apply for Computer Science Internship
- Apply for C# Internship