0

我无法修复此空引用异常

    Dim Conn As New OleDb.OleDbConnection
    Dim Trans As OleDb.OleDbTransaction

    Try
        ' create the stream writer object
        Dim FS As New System.IO.StreamReader(FileName)

        ' open DB connection and start a transaction
        Dim Tmp As New POSDSTableAdapters.ItemsTableAdapter
        Conn.ConnectionString = Tmp.Connection.ConnectionString
        Conn.Open()
        Trans = Conn.BeginTransaction

        ' clear all tables
        Dim CMD As New OleDb.OleDbCommand
        CMD.CommandText = " delete from ReciptDetails"
        CMD.Connection = Conn
        CMD.Transaction = Trans
        CMD.ExecuteNonQuery()
        CMD.Dispose()

        CMD = New OleDb.OleDbCommand
        CMD.CommandText = " delete from Recipts"
        CMD.Connection = Conn
        CMD.Transaction = Trans
        CMD.ExecuteNonQuery()
        CMD.Dispose()

        CMD = New OleDb.OleDbCommand
        CMD.CommandText = " delete from items"
        CMD.Connection = Conn
        CMD.Transaction = Trans
        CMD.ExecuteNonQuery()
        CMD.Dispose()

        CMD = New OleDb.OleDbCommand
        CMD.CommandText = " delete from RestorantTable"
        CMD.Connection = Conn
        CMD.Transaction = Trans
        CMD.ExecuteNonQuery()
        CMD.Dispose()

        CMD = New OleDb.OleDbCommand
        CMD.CommandText = " delete from [Values]"
        CMD.Connection = Conn
        CMD.Transaction = Trans
        CMD.ExecuteNonQuery()
        CMD.Dispose()

        ' load the tables
        If Not LoadTableFromBackup(Conn, Trans, FS, "RestorantTable") Then
            Trans.Rollback()
            Conn.Close()
            FS.Close()
            Return False
        End If
        If Not LoadTableFromBackup(Conn, Trans, FS, "Values") Then
            Trans.Rollback()
            Conn.Close()
            FS.Close()
            Return False
        End If
        If Not LoadTableFromBackup(Conn, Trans, FS, "items") Then
            Trans.Rollback()
            Conn.Close()
            FS.Close()
            Return False
        End If
        If Not LoadTableFromBackup(Conn, Trans, FS, "Recipts") Then
            Trans.Rollback()
            Conn.Close()
            FS.Close()
            Return False
        End If
        If Not LoadTableFromBackup(Conn, Trans, FS, "ReciptDetails") Then
            Trans.Rollback()
            Conn.Close()
            FS.Close()
            Return False
        End If

        FS.Close()

        Trans.Commit()

        Conn.Close()
        Return True
    Catch ex As Exception
        Trans.Rollback()
        Conn.Close()
        Return False
    End Try
End Function
4

2 回答 2

2

根据您上面的评论,似乎Trans永远不会被赋值。看一下您的代码的较小示例:

Try
    ' create the stream writer object
    Dim FS As New System.IO.StreamReader(FileName)

    ' open DB connection and start a transaction
    Dim Tmp As New POSDSTableAdapters.ItemsTableAdapter
    Conn.ConnectionString = Tmp.Connection.ConnectionString
    Conn.Open()
    Trans = Conn.BeginTransaction

    ' the rest of the code...
Catch ex As Exception
    Trans.Rollback()
    Conn.Close()
    Return False
End Try

您的Catch块假定Trans并且Conn将具有值。但是,如果在该行之前抛出异常,Trans = Conn.BeginTransactionTrans永远不会被分配一个值,所以它将是nullNothing在 VB 中)。

这里发生的事情是该行之前的某些东西引发了异常。但是你的异常处理程序本身会抛出一个完全不同的异常。这基本上掩盖了第一个异常,这是您要查找的实际错误。

使用调试器,设置断点Trans.Rollback()并查看原始异常是什么,这是您需要更正的内容。

除此之外,您可以稍微重新构造您的Try/Catch块,以不假设变量具有值。只是为了让你成为初学者,至少你可以在你的Catch街区做到这一点:

Catch ex As Exception
    If Trans Is Not Nothing Then
        Trans.Rollback()
    End If
    If Conn Is Not Nothing Then
        Conn.Close()
    End If
    Return False
End Try
于 2013-10-09T14:27:05.077 回答
1

这不是错误。这是一个警告,因为您在分配它之前使用了 trans。在尝试之前移动 Conn.Open() 和 Trans = Conn.BeginTransaction ,您将解决它。

于 2013-11-27T13:32:20.457 回答