1

I get null on my event , and Just can get why . this is my code (shortly)

Here I subscribe to the event

class Form1
{       
public Form1()
{

InitializeComponent();

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

}
//...
}

in main Form I initialize cEventHandler class and subscribe to OnPrintToScreen event , this is event fires whenever I want to print to screen ,

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);
        }
    }
}

Here I fire It

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");
            }

        }

    }

my problem is with the onDataInput event , I dont understand what am I missing :(

this question is not a duplicate because it tackles a more complicated scenario

LordTitiKaka
  • 2,087
  • 2
  • 31
  • 51
  • Where did you create instance of cEventHandler? – Adil Jun 17 '14 at 09:02
  • @Adil I've added the missing code , but I dont think it is the problem – LordTitiKaka Jun 17 '14 at 10:43
  • 1
    Why you guys signed as dup' ? It is clearly not that identical – LordTitiKaka Jun 17 '14 at 10:44
  • On what line you get the exception? – Adil Jun 17 '14 at 10:45
  • internal void port_DataReceived method --> the event(onDataInput) there is always Null , I've got the exception before I've added the IF there – LordTitiKaka Jun 17 '14 at 10:48
  • Have you tried debugging it? – Thomas Weller Jun 17 '14 at 12:24
  • @ThomasW. Yes , if I put break point in inside the Contractor of cEventHandler i clearly see that the event Is not Null and yet I can notice the event is NULL , I can guess that this is because the subscribe is in another class but I can't understand how to fix it – LordTitiKaka Jun 17 '14 at 12:28
  • In Visual Studio, go to Debug/Exceptions. Then check the "thrown" checkbox for "Common Language Runtime". This will make Visual Studio stop in the line where something is null. Move your mouse over every item in that line to see what exactly is null. – Thomas Weller Jun 17 '14 at 18:11
  • @ThomasW. the event is null as if I've never subscribe to it , I think I making the event static will solve this problem , but I dont know if it is a good idea – LordTitiKaka Jun 17 '14 at 18:31

0 Answers0