A NullReferenceException occurs when you try to use something that hasn't been instantiated or doesn't exist (is null).
For example you would get a NullReferenceException if you did something like:
-
Dim myPerson As Person
-
myPerson .FirstName="Frinny"
-
The reason is because myPerson has not been instantiated as a New Person. This means that memory for myPerson has not been allocated and that myPerson points to "Null"/"Nothing" (no memory location).
So if you try access any of myPerson's properties or methods you'll get a NullReferenceException because these properties/methods are not available since myPerson is pointing to Null/Nothing/No-memory.
To fix the NullReferenceException, all I have to do is use the keyword 'new'. This will instantiate the object. Instantiating the object will allocate memory (and execute the constructor) for the object so that you can use it:
For example, the following will instantiate a Person Object:
-
Dim myPerson As New Person
-
So will:
-
Dim myPerson As Person
-
myPerson = New Person
-
Sometimes you will call methods that should return you an instance of an Object.
For example, say there was a method called "RetrievePerson()" that should return a person object, or if it couldn't it should return nothing. Before you attempt to use any properties or methods for any object returned by a method you should always check to make sure that it exists first. To do this you just check if the object is "nothing".
For example:
-
Dim myPerson As Person = RetrievePerson()
-
If myPerson is Nothing Then
-
myPerson = new Person 'This line allocates memory for the Person Object
-
End If
-
myPerson.FirstName = "Frinny"
-
....
-
In your case you never checked to see if "ds" exists.
You also never checked to see if "DT" exists.....
You are going to get this error as soon as you try to use an object that has not been instantiated.
-Frinny