未将对象引用设置到对象的实例

rocker1218 2006-01-11 01:58:55
类文件:
public class BzAgent
{
#region 字段
public string bzServerIP;
public int bzPort;
public string bzUserName;
public string bzUserPwd;

public TcpCli _bzClient;
private bool _isLoginBz=false;

#endregion

//构造函数
public BzAgent(string bzServerIP,int bzPort,string bzUserName,string bzUserPwd)
{
this.bzServerIP = bzServerIP;
this.bzPort = bzPort;
this.bzUserName = bzUserName;
this.bzUserPwd = bzUserPwd;

//创建与BZ的连接客户端对象
_bzClient = new TcpCli();
//订阅事件
_bzClient.ConnectedServer += new NetEvent(_bzClient_ConnectedServer);
_bzClient.DisConnectedServer +=new NetEvent(_bzClient_DisConnectedServer);
_bzClient.ReceivedDatagram +=new NetEvent(_bzClient_ReceivedDatagram);
//启动连接
_bzClient.Connect(this.bzServerIP,this.bzPort);
}
public BzAgent()
{
//订阅事件
//触发异常:未将对象引用设置到对象的实例
_bzClient.SendLocationServer += new NetEvent(_BzLocationSendAgent);
_bzClient.ReceivedDatagram +=new NetEvent(_bzClient_ReceivedDatagram);
}

主页面:
(page_load)
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
BzAgent ba= new BzAgent("10.0.2.26",6000,"010101","1234");
}
(btnSend_Click)
private void btnSend_Click(object sender, System.EventArgs e)
{
BzAgent mLocation= new BzAgent();
}
小弟新学c#.net,请问:_bzClient对象怎么才能不为空?
...全文
128 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
rocker1218 2006-01-12
  • 打赏
  • 举报
回复
66
rocker1218 2006-01-11
  • 打赏
  • 举报
回复
我需要 _bzClient对象在page_load中获取的一些属性的值。请问,有什么好的办法吗?
rocker1218 2006-01-11
  • 打赏
  • 举报
回复
可是我又不能从新创建一个对象。
feiyun0112 2006-01-11
  • 打赏
  • 举报
回复
public BzAgent()
{
//创建与BZ的连接客户端对象
_bzClient = new TcpCli();
//订阅事件
_bzClient.SendLocationServer += new NetEvent(_BzLocationSendAgent);
_bzClient.ReceivedDatagram +=new NetEvent(_bzClient_ReceivedDatagram);
}
hm7921936 2006-01-11
  • 打赏
  • 举报
回复
改正就是在你声明对象的时候把 你自己定义的参数传进去就可以了
BzAgent mLocation= new BzAgent();括号里面传参数.
time_is_life 2006-01-11
  • 打赏
  • 举报
回复
mark
rocker1218 2006-01-11
  • 打赏
  • 举报
回复
能在详细些吗?问题是明白了。就是不知道怎么改进。
Eddie005 2006-01-11
  • 打赏
  • 举报
回复
1楼正解,两个构造函数,只会运行一个,因此public BzAgent()里面缺实例化_bzClient的代码,不是改个方法名的问题。。。
rocker1218 2006-01-11
  • 打赏
  • 举报
回复
public BzAgent()改称 public SucBzAgent()也是不行。_bzClient还是沒有實例化
hyj_828 2006-01-11
  • 打赏
  • 举报
回复
你的構造函數是重載的?所以你下面這個_bzClient是沒有實例化的。
public BzAgent()
public BzAgent(string bzServerIP,int bzPort,string bzUserName,string bzUserPwd)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧