C# Program to Demonstrate the Nullreferenceexception

You are currently viewing C# Program to Demonstrate the Nullreferenceexception

C# program to demonstrate the NullReferenceException

//C# program to demonstrate the null reference exception.

using System;

class ExceptionDemo
{
    void PrintHello()
    {
        Console.WriteLine("Hello World");
    }
    static void Main()
    {
        try
        {
            ExceptionDemo E = new ExceptionDemo();

            E = null;
            E.PrintHello();
        }
        catch (NullReferenceException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
Object reference not set to an instance of an object.
Press any key to continue . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply