Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Object reference not set to an instance of an object.


VB
Capture — imgbb.com
[^]

What I have tried:

VB
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


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


VB
Private bOWCheck() As Boolean


VB
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


VB
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
Posted
Updated 11-Sep-18 6:03am
Comments
Patrice T 10-Sep-18 18:55pm    
If you look carefully, you will see that the error message also tell you where is the error, its position.
Jayanta Modak 10-Sep-18 23:01pm    
Sir I see the error massage but I don't understand the what is the error. Please help me to solve the problem. Thanks
Dave Kreskowiak 10-Sep-18 19:45pm    
On which line does the exception occur?

You also have another problem. Every time you click btnAdd, you're creating two new Font objects. DO NOT DO THIS! Your app is leaking resources.

Create the Font objects at the class level so you can reuse them anywhere in your code. When your app quits, call Dispose on those objects to free up the resources they are holding.
Jayanta Modak 11-Sep-18 1:18am    
https://ibb.co/cNRFzp in this line

i do not use any button my project, when i add any part of codding then i fist checking it using a button (btnadd) after that it convert a private sub and call from where is necessary.

I try many type of coding listview1.checkbox=true but it is does not work properly, not work changing , click, mouse up / down etc. because when i click on the check the TICK sing on and off but never select full row, for that listview1 changing , click, mouse up / down do not work, So, I use this method

please forgive me sir i don't know good English please forgive. and please help me to solve this problem
Thanks
Dave Kreskowiak 11-Sep-18 15:51pm    
When the code breaks, hover the mouse over .Row, then collLoc, then bOWCheck. One of those is going to be null, or Nothing in VB.NET. You first figure out what's null, then go backwards through the code to figure out why it's null.

You cannot call methods and properties on an object that doesn't exist.

The object is null. That is what the error is telling you. Now, you need to figure out why and then fix it or handle it.
 
Share this answer
 
Private bOWCheck() As Boolean

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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900