Matt
Matt

Reputation: 4200

Very simple ssis script transformation giving Object reference not set to an instance of an object

I have an ssis package that takes a flat file and dumps it to SQL. During that process, 150 columns need to have '-' and '.' removed from them. I have done this using a Script Transformation.

    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
    Dim column As IDTSInputColumn90
    Dim rowType As Type = Row.GetType()
    Dim columnValue As PropertyInfo

    For Each column In Me.ComponentMetaData.InputCollection(0).InputColumnCollection

        columnValue = rowType.GetProperty(column.Name)

        Dim strCol As String = columnValue.GetValue(Row, Nothing).ToString()
        If Not String.IsNullOrEmpty(strCol) Then
            strCol = strCol.Replace("-", "").Replace(".", "")
            columnValue.SetValue(Row, strCol, Nothing)
        End If
    Next
End Sub

I however get the following error when running it

Object reference not set to an instance of an object.

at ScriptComponent_f20c21e11e3348378ef0bbe6b65e9c05.ScriptMain.Input0_ProcessInputRow(Input0Buffer Row)
at ScriptComponent_f20c21e11e3348378ef0bbe6b65e9c05.UserComponent.Input0_ProcessInput(Input0Buffer Buffer)
at ScriptComponent_f20c21e11e3348378ef0bbe6b65e9c05.UserComponent.ProcessInput(Int32 InputID, PipelineBuffer Buffer)
at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.ProcessInput(Int32 inputID, PipelineBuffer buffer)

I can't figure out why this is happening.. Any thoughts?

Edit I've discovered that this line is the problem

Dim strCol As String = columnValue.GetValue(Row, Nothing).ToString()

Upvotes: 0

Views: 8086

Answers (1)

billinkc
billinkc

Reputation: 61269

I was able to get it to work for my dataset. I skipped the cast to string type and just left it as object type based on what I assume to be the source of your script. It's not pretty but it seems to get the job done.

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
    Dim column As IDTSInputColumn90
    Dim rowType As Type = Row.GetType()
    Dim columnValue As PropertyInfo

    For Each column In Me.ComponentMetaData.InputCollection(0).InputColumnCollection

        columnValue = rowType.GetProperty(column.Name)
        Dim meta As Integer = column.ExternalMetadataColumnID
        Try
            Dim Obj As Object = columnValue.GetValue(Row, Nothing)
            If Not String.IsNullOrEmpty(Obj) Then
                Obj = Obj.Replace("-", "").Replace(".", "")
                columnValue.SetValue(Row, Obj.ToString(), Nothing)
            End If
        Finally
            ' Rejoice in our null-ness
        End Try
    Next
End Sub

Source data

Starting with this data

SourceSSN,DestinationSSN,Amount
,111-11-0000,5000.00
111-22-3333,111-22-3334,5000.00
121-22-3333,121-22-3334,5000.00
123-22-3333,123-22-3334,5000.00
124-22-3333,124-22-3334,5000.00

The above script makes this

enter image description here

Upvotes: 1

Related Questions