0

My code works in debug mode, but in release mode I get a System NullReference Exception for the MTable in line 6 and I don't know the reason. In debug mode I have access to the data.

Dim myregex As Regex = New Regex("A[1-6]|A[A-M]") 
Dim MTable As New DataTable
Dim dvr As New System.Windows.Forms.DataGridViewRow

If datasetB.Tables.Contains("BLA") Then

    MTable = datasetB.Tables("BLA").Select().
Where(Function(r) myregex.IsMatch(r.Item("Data"))).ToArray().CopyToDataTable

    MTable.Columns.Add(New DataColumn("textt", GetType(String)))
    MTable.Columns.Add(New DataColumn("texttt", GetType(String)))
    MTable.Columns.Add(New DataColumn("textttt", GetType(String)))
    MTable.Columns.Add(New DataColumn("texttttt", GetType(String)))

    ErrorsDgV.DataSource = MTable

    For Each dvr In Me.ErrorsDgV.Rows

            If dvr.Cells(5).Value IsNot Nothing Then

                'do some things

            End If

        Next
    Else
        MsgBox("didn't work")
    End If

Thanks in advance!

  • 2
    We can't tell which is line 6. Also, you don't need that `.Select()` in there. Additionally you should use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) because correcting the problems it points out can often fix the code. – Andrew Morton Aug 14 '19 at 12:46
  • Line 6 is the sixth line of the code that I inserted above :) and I can't use `.Where` without `.Select()` –  Aug 14 '19 at 13:05
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – TnTinMn Aug 14 '19 at 13:59
  • @ezma well you *can* use `Where` without `Select`. I have never used the empty empty select myself. What if you tried `MTable = datasetB.Tables("BLA").Rows.OfType(Of DataRow).Where(Function(r) myregex.IsMatch(r.Item("Data"))).ToArray().CopyToDataTable` which doesn't convert to an Array? – djv Aug 14 '19 at 14:45
  • Where is `datasetB` declared & loaded? Can you show that code? – HardCode Aug 14 '19 at 16:41
  • `Dim datasetB As New DataSet` is declared in another `private sub`. Here I load the lines of the file to `StringToAdd = Tools.ExtractFields(line, Ln, Filee)` and add `StringToAdd` to the dataset then: `datasetB.Tables(Trim(StringToAdd(2))).Rows.Add() datasetB.Tables(Trim(StringToAdd(2))).Rows(datasetB.Tables(Trim(StringToAdd(2))).Rows.Count - 1).ItemArray = StringToAdd` –  Aug 15 '19 at 08:00

1 Answers1

0

I opened the Compiler Settings in Visual Studio and selected Release in Configuration. Then I clicked on Advanced Compiler Settings. There I unchecked the Optimize Code Button. This way the code runs as in the debug mode.