从另一个类触发事件时NullReferenceException

时间:2014-06-17 08:55:07

标签: c# events

我的活动无效,而且可以理解为什么。 这是我的代码(很快)

我订阅了这个活动

class Form1
{       
public Form1()
{

InitializeComponent();

_domainEventHandler = new cEventHandler();            
_domainEventHandler.OnPrintToScreen +=new PrintToScreen_EventHandler(OnDataToPrint);   

}
//...
}
主要Form中的

我初始化cEventHandler class并订阅OnPrintToScreen event,只要我想打印到屏幕,就会触发事件

namespace DomainLayer
{
    public delegate void PrintToScreen_EventHandler(object source, string data, string Location);
    /// <summary>
    /// class to handle all events 
    /// </summary>
    public class cEventHandler
    {
        /// <summary>
        /// event to send things back to GUI
        /// </summary>
        public static ScriptingSerial _infrastructureSerialAgent = new ScriptingSerial();

        public cEventHandler()
        {
            _infrastructureSerialAgent.onDataInput += new SerialDataInput_EventHandler(OnDataInput);

        }

        public void OnDataInput(object source , string data , string Location)
        {
            //print to screen
            OnPrintToScreen(this, data, Location);
        }
    }
}

我在这里解雇它

namespace InfraStructure
{
    public delegate void SerialDataInput_EventHandler(object source , string data , string Location);

    //send command
    //wait for a return line
    public class ScriptingSerial
    {

        public event SerialDataInput_EventHandler onDataInput;
        public ScriptingSerial(/**bla bla**/)
        {
                 //declare stuff
        serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);

        }

        internal void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string serialInputData = serialPort.ReadExisting();
            BufferLog.Add(serialInputData);
            if (onDataInput != null)
            {
                onDataInput(this, serialInputData, "Terminal");
            }

        }

    }

我的问题是onDataInput事件,我不明白我错过了什么:(

这个问题并不重复,因为它解决了一个更复杂的问题

0 个答案:

没有答案