I am getting the error in the title in my VB.Net code, i try to do auto fix and then run the package but get this error below
Object reference not set to an instance of an object.
at SC_4a02d9c8abdc45aa98dd86fbd048370e.ScriptMain.CreateNewOutputRows()
This is my script below as well:
Public Overrides Sub CreateNewOutputRows()
Dim LoadDate As Date = Nothing
Dim Notes As String = Nothing
Dim CardNum As String = Nothing
Dim LineData As String = Nothing
Dim r As String = Nothing
Dim sr As IO.StreamReader
Dim LoopFile As String = Variables.Output.ToString() "FileName.txt"
i get the error at Variable.Output.ToString()
i tried to use "show potential fix in vb"
Anyone have an idea of what i can do differently?
CodePudding user response:
error is occurring because the Variables.Output object is Nothing (i.e., it has not been instantiated).
To fix the error, you will need to make sure that Variables.Output is properly instantiated before attempting to use it. One way to do this might be to add a line of code at the beginning of your CreateNewOutputRows method to instantiate Variables.Output, like this:
Variables.Output = New Object()
And you might need to modify your code so that Variables.Output is properly instantiated before it is used in the CreateNewOutputRows method
CodePudding user response:
I don't believe you've given us enough information to reproduce your problem. The following code assume you have an SSIS Variable named Output, of type String and I initialized it to C:\foo\bar\blee\
That trailing slash is likely going to be important when you compare this solution to yours.
I added a DataFlow to my package, added a Script Component as Source. I added a column to the Output0Buffer named LoopFile
of type DT_STR length 150. I changed the language type to VB.NET for applications (name approx) and finally, I checked Output as a member of the ReadOnly variables.
I used the following code
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
<Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute>
<CLSCompliant(False)> _
Public Class ScriptMain
Inherits UserComponent
Public Overrides Sub CreateNewOutputRows()
Dim LoadDate As Date = Nothing
Dim Notes As String = Nothing
Dim CardNum As String = Nothing
Dim LineData As String = Nothing
Dim r As String = Nothing
Dim sr As IO.StreamReader
Dim LoopFile As String = Variables.Output.ToString() "FileName.txt"
Output0Buffer.AddRow()
Output0Buffer.LoopFile = LoopFile
End Sub
End Class
I added a data viewer and connected a do nothing Derived Column
You can see that the value pushed into that variable is a normalized path.
What's likely the problem
Based on the visible code which instantiates a StreamReader and you're using string building to build a file path, I can infer you're about to open a file, read the data and then assign values to a data flow buffer. Something in that is wrong.
Path building
Lazy path building via string concatenation is fine until someone doesn't give you the data as expected. If the value of Variables.Output.ToString() does not end with a trailing slash, then the value of LoopFile
is not going to be valid C:\foo\bar\bleeFileName.txt
vs C:\foo\bar\blee\FileName.txt
The right way is to just use the System.IO.Path.Combine when putting path objects together. Trust that the fine engineers at Microsoft have tested and put far more hours into dealing with edge cases than you'll ever have to consider, much less solve. https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=netframework-4.7.2
StreamReader
The value of LoopFile is as expected? Great then the next candidate is the StreamReader usage (not shown). https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader?view=netframework-4.7.2
If your code doesn't have something like the following after you built the LoopFile value, then that's the source of your Object reference not set to an instance of an object.
sr = new IO.StreamReader(LoopFile)
I'd also advocate changing out the above line the declaration into a using statement. It makes things cleaner from a logic encapsulation resource management.
Dim LoopFile As String = System.IO.Path.Combine(Variables.Output.ToString() , "FileName.txt")
Using sr As New StreamReader(LoopFile)
' Do stuff here
End Using
What else
Permutations are wild here, but ensure you are actually getting values for your LoadDate, Notes etc as I assume they're parsed out of the text of your stream reader calls. Ensure that you call the .AddRow()
to your output buffer and actually assign buffer values from your local variables as I show in my sample code.
Without seeing actual code some data for FileName.txt, it'll be hard to say what else is wrong.
If you love VB.NET, great. Otherwise, you're going to find far more sample C# code on the internet than vb.
Finally, given that you're calling something LoopFile, there's likely some more native SSIS things that you could be using here.