Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What is with null reference

Posted on 2014-07-18
3
Medium Priority
?
96 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 5

Accepted Solution

by:
Chris Watson earned 2000 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 64

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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 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