使用invoke时出现NullReferneceException

时间:2016-09-22 09:50:01

标签: c#

我正在尝试为我的程序中的一个委托使用invoke方法,该委托代表一个事件处理程序(当我的应用程序连接到设备时激活)。但它给了我NullReferneceException处理invoke方法时未处理的错误,并说该对象引用未设置为对象的实例。但是,当我在调试模式下检查监视窗口时,invoke的参数不为null。我是一名敏锐的编码新手,我试图在任何地方寻找解决方案,但似乎没有任何结果,所以我将非常感谢任何帮助,谢谢。

public partial class Reapiclass : Form    
 { 
   private void OnConnect(ReaPi.ConnectionIdentifier connection)
   {
    if (this.InvokeRequired)
    {
        Reapiclass.ConnectEventHandler connectEventHandler = new Reapiclass.ConnectEventHandler(this.OnConnect);
        if (!this.Created || this.IsDisposed)
            return;

        this.Invoke((Delegate)connectEventHandler, (object) connection);
   //     connectEventHandler(connection);
    }
    else
    {
       // some other functionality here     
     }
}
 public void OnReaPiConnectionCallbackPtr(ReaPi.ConnectionIdentifier connection, ReaPi.EConnState state, ReaPi.EErrorCode errorcode, IntPtr context)
{
    if (state == ReaPi.EConnState.CONNECT)
    {
        foreach (REAPILibConnection reapiLibConnection in this._infoREAPILib.Connections.Values)
           this.textLastError.Text = ReaPi.Disconnect(reapiLibConnection.ID).ToString();
        MessageBox.Show("Recognised");
        this._infoREAPILib.Connections.Add(connection, new REAPILibConnection(connection, connection.ToString(), (Version)null));
        this._infoREAPILib.CurrentConnectionIdentifier = connection;
        IntPtr context1 = new IntPtr(0);
        this._eventCallback = new ReaPi.eventCallbackPtr(this.OnReaPiEventCallbackPtr);
        this._responseCallback = new ReaPi.responseCallbackPtr(this.OnReaPiResponseCallbackPtr);
        int num1 = (int)ReaPi.RegisterResponseCallback(this._infoREAPILib.Connections[connection].ID, this._responseCallback, context1);
        int num2 = (int)ReaPi.RegisterEventCallback(this._infoREAPILib.Connections[connection].ID, this._eventCallback, context1);
        this.OnConnect(connection);
    }
    else if (state == ReaPi.EConnState.DISCONNECT)
    {
        // some other functionality here
    }

}
public delegate void ConnectEventHandler(ReaPi.ConnectionIdentifier connection);
}

0 个答案:

没有答案