4

I have a DataFlow where there is a Script Component as a Source.

I have defined the Output (OutputRows) and Column (MyOutputValue) according to my needs.

When I want to test my script, even with hard coded values, I always get the same error:

System.NullReferenceException: Object reference not set to an instance of an object. at ScriptMain.CreateNewOutputRows().

I have no clue what's going wrong here. Any idea?

Here my code:

using System;
using System.Data;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using Microsoft.SqlServer.Dts.Runtime;
using Excel = Microsoft.Office.Interop.Excel;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
#region Members
    String MyOutputValue;
#endregion

public override void PreExecute()
{
    base.PreExecute();


    MyOutputValue = "test";

    CreateNewOutputRows();

}

public override void PostExecute()
{

base.PostExecute();
}

public override void CreateNewOutputRows()
{
    OutputRowsBuffer.AddRow();

    OutputRowsBuffer.MyOutputValue = MyOutputValue;
}
}

Within my SSIS Package I start debug and then I get the following screen (it is german, so I translated the error into english for this post): enter image description here

BaseBallBatBoy
  • 685
  • 2
  • 12
  • 25
  • Exactly where do you get the exception? Please post the stack trace. – John Saunders Jun 17 '13 at 13:04
  • I added the message I get when I debug the Package. This is all what I have. Would you need me to add something more in the code to give you more info about the error? – BaseBallBatBoy Jun 17 '13 at 13:38
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jun 17 '13 at 13:40

1 Answers1

6

The SSIS runtime doesn't initialize output buffers (such as your OutputRowsBuffer object) until after the PreExecute method returns.

Unfortunately, your PreExecute method is calling CreateNewOutputRows directly, which is causing the NullReferenceException. Instead, you should let the SSIS runtime call CreateNewOutputRows (which it will, at the appropriate point in the execution cycle):

public override void PreExecute()
{
    base.PreExecute();

    MyOutputValue = "test";

    // Do NOT call CreateNewOutputRows from PreExecute!
    // CreateNewOutputRows(); 
}

public override void CreateNewOutputRows()
{
    OutputRowsBuffer.AddRow();
    OutputRowsBuffer.MyOutputValue = MyOutputValue;
}

See Creating a Source with the Script Component page on MSDN for additional sample code.

Edmund Schweppe
  • 4,992
  • 1
  • 20
  • 26