对象引用未设置为对象的实例无行VB [英] Object reference not set to an instance of an object No line VB

查看:91
本文介绍了对象引用未设置为对象的实例无行VB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道是否有人可以提供帮助。我不断得到一个对象引用未设置为对象的实例。对于我的生活,我无法找到这种情况发生的地方。我没有得到任何行号或错误/警告。



当在选中的列表框中勾选时,代码基本上将数据库中的记录更新为完成。从我所看到的,它确实有效,但只要我试图解开盒子并重新绑定它,就会发生这种情况。关闭表单并重新打开表明它工作正常。



Hi,
I wondered if someone could help. I keep getting an "Object reference not set to an instance of an object." For the life of me I cannot find where this is happening. I am not getting any line numbers or errors/warnings.

The code basically updates a record in a database as complete, when ticked in the checked list box. From what I can see, it actually works, but as soon as I try to "unbind" the box and re-bind it, this happens. Closing the form and re-opening shows it works fine.

Public Class Form1
    Dim tDataSet As New DataSet




    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            getData()
        Catch ex As Exception
            MsgBox(Err.Description)
        End Try


    End Sub


    Private Sub getData()
        Try

            tDataSet.Clear()

            System.Threading.Thread.Sleep(3000)

            connectDB()

            'no login screen - so just set login iD manually
            loginID = 1

            Dim sqlLine As String = ""

            sqlLine = "SELECT tblTasks.TaskID, tblTasks.UserID, tblTasks.Task, tblTasks.CompleteState FROM(tblTasks) WHERE (((tblTasks.UserID)=" & loginID & ") AND((tblTasks.CompleteState)=False));"


            Dim dbAdapter As New OleDb.OleDbDataAdapter(sqlLine, DBConnection)

            dbAdapter.Fill(tDataSet, "tblTasks")


            DBConnection.Close()

'chkTasks is a checkedlistbox

            With chkTasks
                .DataSource = tDataSet.Tables("tblTasks")
                .DisplayMember = "Task"
                .ValueMember = "TaskID"
            End With

        Catch ex As Exception
            MsgBox(Err.Description)
        End Try
        

    End Sub

    Private Sub chkTasks_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles chkTasks.ItemCheck
        Try
            Dim selectedTaskID As Integer = chkTasks.SelectedValue


            UpdateTask(selectedTaskID)

            chkTasks.DataSource = Nothing
            chkTasks.Items.Clear()
            chkTasks.Refresh()
            chkTasks = New CheckedListBox



            getData()



        Catch ex As Exception

            MsgBox(Err.Description)

        End Try

    End Sub


    Private Sub UpdateTask(ByVal selectedTaskID As Integer)

        Try
            Dim sqlLine As String = "UPDATE tblTasks SET tblTasks.CompleteState = True WHERE tblTasks.TaskID =" & selectedTaskID & ";"

            connectDB()

            DBCommand = New OleDb.OleDbCommand(sqlLine, DBConnection)

            DBCommand.ExecuteNonQuery()

            DBConnection.Close()

        Catch ex As Exception
            MsgBox(Err.Description)

        End Try
    End Sub


End Class

推荐答案

我们无法帮助:你需要使用调试器找出问题的确切位置。

在每个MsgBox函数上放置一个断点,并等待它被击中。当调试器遇到断点时,它将停止并让您看到它发生了什么。从您的Exception对象ex开始,仔细查看它的各种属性 - 它将解释导致问题的行(不在Description属性中)。



然后查看它引用的行,并再次使用调试器找出哪个变量为null(或VB中为Nothing)。如果你有,你可以开始解决它为空的原因。但我们不能为您做到这一点 - 我们无法访问您正在运行的代码或数据。
We can't help: you need to use the debugger to find out exactly where the problem is.
Put a breakpoint on each of the MsgBox functions, and wait until it is hit. When the debugger meets a breakpoint it will stop and let you see what it happening. Start with your Exception object "ex" and look closely at it's various properties - it will explain which line is causing the problem (that isn't in the Description property).

Then look at the line it refers to, and use the debugger again to find out which variable is null (or Nothing in VB). When you have that, you can start working out why it is null. But we can't do that for you - we have no access to your running code or data.


我似乎无法看到出现的错误。 VB根本不提供任何行信息。





[ ^ ]
I cannot seem to see the error that comes up. VB doesn't give any line information at all.


[^]


自行排序循环每个项目并检查状态。



无论如何,谢谢。
Sorted myself looping through each item and checking state.

Thanks anyway.


这篇关于对象引用未设置为对象的实例无行VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆