0

I am trying to raise an event in a DLL file refrenced to a windows forms project.

I have the following message when I run the program "Object is not set to an instace of an object":

namespace Server {

public delegate void messageHnadler();


public class ClassServer
{


    public event messageHnadler messageForChat




    public string Message { get; set; }

    public Socket listenerSocket;
    public BinaryFormatter transBinary;

    public Thread threadingServer;
    public TcpListener listenerServer;




    private List<TcpClient> connectedClients = new List<TcpClient>();


    public bool OpenServer(string ipAddress, int PortNumber)
    {

        try
        {
            listenerServer = new TcpListener(IPAddress.Parse(ipAddress), PortNumber);//creating listener for clients to connect

            listenerServer.Start();

            threadingServer = new Thread(LoopThroughClients);
            threadingServer.Start();

            threadingServer = new Thread(GetMessage);
            threadingServer.Start();


            return true;
        }
        catch (Exception)
        {

            return false;
        }
    }

    public void LoopThroughClients()
    {
        listenerSocket = listenerServer.AcceptSocket();

    }



    public void GetMessage()
    {
        while (true)
        {
            if (listenerSocket != null)
            {
                NetworkStream streamWithClient = new NetworkStream(listenerSocket);
                transBinary = new BinaryFormatter();
                string stringFromClient = (string)transBinary.Deserialize(streamWithClient);
                if (stringFromClient != null)
                {
                    Message = stringFromClient;
                    messageForChat();
                }

                streamWithClient = new NetworkStream(listenerSocket);
                BinaryFormatter tranBinary = new BinaryFormatter();


                tranBinary.Serialize(streamWithClient, stringFromClient);
                stringFromClient = null;
            }
        }
    }

In the windows forms project I signed the event to a function:

namespace Chat_Project_Server_UI { public partial class SeverUI : Form { OpenServerForm openServer = new OpenServerForm(); ClassServer serverForEvent = new ClassServer();

    public SeverUI()
    {
        InitializeComponent();

        openServer.ShowDialog();

        serverForEvent.messageForChat += new messageHnadler(serverForEvent_messageForChat);  

        OpenningServer();


    }

    public void OpenningServer()
    {


        if(openServer.IsConnected)
        {
            ChatTextBox.AppendText("SERVER OPEN!\n");
        }
        else
        {
            ChatTextBox.AppendText("Faild to open server...\n");
        }

    }
    private void test_Click(object sender, EventArgs e)
    {

        ChatTextBox.AppendText("aaaaa");

    }

    public void EventHolder()
    {

    }

    void serverForEvent_messageForChat()
    {
        ChatTextBox.AppendText(serverForEvent.Message);

    }



}
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – DGibbs Jun 01 '15 at 08:54
  • 1
    I don't quite understand what is the type for openServer and serverForEvent variables. If at all it has to work as you expect, you should create an instance of ClassServer like this. ClassServer server = new ClassServer(); And then set the method to be called - server.messageForChat += serverForEvent_messageForChat; The invoke the method - server.GetMessage() – Sherin Mathew Jun 01 '15 at 08:59
  • 1
    You need to show the rest of your related code otherwise we can't give a definitive answer. – Enigmativity Jun 01 '15 at 09:15
  • Ive updated the post... this is all the code. – Eviatar Hasid Jun 01 '15 at 11:00

2 Answers2

1

Always check if a handler has been assigned first as follows:

var handler = messageForChat;
if (handler != null)
    handler()
Kevin
  • 2,281
  • 1
  • 14
  • 16
0

Standard Way to handle Events inside the Class that defines it Is to Create A Method Named OnXxx and Always Check whether the Event is Assigned Handler Or Not

in your case define new function as following:

protected void OnMessageForChat(){
    //protected modifier allows subclasses to raise the event by calling this method
    if (messageForChat!=null)messageForChat();
}

And WhenEver you want to raise the event Just Call this Function

Aladdin
  • 339
  • 1
  • 2
  • 15