Solved

What is with null reference

Posted on 2014-07-18
3
92 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
C#.NET MSVS 2010 is there some things not allowed in Background Worker DoWork? 27 43
Help with error in Query 2 39
Please explain purpose of GZIP 4 56
Why use this lambda? 12 60
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

761 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