P. nganga P. nganga - 1 month ago 4
Vb.net Question

How do I get rid of the error system.null reference exception object reference not set to an instance of an object

I get an error when i read data in a Null or empty cell in a datagridview to enable me send the data to a fiscal printer

Here is the part of the code

ke.PrintRecMessage("--------------------------------------------")

ke.PrintRecMessage("ITEM QTY PRICE AMOUNT")

ke.PrintRecMessage("--------------------------------------------")


Dim max1 As Integer = Qualitix_POS.Form1.DataGridView1.Rows.Count - 1

For Each row As DataGridViewRow In Qualitix_POS.Form1.DataGridView1.Rows


ke.PrintRecItem(Description:=row.Cells("Column2").Value,
Price:=row.Cells("Column4").Value,
Quantity:=row.Cells("Column3").Value.ToString.PadRight(4, "0"),
VatInfo:=row.Cells("Column6").Value, UnitPrice:=row.Cells("Column4").Value,
UnitName:=row.Cells("Column1").Value)

Next

Answer

Add a check in the loop

if row <> nothing then
...
end if

If that doesnt help, we have some wrappers in our code that return empty string if the response is null

wrapString(myvar.getName())

Will return the name or "" same for wrapInt etc