简体   繁体   中英

UiPath Invoke Code with VB.net vs Visual Studio

After a few days of trying to solve this one I would like to ask you for help - we are currently developing RPA workflow that takes PDF file, reads it, formats it (it's invoice in PDF) and then does something with the data. Right now I have code working in Visual studio of taking string from read PDF activity and formatting it to datatable to further procession. In Visual Studio everything is fine even with Strict On and everything, but when I insert the code into Invoke Code activity all I get is

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ----> System.NullReferenceException: Object reference not set to an instance of an object. at UiPathCodeRunner_f63e6e400b2d4eb6b485d3e5c0e78e7f.Run(String temp_string, DataTable& dtable, String& ex, Int32 tab_len, String[] array_main)

Tried chopping the code up to smaller pieces, which helped in narrowing the problem down as I suspect the problem is somewhere in the datatable converting. Other than that I see no problem with the code, VS see no problem with the code with the same data inputted and previous version of UiPath also saw no problem with the code (recently updated studio 2018.2.2 i think to 2019.4.4 and had to rewrite the whole code because the read pdf activity 2.0.0 and 3.0.0 read pdf differently.

I know lot of the code can be simplified but I rewrote it in as simple steps as I thought possible so UiPath can digest it. Appreciate any help you can give me and also forgive my formatting, first question here.

So the code:

        Dim e As String

        Dim temp_string As String = "some long string that is delimited by \r\n"
        Dim splitfinal As String

        Dim delim1 As String() = New String() {"delimiter of invoice body start"}
        Dim delim2 As String() = New String() {"delimiter of invoice body end"}
        Dim delim3 As String() = New String() {"\r\n"}
        Dim split1 As String()
        Dim split2 As String()

        split1 = temp_string.Split(delim1, StringSplitOptions.None)
        split2 = split1(1).Split(delim2, StringSplitOptions.None)
        splitfinal = split2(0).ToString()

        Dim array_main As String() = splitfinal.Split(delim3, StringSplitOptions.None)
        Dim array_len As Integer = array_main.Length

        Dim temp As Double = Int((array_len) / 2) - 3

        Dim tab_len As Integer = Convert.ToInt32(temp)
        Dim table(tab_len, 9) As String

        Dim i As Integer
        Dim j As Integer = 1
        Dim k As Boolean = True

        Try

            i = 0
            '''''
            For Each line As String In array_main
                If (i>tab_len) Then 
                    Exit For
                ElseIf line.Trim = "" Then
                    Continue For
                ElseIf line.Trim = line.Split(" "c)(0) Then
                    table(i, 0) &= " " & line.Trim
                    Continue For
                End If

                If k = True Then

                    j = 1

                    For Each word As String In line.Split(" "c)
                        If word.Trim.Contains("/") Or word.Trim.Contains("\") And j = 1 Then
                            table(i, j) = word.Trim
                            j = 3
                        ElseIf Int32.TryParse(word.Trim, Nothing) = False And table(i, 0) = Nothing Then
                            table(i, 0) = word.Trim
                        ElseIf Int32.TryParse(word.Trim, Nothing) = False And table(i, 0) <> Nothing And j = 1 Then
                            table(i, 0) &= " " & word.Trim
                        ElseIf (word.Trim = "6117") Or (word.Trim = "6118") Then
                            table(i, 2) = word.Trim
                            k = False
                        ElseIf Int32.TryParse(word.Trim, Nothing) And j = 1 Then
                            table(i, j) = word.Trim
                            j = 3
                        ElseIf j <> 1 Then
                            table(i, j) = word.Trim
                            j += 1
                        Else 
                            'Throw New Exception
                        End If

                    Next

                ElseIf k = False Then

                    For Each word As String In line.Split(" "c)
                        If (j = 8) And (k = False) Then
                            table(i, 2) &= word.Trim
                            k = True
                        ElseIf j = 9 Then
                            table(i, j) = word.Trim
                            If (i>tab_len) Then 
                                Continue For
                            Else 
                                i += 1
                            End If
                            j = 1
                        Else
                            table(i, j) = word.Trim
                            j += 1

                        End If

                    Next

                End If

            Next

        Catch ex As Exception
            e = ex.ToString
        End Try

        ''done Now to convert to datatable

        Dim dtable = New DataTable

        For i = 0 To tab_len
            Dim row As DataRow = dtable.NewRow
            For j = 0 To 9
                If (i = 0) Then
                    dtable.Columns.Add(j.ToString, GetType(String))
                End If
                If (table(i, j) = Nothing) Then
                    row(j) = ""
                Else
                    row(j) = table(i, j)
                End If
            Next
            dtable.Rows.Add(row)
        Next

Resolved it via standalone c# app, as I needed fast resolution. (Well didn't exactly resolve the issue but more like bypassed it) But still leaving the code in backup so I can try it if anyone has resolution in Invoke Code activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM