未将对象引用设置到对象的实例????高手请进

linuxuer 2003-10-10 12:41:33
建立一个button名字为Button1,建立事件 Button1_Click,

private void Button1_Click(object sender, System.EventArgs e)
{
int CurrentPageNum=0;
try
{
CurrentPageNum=threads.CurrentPageIndex;
threads.CurrentPageIndex=Convert.ToInt32(TBPageNum.Text);
BindData();
}
catch
{
threads.CurrentPageIndex=CurrentPageNum;
BindData();
}
}


运行时,出现错误提示:未将对象引用设置到对象的实例。
什么意思?


具体出错信息如下:

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 173: this.threads.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.threads_PageIndexChanged_1);
行 174: this.threads.SelectedIndexChanged += new System.EventHandler(this.threads_SelectedIndexChanged);
行 175: this.Button1.Click += new System.EventHandler(this.Button1_Click);
行 176: this.Load += new System.EventHandler(this.Page_Load);
行 177:


源文件: G:\web\bbs2\display2.aspx.cs 行: 175

堆栈跟踪:


[NullReferenceException: 未将对象引用设置到对象的实例。]
Project1.display2.InitializeComponent() in G:\web\bbs2\display2.aspx.cs:175
Project1.display2.OnInit(EventArgs e) in G:\web\bbs2\display2.aspx.cs:161
System.Web.UI.Control.InitRecursive(Control namingContainer) +241
System.Web.UI.Page.ProcessRequestMain() +174




--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573



谢谢高手回答
...全文
48 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhbname 2003-10-14
  • 打赏
  • 举报
回复
將這句中的this.threads.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.threads_PageIndexChanged_1);括號內容改為
:this.threads_PageIndexChanged
vikey 2003-10-14
  • 打赏
  • 举报
回复
BindData()是怎么写的
honghaozi 2003-10-14
  • 打赏
  • 举报
回复
和winform不同,webform实际并不会保存数据。实际上,每次提交之后,都会生成一个新的实例,只不过微软处理得比较巧妙,看以来象是同一个实例。
所以,数据(包括控件的状态)都是以session或viewstat的方式在页面往返时传递的。

把你的数据源存放在session里,页面刷新时取出,赋给一个新的数据源,然后把datagrid绑定到新的数据源。否则数据源就空了。
saucer 2003-10-10
  • 打赏
  • 举报
回复
>>>建立一个button名字为Button1

how did you create the button? did you use

Button1 = new Button();
Form1.Controls.Add(Button1);

or you have Button1 in your page?
linuxuer 2003-10-10
  • 打赏
  • 举报
回复
是在用asp.net
threads是一个dataGrid
请红耗子再解释一下
谢谢

wideroad 2003-10-10
  • 打赏
  • 举报
回复
奇怪的问题
honghaozi 2003-10-10
  • 打赏
  • 举报
回复
你是不是用asp.net?
如果是,数据(应该是个dataset之类的吧?)是不会在页面往返时保持的。
我一般是在session中保持数据
linuxuer 2003-10-10
  • 打赏
  • 举报
回复
yeah,button1 is in my page,i drag it to my page.

then i double click on the button,

enter the .cs page

then code as before

then test

then error

thanks

110,571

社区成员

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

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

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