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

问题描述

Object reference not set to an instance of an object.







Capture — imgbb.com

[]



我的尝试:



[^]

What I have tried:

Private Sub btnaddcat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddcat.Click
        Dim myCheckFont As New System.Drawing.Font("Wingdings", 12, FontStyle.Regular)
        Dim myRegFont As New System.Drawing.Font("Arial", 12, FontStyle.Regular)

        With objPurID_PR
            ListView2.Items.Clear()
            .purinvid = id
            dt = .GetPurInvItemForPR()
            For i As Integer = 0 To dt.Rows.Count - 1
                Dim dr As DataRow = dt.Rows(i)

                Dim listitem As New ListViewItem(dr("itemnm").ToString())
                listitem.UseItemStyleForSubItems = False
                listitem.SubItems.Add(Chr(168))
                listitem.SubItems.Item(1).Font = myCheckFont


                listitem.SubItems.Add(dr("PDQty").ToString())

                listitem.SubItems.Add(dr("itemnm").ToString())

                listitem.SubItems.Add(dr("itemnm").ToString())

                ListView2.Items.Add(listitem)
            Next
        End With
    End Sub







Private Structure MyCell
        Dim Row As Integer
        Dim Col As Integer
    End Structure







Private bOWCheck() As Boolean







Private Sub ListView2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView2.MouseUp
        Dim cellLoc As MyCell
        cellLoc = WhichCell(ListView2, e.X, e.Y)
        Label4.Text = cellLoc.Row.ToString & "  " & cellLoc.Col.ToString
        If cellLoc.Col = 2 Then
            If bOWCheck(cellLoc.Row) = True Then

                Label5.BackColor = Color.AliceBlue
                ListView2.Items(cellLoc.Row).SubItems(cellLoc.Col - 1).Text = Chr(168)
                bOWCheck(cellLoc.Row) = False
            Else
                Label5.BackColor = Color.Coral
                ListView2.Items(cellLoc.Row).SubItems(cellLoc.Col - 1).Text = Chr(258)
                bOWCheck(cellLoc.Row) = True
            End If

        End If
    End Sub







Private Function WhichCell(ByVal lvw As ListView, ByVal X As Integer, ByVal Y As Integer) As MyCell

        Dim colstart As Integer = 0
        Dim colend As Integer = 0
        Dim xCol As Integer

        For xCol = 0 To (ListView2.Columns.Count - 1)
            colend = colend + ListView2.Columns(xCol).Width
            If colstart <= X And X <= colend Then
                WhichCell.Col = xCol + 1
                Exit For
            End If
            colstart = colstart + ListView2.Columns(xCol).Width
        Next

        WhichCell.Row = ListView2.FocusedItem.Index
        Return WhichCell

    End Function

推荐答案


Private bOWCheck() As Boolean



你已经声明了一个布尔值的数组,但是你所展示的代码中没有任何地方你实际上已经初始化它。因此,它的值是 Nothing ,并且尝试读取或写入它将产生 NullReferenceException



[]



注意:你可以通过简单的调试代码!


You've declared an array of Boolean values, but nowhere in the code you've shown have you actually initialized it. Therefore, its value is Nothing, and attempting to read from or write to it will produce a NullReferenceException.

Arrays in Visual Basic | Microsoft Docs[^]

NB: You could have found this out for yourself much faster by simply debugging your code!


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

11-02 11:55