簡體   English   中英

從另一個對象訪問Form組件將引發“未處理System.NullReferenceException”

[英]accessing a Form component from another object throws “System.NullReferenceException was unhandled”

我正在嘗試從WCF對象中修改屬於Form2的TextBox。

namespace server2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private ServiceHost duplex;

        private void Form2_Load(object sender, EventArgs e)     /// once the form loads, create and open a new ServiceEndpoint.
        {
            duplex = new ServiceHost(typeof(ServerClass));
            duplex.AddServiceEndpoint(typeof(IfaceClient2Server), new NetTcpBinding(), "net.tcp://localhost:9080/service");
            duplex.Open();
            this.Text = "SERVER *on-line*";
        }
    }


    class ServerClass : IfaceClient2Server
    {


        IfaceServer2Client callback;

        public ServerClass()
        {
            callback = OperationContext.Current.GetCallbackChannel<IfaceServer2Client>();

        }

        public void StartConnection(string name)
        {
            var myForm = Form.ActiveForm as Form2;
            myForm.textBox1.Text = "Hello world!";  /// <- this one trows “System.NullReferenceException was unhandled”
                                                    /// unless Form2 is selected when this fires.

            callback.Message_Server2Client("Welcome, " + name );
        }


        public void Message_Cleint2Server(string msg)
        {
        }

        public void Message2Client(string msg)
        {
        }

    }




    [ServiceContract(Namespace = "server", CallbackContract = typeof(IfaceServer2Client), SessionMode = SessionMode.Required)]


    public interface IfaceClient2Server           ///// what comes from the client to the server.
    {
        [OperationContract(IsOneWay = true)]
        void StartConnection(string clientName);

        [OperationContract(IsOneWay = true)]
        void Message_Cleint2Server(string msg);
    }


    public interface IfaceServer2Client          ///// what goes from the sertver, to the client.
    {
        [OperationContract(IsOneWay = true)]
        void AcceptConnection();

        [OperationContract(IsOneWay = true)]
        void RejectConnection();

        [OperationContract(IsOneWay = true)]
        void Message_Server2Client(string msg);
    }

}

但是“ myForm.textBox1.Text =”世界你好!“;” 拋出System.NullReferenceException未處理”。

任何想法,謝謝!

myForm可能不是Form2類型,或者可能不包含textBox1字段。 對於這兩種情況,請確保檢查是否為null。

如最初問題的注釋中所述,問題在於您要使用的ActiveForm是您想要的表單未處於活動狀態。 每當嘗試使用as關鍵字進行轉換時,如果轉換無效,則結果將為null。 由於您獲取了無法轉換為Form2的表單(因為它是另一種表單),因此您正確地收到了null引用異常。

假設您在Form2上執行了單例規則,並且沒有使用表單的名稱,則可以通過Application.OpenForms集合來訪問它,如下所示:

(Form2)Application.OpenForms["Form2"];

在您的代碼示例中,可能看起來像這樣:

public void StartConnection(string name)
{
    //var myForm = Form.ActiveForm as Form2;
    var myForm = Application.OpenForms["Form2"] as Form2;
    myForm.textBox1.Text = "Hello world!";  /// <- this one trows “System.NullReferenceException was unhandled”
                                            /// unless Form2 is selected when this fires.

    callback.Message_Server2Client("Welcome, " + name );
}

就是說,我認為我不負責修改WCF服務的表單控件。 我會更快地消耗表單中服務觸發的事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM