对象引用未设置为实例-事件和委托 [英] Object reference not set to instance- Events and delegates

查看:156
本文介绍了对象引用未设置为实例-事件和委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个名为ChatServer和ChatClient的项目.当用户单击登录按钮时,我利用事件和委托来引发事件.

ChatServer上的代码是:

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:

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

       }



ChatClient上的代码是



The code on ChatClient is

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

}



在ChatServer中添加了对ChatClient的引用.我的意思是我在ChatServer项目中使用ChatClient参考添加.当我单击登录按钮时,出现异常,提示未将tat对象引用设置为实例.

请帮助



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

推荐答案

首先缺少一些代码,我确定它在您的项目中,但是为了能够给出准确的答案您需要提供以下内容.
flag在任何地方都没有定义,也没有在任何地方赋值.
由于我看不到标记的处理方式,因此我假设在login_btn_Click
中调用client.JoinChat(username);flag为true 在这种情况下,如果没有为事件分配处理程序,则Newjoin为null.因此,您需要检查是否为空.
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.
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);
}


将来有助于知道哪一行导致了错误.
照原样,由于错误发生在login_btn_Click方法中,因此必须是login_txt 为null(不太可能是文本框)或client.在该方法的第一行上放置一个断点,然后继续进行.我的猜测是您无法将客户端初始化为代码中其他位置的新实例.
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.


您是否尝试调试它?如果不是,那么我建议先这样做,您将能够指出哪个对象为null,这就是引发此异常的原因.顺便说一句,这条线在做什么?
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 ?
client.JoinChat(username);

实例化了客户端对象吗?究竟是什么对象?

干杯...

have you instantiated the client object?? what object exactly its meant to be?

Cheers...


这篇关于对象引用未设置为实例-事件和委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆