none
System.NullReferenceException help!!

    Question

  • Hey all,

    I am having an exception thrown which tells me basically it is returning a nothing value. I know this is not a blank cell but it instead contains for example "W/C 58". Code Below

            For i As Integer = 2 To counta
                If xlWorkSheet2.Cells(i, 21).value.ToString = Form3.Label1.Text Then
                    For y As Integer = 1 To 33
                        temp2 = xlWorkSheet2.Cells(i, y).value.ToString
                        temp1.Add(temp2)
                        TextBox2.Text = xlWorkSheet2.Cells(i, y).value.ToString
                    Next
                    Form3.DataGridView1.Rows.Add(temp1)
                    ProgressBar1.Value = ProgressBar1.Value + 1
                Else
                    ProgressBar1.Value = ProgressBar1.Value + 1
                End If
            Next


    • Edited by k7s41gx Friday, January 11, 2019 2:06 PM update
    Friday, January 11, 2019 12:35 PM

Answers

  • Hey again,

    I have fixed the "w/c 68" showing as error from cell by using raw data instead of an array. No idea why that wasn't working, but this adds the empty cells and all. Is there a way to process an xlsx faster? I have 311k rows and this is quite slow.

    Thanks,

            For i As Integer = 2 To counta
                If xlWorkSheet2.Cells(i, 21).value.ToString = Form3.Label1.Text Then
                    t1 = CStr(xlWorkSheet2.Cells(i, 1).value)
                    t2 = CStr(xlWorkSheet2.Cells(i, 2).value)
                    t3 = CStr(xlWorkSheet2.Cells(i, 3).value)
                    t4 = CStr(xlWorkSheet2.Cells(i, 4).value)
                    t5 = CStr(xlWorkSheet2.Cells(i, 5).value)
                    t6 = CStr(xlWorkSheet2.Cells(i, 6).value)
                    t7 = CStr(xlWorkSheet2.Cells(i, 7).value)
                    t8 = CStr(xlWorkSheet2.Cells(i, 8).value)
                    t9 = CStr(xlWorkSheet2.Cells(i, 9).value)
                    t10 = CStr(xlWorkSheet2.Cells(i, 10).value)
                    t11 = CStr(xlWorkSheet2.Cells(i, 11).value)
                    t12 = CStr(xlWorkSheet2.Cells(i, 12).value)
                    t13 = CStr(xlWorkSheet2.Cells(i, 13).value)
                    t14 = CStr(xlWorkSheet2.Cells(i, 14).value)
                    t15 = CStr(xlWorkSheet2.Cells(i, 15).value)
                    t16 = CStr(xlWorkSheet2.Cells(i, 16).value)
                    t17 = CStr(xlWorkSheet2.Cells(i, 17).value)
                    t18 = CStr(xlWorkSheet2.Cells(i, 18).value)
                    t19 = CStr(xlWorkSheet2.Cells(i, 19).value)
                    t20 = CStr(xlWorkSheet2.Cells(i, 20).value)
                    t21 = CStr(xlWorkSheet2.Cells(i, 21).value)
                    t22 = CStr(xlWorkSheet2.Cells(i, 22).value)
                    t23 = CStr(xlWorkSheet2.Cells(i, 23).value)
                    t24 = CStr(xlWorkSheet2.Cells(i, 24).value)
                    t25 = CStr(xlWorkSheet2.Cells(i, 25).value)
                    t26 = CStr(xlWorkSheet2.Cells(i, 26).value)
                    t27 = CStr(xlWorkSheet2.Cells(i, 27).value)
                    t28 = CStr(xlWorkSheet2.Cells(i, 28).value)
                    t29 = CStr(xlWorkSheet2.Cells(i, 29).value)
                    t30 = CStr(xlWorkSheet2.Cells(i, 30).value)
                    t31 = CStr(xlWorkSheet2.Cells(i, 31).value)
                    t32 = CStr(xlWorkSheet2.Cells(i, 32).value)
                    t33 = CStr(xlWorkSheet2.Cells(i, 33).value)
                    Form3.DataGridView1.Rows.Add(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20, t21, t22, t23, t24, t25, t26, t27, t28, t29, t30, t31, t32, t33)
                    TextBox2.Text = "Row " & i
                    ProgressBar1.Value = ProgressBar1.Value + 1
                Else
                    ProgressBar1.Value = ProgressBar1.Value + 1
                End If
            Next

    • Marked as answer by k7s41gx Saturday, January 12, 2019 9:45 PM
    Friday, January 11, 2019 2:33 PM

All replies

  • Hello,

    If the result of an operation is returning null/nothing then you need to check for this e.g. IsNot Nothing should be used to assert there is a value or no value then within the if act e.g. set a value of a DataGridView or TextBox else set something to an empty string. Otherwise as you have seen simply assigning a value without checking will throw an exception.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 11, 2019 12:58 PM
    Moderator
  • Hey Karen!

    Didn't realize you responded before I edited the question. I did some digging and found out the cell itself is not empty but instead contains "W/C 68" for example. When vb tried to read this i'm getting the null exception. Any ideas?

    Friday, January 11, 2019 2:08 PM
  • Hey again,

    I have fixed the "w/c 68" showing as error from cell by using raw data instead of an array. No idea why that wasn't working, but this adds the empty cells and all. Is there a way to process an xlsx faster? I have 311k rows and this is quite slow.

    Thanks,

            For i As Integer = 2 To counta
                If xlWorkSheet2.Cells(i, 21).value.ToString = Form3.Label1.Text Then
                    t1 = CStr(xlWorkSheet2.Cells(i, 1).value)
                    t2 = CStr(xlWorkSheet2.Cells(i, 2).value)
                    t3 = CStr(xlWorkSheet2.Cells(i, 3).value)
                    t4 = CStr(xlWorkSheet2.Cells(i, 4).value)
                    t5 = CStr(xlWorkSheet2.Cells(i, 5).value)
                    t6 = CStr(xlWorkSheet2.Cells(i, 6).value)
                    t7 = CStr(xlWorkSheet2.Cells(i, 7).value)
                    t8 = CStr(xlWorkSheet2.Cells(i, 8).value)
                    t9 = CStr(xlWorkSheet2.Cells(i, 9).value)
                    t10 = CStr(xlWorkSheet2.Cells(i, 10).value)
                    t11 = CStr(xlWorkSheet2.Cells(i, 11).value)
                    t12 = CStr(xlWorkSheet2.Cells(i, 12).value)
                    t13 = CStr(xlWorkSheet2.Cells(i, 13).value)
                    t14 = CStr(xlWorkSheet2.Cells(i, 14).value)
                    t15 = CStr(xlWorkSheet2.Cells(i, 15).value)
                    t16 = CStr(xlWorkSheet2.Cells(i, 16).value)
                    t17 = CStr(xlWorkSheet2.Cells(i, 17).value)
                    t18 = CStr(xlWorkSheet2.Cells(i, 18).value)
                    t19 = CStr(xlWorkSheet2.Cells(i, 19).value)
                    t20 = CStr(xlWorkSheet2.Cells(i, 20).value)
                    t21 = CStr(xlWorkSheet2.Cells(i, 21).value)
                    t22 = CStr(xlWorkSheet2.Cells(i, 22).value)
                    t23 = CStr(xlWorkSheet2.Cells(i, 23).value)
                    t24 = CStr(xlWorkSheet2.Cells(i, 24).value)
                    t25 = CStr(xlWorkSheet2.Cells(i, 25).value)
                    t26 = CStr(xlWorkSheet2.Cells(i, 26).value)
                    t27 = CStr(xlWorkSheet2.Cells(i, 27).value)
                    t28 = CStr(xlWorkSheet2.Cells(i, 28).value)
                    t29 = CStr(xlWorkSheet2.Cells(i, 29).value)
                    t30 = CStr(xlWorkSheet2.Cells(i, 30).value)
                    t31 = CStr(xlWorkSheet2.Cells(i, 31).value)
                    t32 = CStr(xlWorkSheet2.Cells(i, 32).value)
                    t33 = CStr(xlWorkSheet2.Cells(i, 33).value)
                    Form3.DataGridView1.Rows.Add(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20, t21, t22, t23, t24, t25, t26, t27, t28, t29, t30, t31, t32, t33)
                    TextBox2.Text = "Row " & i
                    ProgressBar1.Value = ProgressBar1.Value + 1
                Else
                    ProgressBar1.Value = ProgressBar1.Value + 1
                End If
            Next

    • Marked as answer by k7s41gx Saturday, January 12, 2019 9:45 PM
    Friday, January 11, 2019 2:33 PM