Solved

What is with null reference

Posted on 2014-07-18
3
93 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 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