简体   繁体   中英

Object reference not set to an instance of an object error in SSIS package

I have the following code in a script task but when I try execute the script I get an error with the following message: "Exception has been thrown by the target of an invocation. ---> Object reference not set to an instance of an object."

I am trying to address a deadlock error in my package with the use of variables. Based on the value of Region, I'm trying to populate another variable InputRegion.

    public void Main()
    {
        String InputFile;
        int StrLen;
        Variables var = null;    
        Dts.VariableDispenser.LockOneForWrite("User::Region", ref var);
        var[0].Value = "DEFAULT";
        InputFile = (String)vars["User::InputRegion"].Value;

        if (InputFile == "Europe.txt")
            var[0].Value = "European";
        if (InputFile == "Amers.txt")
            var[0].Value = "American";
        if (InputFile == "Tokyo.txt")
            var[0].Value = "Asian";
        else        
            var[0].Value = "Unknown";

        var.Unlock();

        Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}

With the given informations, I can guess that :

The problem

 Variables var = null;  

You initialize your variable with a null valu and then you use it without giving a value.
Because you use it there (probably where you get the exception too)

  Dts.VariableDispenser.LockOneForWrite("User::Region", ref var);

Also

If the line of code above doesn't set a value to var, your program could crash :

  • There: var[0].Value = "DEFAULT";
  • There: var[0].Value = "European";
  • There: var[0].Value = "American";
  • There: var[0].Value = "Asian";
  • There: var[0].Value = "Unknown";
  • Or There: var.Unlock();

Possible Solution

Try giving it a valid value instead of null to make sure the problem is there.

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