• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Object reference not set to an instance of an object.

I get..
System.NullReferenceException: Object reference not set to an instance of an object.

on this line...
ds = databaseFunctions.SelectCommand("SELECT * FROM tUsers")**

when I run this code...
Dim ds As Data.DataSet = New Data.DataSet
ds = databaseFunctions.SelectCommand("SELECT * FROM tUsers")
With ds.Tables(0)
  For i = 0 To .Rows.Count - 1
    Response.Write("test" & i)
  Next
End With

0
CodeManiac
Asked:
CodeManiac
1 Solution
 
CodeManiacAuthor Commented:
"databaseFunctions" is a class where the SelectCommand function is located.
0
 
ctm5Commented:
Try splitting the declaration :

Dim ds as Data.DataSet
ds = New Data.DataSet

ctm5
0
 
S-TwilleyCommented:
I don't know alot about your "databaseFunctions" class but if that's the name of the class rather than a reference to instance of the class, create an instance:

Dim myDBFunctions As new databaseFunctions
Dim ds As Data.DataSet = New Data.DataSet
ds = myDBFunctions.SelectCommand("SELECT * FROM tUsers")

With ds.Tables(0)
  For i = 0 To .Rows.Count - 1
    Response.Write("test" & i)
  Next
End With

========================

The other possibility is that there is an error within the SelectCommand method
0
 
TorrwinCommented:
Usually that error means that a variable has not been properly initialized.  It isn't always true, but may want to double check.

Here's what I use:

Public Function SelectCommand(ByVal sSQL As String) As DataSet
        Try
            Dim daDataAdapter As New SqlDataAdapter
            Dim dsDataSet As New DataSet
            daDataAdapter.SelectCommand = New SqlCommand(sSQL, mobjConn)
            daDataAdapter.Fill(dsDataSet)
            Return dsDataSet
        Catch err As Exception
            MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

And here's how I call it:

Dim ds as New Dataset
Dim sSQL as String
sSQL = "SELECT * FROM AUTHORS"
ds = SelectCommand(sSQL)
0
 
CodeManiacAuthor Commented:
Thanks that was it, I was calling it like this...
Dim databaseFunctions As databaseFunctions

instead of this...
Dim databaseFunctions As New databaseFunctions
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now