function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JJoshJLJJoshJL 

Object refrence not set to the instance of an object

Greetings all, I have an extremely strange situation occurring that I cannot figure out what is going on. I have a piece of code that works beautifully on two differnat applications. I created a new application to streamline some of my daily functions, and once again needed this same code snippit. Anyway, I ported the same code over that I have used on several differant applications, works fine on those application, and on the new application, I get "Object refrence not set to an instance of an object". After doing a little digging, I am able to detirmine that it is dying on

binding.QueryOptionsValue = New Salesforce.QueryOptions

I am properly logging in so I do not believe that is the issue, though normally, that would be the case. I know the Login script works because in order to access the application, you have to authenticate against salesforce. Furtheremore, another script in the same application that updates my assets works fine. The ONLY differance in the way the two scripts are called is that on the successfull application, it is single threaded application, on the faulty one the application is multithreaded. I dont see why this would be an issue though because the script is a public subroutine and runs (in theory) the same way. Also, all of the salesforce variable declarations are the same in both apps. It also uses the same WSDL. Any help is greatly appreciated.

Josh

API 5.0, VB.NET

DevAngelDevAngel

Hi JJoshJL,

You have put your finger directly on the cause.  I've also run into the multi-threaded issue and it is quite frustrating.  Are you doing asp.net or winforms?  If you are doing winforms, you need to "invoke" a method on the form thread to get to the properly initialized instance of your binding object.

I confess to a lack of multi-threading experience in .NET, but the suggestion above has worked for me.

JJoshJLJJoshJL

It is using Winforms...  Going to play dumb for a second, so what method would I need to invoke and where should I put it... 

Thanks

Josh

DevAngelDevAngel

Create a method on the code behind your winform (lets call it DoIt).  On the "other" thread, one of the multi threads that is not the main thread, you would do form.Invoke.  The code snippet below is from the STAPI2 toolkit.  It is called from a custom component hosted on a form.

#region Threaded Browser Launch
delegate void LaunchBrowserDelegate(string url);

internal void LaunchBrowser(string url)
{
    System.Diagnostics.Process.Start(url);
}
internal void InvokeLaunchBrowser(string url)
{
    System.Diagnostics.Trace.WriteLine("Going to invoke LaunchBrowser");
    this.FindForm().Invoke(new LaunchBrowserDelegate(LaunchBrowser), new object[] {url});
}
#endregion

This will cause the LaunchBrowser method to be exectuted on the hosting form's thread.

Check the VS MSDN doc for Invoke on the Form object:

 

Control.Invoke Method 

Executes a delegate on the thread that owns the control's underlying window handle.

Overload List

Executes the specified delegate on the thread that owns the control's underlying window handle.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Invoke(Delegate) As Object
[C#] public object Invoke(Delegate);
[C++] public: Object* Invoke(Delegate*);
[JScript] public function Invoke(Delegate) : Object;

Executes the specified delegate, on the thread that owns the control's underlying window handle, with the specified list of arguments.

[Visual Basic] Overloads Public Overridable Function Invoke(Delegate, Object()) As Object Implements ISynchronizeInvoke.Invoke
[C#] public virtual object Invoke(Delegate, object[]);