Solved

What is with null reference

Posted on 2014-07-18
3
88 Views
Last Modified: 2014-07-23
I have been a vb 6 programmer for years and I'm starting to use vb.net .  For the most part it makes sense, but on several occasions I have run into this error "NullReferenceException was unhandled"   Can someone explain what that is.
0
Comment
Question by:Tom_Hickerson
3 Comments
 
LVL 4

Accepted Solution

by:
Chris Watson earned 500 total points
ID: 40204666
A NullReferenceException is raised when you try to access a property or method on a object which is currently null.

Dim object As Object = Nothing

Dim stringValue As String

stringValue = object.ToString() ' This will cause a NullReferenceException because object is currently Nothing (or null).

Open in new window


To avoid these exceptions, you should check to see whether your object has a value before calling any of its methods.

Dim object As Object = Nothing

Dim stringValue As String

If object Is Nothing Then
    stringValue = "null"
Else
    stringValue = object.ToString()
End If

Open in new window

0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40204705
This exception is thrown when you attempt to access an object or one of its properties or methods without first creating the object. For example lets say you have a List(Of String) and you attempt to add a string to that list as follows.

' The variable myList can hold an object of List(Of String) but the list has not yet been created.
Dim myList As List(Of String)
' Adding a string to the above List will throw an NullReferenceException because the List does not exist yet
myList.Add("This is my String")

' The following is the correct way to do it so that you do not throw the exception. Please note the New keyword
' which will create an instance of the List
Dim myList As New List(Of String)
' Now you can do this
myList.Add("This is my String")
0
 
LVL 40
ID: 40204871
Where VB has the notion of Nothing, most other projects use the word Null (not the same as a database Null).

They kept Nothing in the VB.NET syntax, but because .NET is made to work with almost any existing language, and because they all use the same runtime, most of the information that you will get from the framework about Nothing will show up with Null.

That exception is the equivalent of the "Object variable or With block variable not set" error that you probably know if you have worked in VB6 for many years.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now