Error in VB script

  • Hi,

    I am getting an error in the VB script I have created - 'Object reference not set to an instance of an object.'

    Pls find the script below (I have mentioned the line on which I get the error):

    --------------------------------------------------------------------------------

    Imports System

    Imports System.Data

    Imports System.Math

    Imports Microsoft.SqlServer.Dts.Runtime

    Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper

    Imports Microsoft.SqlServer.Dts.Runtime.Wrapper

    Imports Microsoft.VisualBasic.FileIO.FileSystem

    Imports System.IO.FileSystemInfo

    Imports System.IO

    Imports System.Net

    Public Class ScriptMain

    Public Sub Main()

    Dim folders() As String

    Dim files() As String

    Dim filesZip() As String

    Dim i As Integer

    'if there're no files finish script

    If files.Length = 0 Then 'Getting Error on this line

    GoTo koniec

    End If

    'the bigest number

    Dim biggest As Integer = 0

    Dim n As Integer

    Dim t As Integer

    Dim sa As Boolean = False

    'filtering not zip and not in format

    For i = 0 To files.Length - 1

    If files(i).Substring(0, 12) = "12012_12012_" And files(i).Substring(files(i).Length - 3, 3) = "zip" Then

    sa = True

    If files(i).Length = 26 Then

    n = CInt(files(i).Substring(12, 1))

    ElseIf files(i).Length = 27 Then

    n = CInt(files(i).Substring(12, 2))

    ElseIf files(i).Length = 28 Then

    n = CInt(files(i).Substring(12, 3))

    ElseIf files(i).Length = 29 Then

    n = CInt(files(i).Substring(12, 4))

    End If

    If i > 0 Then

    If n > t Then

    biggest = i

    t = n

    End If

    End If

    If i = 0 Then

    t = n

    End If

    End If

    Next

    '-------------------automatic input

    Dts.Variables("filename").Value = files(biggest)

    Dts.Variables("file").Value = "/" & files(biggest)

    Dts.Variables("xmlfile").Value = files(biggest).Replace("zip", "xml")

    Dts.Variables("renameFile").Value = "C:\Visio2020Upload\" & files(biggest).Replace("zip", "xml")

    '--------------------automatic input end

    Dts.TaskResult = Dts.Results.Success

    GoTo koniec2

    koniec:

    Dts.TaskResult = Dts.Results.Failure

    koniec2:

    End Sub

    End Class

    ------------------------------------------------------------------------------

    I am quite new to VB script and would appreciate any suggestions.

    Thanks,

    Paul

  • You declared your files array but never instantiated it. In other words, you need to get your files somewhere. You need something like files = GetListOfFileNamesFromSomeLocation

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply