VB.Net program to demonstrate the NullReferenceException

Here, we are going to demonstrate the NullReferenceException in VB.Net.
Submitted by Nidhi, on January 05, 2021

Here, we demonstrate the NullReferenceException. Here, we will call a method by a reference that contains a null value. Then exceptions will be generated by the program.

Program/Source Code:

The source code to demonstrate the NullReferenceException is given below. The given program is compiled and executed successfully.

'Vb.Net program to demonstrate the NullReferenceException.

Module Module1
    Class Sample
        Public Sub SayHello()
            Console.WriteLine("Hello World")
        End Sub
    End Class

    Sub Main()
            Dim S As New Sample()

            S = Nothing
        Catch e As NullReferenceException
        End Try
    End Sub
End Module


Object reference not set to an instance of an object.
Press any key to continue . . .


In the above program, here we created a module Module1. In Module1 we created a class Sample that contains SayHello() method.

The Main() is the entry point for the program, here we created the object of Sample class and then assigned the Nothing in the object and then called the SayHello() method then it will generate null reference exception that will be caught by catch block and print an appropriate message on the console screen.

VB.Net Exception Handling Programs »



Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.