Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi

I have two projects named ChatServer and ChatClient. I make use of events and delegats to raise the event when user clicks on login button.

The code on ChatServer is:

C#
public class ChatServerClass : IServiceInterface
   {
       public delegate void UserJoin(string username);

       public event UserJoin Newjoin;
       public void JoinChat(string name)
       {

           if (flag == true)
           {
              ListofUsers.Add(name);
              MessageBox.Show("User Logged in");
              Newjoin(name);
           }
           else
               MessageBox.Show("Select other Name");

           ListofUsers.ToArray();

       }


The code on ChatClient is

C#
private void login_btn_Click(object sender, EventArgs e)
{
//Client newclient = new Client(login_txt.Text);
username = login_txt.Text;
//InstanceContext context = new InstanceContext(Icall);
//connect.Open();
client.JoinChat(username);
ChatServerClass cs=new ChatServerClass();
cs.Newjoin+=new Userjoin(UserEnter);

 public void UserEnter(string username)
{
MessageBox.Show("User" +username + "is online");

}


The reference to ChatClient is added in ChatServer. I mean i am adding using ChatClient reference in ChatServer project. When i click on the login button i get exception saying tat object reference not set to the instance.

PLease help
Posted

First of there is some code missing, I'm sure it's in your project but in order to be able to give an accurate answer you need to provide the following.
flag is not defined anywhere and it's not being assinged a value anywhere either.
Since I can't see how flag is treated I'm going to assume that flag is true when you call client.JoinChat(username); in login_btn_Click
and in that case Newjoin is null when no handler has been assigned to the event. So you need to check for null.
C#
if (flag == true)
{
    ListofUsers.Add(name);
    MessageBox.Show("User Logged in");

    UserJoin handler = Newjoin; // this is not necesarry, it just
    if(handler != null)         // to make it threadsafe.
        handler (name);
}
 
Share this answer
 
v3
Comments
Mehdi Gholam 20-Oct-11 10:26am    
my 5!
Simon Bang Terkildsen 20-Oct-11 17:14pm    
Thank you, Mehdi.
It would help in future to know which line is causing the error.
As it is, since the error is in the login_btn_Click method, it must be that login_txt is null (unlikely, that seems to be a textbox) or client is. Put a breakpoint on the first line of the method and follow it through. My guess is that you have failed to initialize client to a new instance somewhere else in your code.
 
Share this answer
 
did you try debugging it? if not then i recommend doing that first, you will be able to point out which object is null which is why this exception gets thrown. BTW, whats this line doing ?
C#
client.JoinChat(username);
have you instantiated the client object?? what object exactly its meant to be?

Cheers...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900