Solved

What is with null reference

Posted on 2014-07-18
3
94 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 63

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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