locked
VB.Net MySQL Null Exception Error RRS feed

  • Question

  • I am building a VB.net application using VB Express 2008 which connects to a MySQL database.  Right now, I am only selecting records, but will be writing code for update / insert.  I have installed the MySQL Net Connector, most recent stable build.  I have set a reference to it in project references.  It makes a connection and reads just fine, no issues there at all.

    My problem is this:  When exiting the project (via calling "End") I am receiving a null Exception error.  Here is the error:

    System.NullReferenceException was unhandled
     Message="Object reference not set to an instance of an object."
     Source="MySql.Data"
     StackTrace:
        at MySql.Data.MySqlClient.NativeDriver.FetchDataRow(Int32 statementId, Int32 columns)
        at MySql.Data.MySqlClient.Driver.FetchDataRow(Int32 statementId, Int32 columns)
        at MySql.Data.MySqlClient.Driver.SkipDataRow()
        at MySql.Data.MySqlClient.ResultSet.Close()
        at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
        at MySql.Data.MySqlClient.MySqlDataReader.Close()
        at MySql.Data.MySqlClient.MySqlConnection.Close()
        at MySql.Data.MySqlClient.MySqlConnection.Dispose(Boolean disposing)
        at System.ComponentModel.Component.Finalize()
     InnerException: 
    

    Again, this only seems to be happening when the "End" method is called for closing the project.  Here is the code of the main form:

    Option Explicit On
    Option Strict On
    
    Imports MySql.Data.MySqlClient
    
    'Please install the MySQL Connector for .net v 6.2.3
    'Add a service reference to the MySQL Data Client
    
    '=CHANGE LOG==========================================================================================
    '
    '    OBJECT     |  DATE   |  USER    |    REASON
    '-----------------------------------------------------------------------------------------------------
    '  frmMain.vb     | 6/3/2010  | Anderson   | Initial Creation
    '-----------------------------------------------------------------------------------------------------
    
    Public Class frmMain
    
      Public intConState As Integer
    
      Private Sub btnBegin_Click(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles btnBegin.Click
        Dim startSO_1form As New frmSO_1_Start
    
        startSO_1form.Show()
    
        startSO_1form.txtUser.Text = Me.txtUser.Text
        'Push username forward to next form
    
    
      End Sub
    
      Private Sub btnQuit_Click(ByVal sender As System.Object, _
               ByVal e As System.EventArgs) Handles btnQuit.Click
    
        Try
    
          If intConState = 1 Then   'Check to see if connection is open or not
            conn.Close()      'If cconnection is open, close it
          End If
    
          Me.Close()         'Close the form
          End             'Remove executable from memory
    
        Catch ex As Exception
    
          MsgBox("" & ex.Message & "")
    
    
        End Try
    
    
    
      End Sub
    
      Private Sub frmMain_Load(ByVal sender As System.Object, _
                   ByVal e As System.EventArgs) Handles MyBase.Load
    
      End Sub
    End Class

    The null exception seems to be occuring in the btnQuit_Click Routine.  The MySQL connections are called and used from the form that calls frmMain.vb (above).  They are properly closed and disposed of before frmMain.vb is ever called.  The Try-Catch method didn't cut it.  It moves through that without breaking.

    Any ideas are greatly appreciated.  Let me know if anyone needs any more code / information.  Happy to provide.

    Monday, June 7, 2010 3:25 PM

Answers

  • Normally you should not use the End statement.

    End means "End right now". It does *not* mean "Finish processing the events and then end".

    Just closing your main form should cause the application to continue processing its events and then cleanly terminate.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by Cor Ligthert Monday, June 7, 2010 3:42 PM
    • Marked as answer by Alex Liang Monday, June 14, 2010 10:23 AM
    Monday, June 7, 2010 3:41 PM
  • Dispose from the Command and the Adapters in System.Net is a kind of NOP method (it does a slight little bit more), those classes hold no unmanaged resources.

    The only one which has slight sence in AdoNet is the connection.dispose (removes the object string from the object), but that it called direct by the close.

    If you don't know where or when to dispose, then use the "using". Then at least your code is not trashed with all kind of not needed disposes, the using use then the what I describe as nops, so nothing is done.

    There is for some a very slight speed advantage in the releasing of objects when you use dispose, that method has than not to be done anymore as the garbage collector is running (it also does it direct, and goes not to the wait fase).

    However, in some non Net development team created namespaces (so you have the chance also your MYSQL one), is the dispose also (mis) used to do more than only removing unmanaged resources, therefore the Using is in my idea the best way to overcome that for those namespaces.

    When you use "using" on a class which has not IDisposeble implemented (in fact inherits from components) then you are warned.

    The GC is optimized in Framework 4 so I don't know if in that the dispose makes still the same sense for that.

     

     

     

     


    Success
    Cor
    • Marked as answer by Alex Liang Monday, June 14, 2010 10:23 AM
    Monday, June 7, 2010 5:38 PM
  • If you use the Using statement, you won't have to worry about the close/dispose:

      ' Open the connection 
      Using cnn As New SqlConnection( 
          "Data Source=.\sqlexpress;Initial Catalog=AcmeRentals; 
                      Integrated Security=True") 
        cnn.Open() 
    
        ' Define the command 
        Using cmd As New SqlCommand 
          cmd.Connection = cnn 
          cmd.CommandType = CommandType.Text 
          cmd.CommandText = sqlStatement
    
          ' Handle the parameters 
          If arrParam IsNot Nothing Then 
            For Each param As SqlParameter In arrParam 
              cmd.Parameters.Add(param) 
            Next 
          End If 
    
          ' Define the data adapter and fill the dataset 
          Using da As New SqlDataAdapter(cmd) 
            dt = New DataTable 
            da.Fill(dt) 
    
          End Using 
        End Using 

    This uses a SQLServer database, but you can easily change it to use MySql instead.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Alex Liang Monday, June 14, 2010 10:23 AM
    Monday, June 7, 2010 5:44 PM

All replies

  • Normally you should not use the End statement.

    End means "End right now". It does *not* mean "Finish processing the events and then end".

    Just closing your main form should cause the application to continue processing its events and then cleanly terminate.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by Cor Ligthert Monday, June 7, 2010 3:42 PM
    • Marked as answer by Alex Liang Monday, June 14, 2010 10:23 AM
    Monday, June 7, 2010 3:41 PM
  • Here is a thread with further discussion on this topic:

    http://www.developmentnow.com/g/38_2006_5_0_0_765150/Me-close-vs-End.htm

    And here is the documentation:

    http://msdn.microsoft.com/en-us/library/0wt87xba(v=VS.100).aspx

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Monday, June 7, 2010 3:45 PM
  • Thanks, Deborah.

    I will change from using End to using the Close method.  Old VB6 programmer so habits are hard to break.

    I did resolve the Null Exception error just now.  It appeared that after populating a combo box I did not Close / Dispose of the MySQL objects.  Once I added the following code at the end of that particular sub, it resolved the issue:

    myData.Close()             'Close the reader
        myCommand.Dispose()           'Remove bindings from memory
        myAdapter.Dispose()           'Remove bindings from memory
    Seems redundant to do this, but if it makes the front end functional, I'll simply repeat for every connection I make to the db.
    Monday, June 7, 2010 3:55 PM
  • Dispose from the Command and the Adapters in System.Net is a kind of NOP method (it does a slight little bit more), those classes hold no unmanaged resources.

    The only one which has slight sence in AdoNet is the connection.dispose (removes the object string from the object), but that it called direct by the close.

    If you don't know where or when to dispose, then use the "using". Then at least your code is not trashed with all kind of not needed disposes, the using use then the what I describe as nops, so nothing is done.

    There is for some a very slight speed advantage in the releasing of objects when you use dispose, that method has than not to be done anymore as the garbage collector is running (it also does it direct, and goes not to the wait fase).

    However, in some non Net development team created namespaces (so you have the chance also your MYSQL one), is the dispose also (mis) used to do more than only removing unmanaged resources, therefore the Using is in my idea the best way to overcome that for those namespaces.

    When you use "using" on a class which has not IDisposeble implemented (in fact inherits from components) then you are warned.

    The GC is optimized in Framework 4 so I don't know if in that the dispose makes still the same sense for that.

     

     

     

     


    Success
    Cor
    • Marked as answer by Alex Liang Monday, June 14, 2010 10:23 AM
    Monday, June 7, 2010 5:38 PM
  • If you use the Using statement, you won't have to worry about the close/dispose:

      ' Open the connection 
      Using cnn As New SqlConnection( 
          "Data Source=.\sqlexpress;Initial Catalog=AcmeRentals; 
                      Integrated Security=True") 
        cnn.Open() 
    
        ' Define the command 
        Using cmd As New SqlCommand 
          cmd.Connection = cnn 
          cmd.CommandType = CommandType.Text 
          cmd.CommandText = sqlStatement
    
          ' Handle the parameters 
          If arrParam IsNot Nothing Then 
            For Each param As SqlParameter In arrParam 
              cmd.Parameters.Add(param) 
            Next 
          End If 
    
          ' Define the data adapter and fill the dataset 
          Using da As New SqlDataAdapter(cmd) 
            dt = New DataTable 
            da.Fill(dt) 
    
          End Using 
        End Using 

    This uses a SQLServer database, but you can easily change it to use MySql instead.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Alex Liang Monday, June 14, 2010 10:23 AM
    Monday, June 7, 2010 5:44 PM