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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sujen
    New Member
    • Dec 2009
    • 1

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

    i'm getting this error whenever i run this page.
    My codes:

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim con As String = "Database=barcouncil;Data Source=dev;UserId=root;password=;"
            Dim cn As New MySqlConnection
            Dim da As New MySqlDataAdapter
    
            'da.Fill(ds, "firm_headquarters")
            Try
                cn.Open()
                da.SelectCommand.ExecuteNonQuery()
                da.Fill(ds, "firm_headquarters")
                
    
    
                DT = ds.Tables("firm_headquarters")
                Dim dr As DataRow
                dr = DT.NewRow
    
                dr(2) = TextBox2.Text
                dr(4) = TextBox3.Text
                dr(6) = TextBox4.Text
                dr(8) = TextBox5.Text
                dr(10) = TextBox6.Text
                dr(3) = TextBox7.Text
                dr(11) = LinkButton2.Text
               
                dr.EndEdit()
                Response.Write("Updated")
                DT.Rows.Add(dr)
    
                Dim cb As New MySqlCommandBuilder(da)
                da.Update(ds, "firm_headquarters")
    
            Catch ex As Exception
                Response.Write(ex.ToString)
            End Try
    
            
        End Sub
    can you guys help me here?
    tq
  • MrMancunian
    Recognized Expert Contributor
    • Jul 2008
    • 569

    #2
    On what line did you get this error? In general, this kind of error occurs when you didn't declare the object you're handling as New.

    Steven

    Comment

    • kiseitai2
      New Member
      • Jul 2007
      • 93

      #3
      For one thing, I see you are using a variable named ds in da.Fill(ds, "firm_headquart ers"), but I don't see it anywhere before it. Did you meant to use cn instead of ds, or is ds a global variable in another file in your project?

      Also, I see that you declared the function cn but I don't see you using the con string. Are you sure you are not asked for parameters by cn when you used the new keyword like Dim cn As New MySqlConnection = something, or Dim cn As New MySqlConnection (something)? This is something I have had trouble with in the past when I have declared objects and did not know they had parameters that had to be especified.
      Last edited by kiseitai2; Dec 6 '09, 02:43 AM. Reason: Remembered something extra to add in my answer after I posted it

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        A NullReferenceEx ception occurs when you try to use something that hasn't been instantiated or doesn't exist (is null).

        For example you would get a NullReferenceEx ception if you did something like:
        Code:
        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 NullReferenceEx ception because these properties/methods are not available since myPerson is pointing to Null/Nothing/No-memory.

        To fix the NullReferenceEx ception, 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:
        Code:
        Dim myPerson As New Person
        So will:
        Code:
        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:
        Code:
        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

        Comment

        Working...