0

I'm having issues with my code and the objective is that I want it to send to a DataGridView a "product" when you click on the image of said "product" but it gives me this error:

    Private Sub PictureBox5_Click(sender As Object, e As EventArgs) Handles PictureBox5.Click
        s1 = s1 + 1
        If s1 = 0 Then
            s1 = 1
            Compras.DataGridView3.Rows.Add("1", " Paracetamol")
        ElseIf s1 = 1 Then
            s1 = 0
            For Each fila As DataGridViewRow In Compras.DataGridView3.Rows
                If fila.Cells(1).Value = " Paracetamol" Then
                    Compras.DataGridView3.Rows.Remove(fila)
                End If
            Next


        End If

Compras - Is the form where its located fila - means line in Spanish (need this because my teacher only speaks Spanish) How do I fix this or is there a way to create a checkout easier? Thanks for the help!

I tried maybe deleting the "filas" part and just putting "compras" but nope also read the article that Microsoft has but not much of a help (sorry for having a smooth brain)

The error pop up is

System.NullReferenceException: 'Object reference not set to an instance of an object.' FarRanita.Compras.DataGridView3.get returned Nothing.

braX
  • 11,506
  • 5
  • 20
  • 33
  • Assuming you're working on winforms here. The question is how you created DataGridView3? The error would suggest that DataGridView3 doesn't exist, but if that was the case, I would expect you to be getting compile errors. Is Compras the same form you're triggering this event from? If not, I would suspect the issue is around how you're creating a reference to that form instance – Hursey Nov 07 '22 at 00:58
  • It's not clear which line the error occurs on. Add code to check if `DataGridView3` is Nothing. If so, output an error message: `System.Diagnostics.Debug.WriteLine("Error DataGridView3 is Nothing")`. The issue may be your use of `For Each`. Try using `For` instead. – Tu deschizi eu inchid Nov 07 '22 at 03:23

0 Answers0