Object reference not set to an instance of an object.

Posted on 2005-04-18
Last Modified: 2012-05-05
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)
End With

Question by:CodeManiac
    LVL 1

    Author Comment

    "databaseFunctions" is a class where the SelectCommand function is located.
    LVL 6

    Expert Comment

    Try splitting the declaration :

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

    LVL 12

    Accepted Solution

    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)
    End With


    The other possibility is that there is an error within the SelectCommand method
    LVL 13

    Expert Comment

    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
                Dim daDataAdapter As New SqlDataAdapter
                Dim dsDataSet As New DataSet
                daDataAdapter.SelectCommand = New SqlCommand(sSQL, mobjConn)
                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
    ds = SelectCommand(sSQL)
    LVL 1

    Author Comment

    Thanks that was it, I was calling it like this...
    Dim databaseFunctions As databaseFunctions

    instead of this...
    Dim databaseFunctions As New databaseFunctions

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Convert money to nchar 4 34
    Add row in datatable 18 43
    Help with autosize of dataGridView 4 25
    SequenceEqual in C# List 2 32
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now