用C#写了一个程序,在WEB访问的时候出现以下错误,100分奖励!

wwwshowjicom 2003-09-13 04:34:51
“/code”应用程序中的服务器错误。
----------------------------------------------------

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

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

源错误:

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。

堆栈跟踪:


[NullReferenceException: 未将对象引用设置到对象的实例。]
ColorNote.book2_code2.Page_Load(Object sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +29
System.Web.UI.Page.ProcessRequestMain() +724




--------------------------------------------------------------------------------
版本信息: Microsoft .NET 框架版本:1.0.3705.0; ASP.NET 版本:1.0.3705.0
...全文
64 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwshowjicom 2003-09-14
  • 打赏
  • 举报
回复
下面是我程序的全部代码,WEB执行方法book.aspx?id=1002 然后执行后转到book2.aspx:
文件book.aspx代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ColorNote
{
/// <summary>
/// book 的摘要说明。
/// </summary>
public class book : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TBoxNum;
protected System.Web.UI.WebControls.Button BtnOk;
protected System.Web.UI.WebControls.Label L1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
L1.Visible = false;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.BtnOk.Click += new System.EventHandler(this.BtnOk_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void BtnOk_Click(object sender, System.EventArgs e)
{
clMmsUnion.SubscribeUser SU = new clMmsUnion.SubscribeUser();
Int64 tmpMobileNum = 0;
//获得用户ID
string tmpMasterID = (string)Request.QueryString["id"];
if( tmpMasterID == null)
tmpMasterID = "0";
Session["tmpMasterID"] = int.Parse(tmpMasterID);

if(TBoxNum.Text != "")
tmpMobileNum = Int64.Parse((string)TBoxNum.Text);

switch(SU.Register(tmpMobileNum))
{
case 0: // run ok
Session["MobileNum"] = (string)TBoxNum.Text;
Session["passVar"] = 0;
Response.Redirect("book2.aspx");
break;
case 2: // 非服务对象(例如:手机号码不是移动用户)
L1.Visible = true;
L1.Text = "请输入属于移动用户的手机号码!";
break;
case 3: // 用户已注册
Session["MobileNum"] = (string)TBoxNum.Text;
Session["passVar"] = 1;
Response.Redirect("book2.aspx");
break;
default: // 意外错误
L1.Visible = true;
L1.Text = "服务暂时出错,请稍后在试!";
break;
}
}
}
}


book2.aspx 的代码如下:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ColorNote
{
/// <summary>
/// book2 的摘要说明。
/// </summary>
public class book2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label LMobileNum;
protected System.Web.UI.WebControls.Label L1;
protected System.Web.UI.WebControls.TextBox TBoxPass;
protected System.Web.UI.WebControls.LinkButton LBtnGetPass;
protected System.Web.UI.WebControls.Button BtnOk;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
L1.Visible = false;
LMobileNum.Text = Session["MobileNum"].ToString();
if(int.Parse(Session["passVar"].ToString()) == 0)
{
Response.Write ("<script language=javascript>alert('欢迎您注册!密码已发送您的手机,请注意查收!')</script>");
}
else if(int.Parse(Session["passVar"].ToString()) == 1)
{
Response.Write ("<script language=javascript>alert('欢迎您!您是老用户,若忘记密码请点击重发密码!')</script>");
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.BtnOk.Click += new System.EventHandler(this.BtnOk_Click);
this.LBtnGetPass.Click += new System.EventHandler(this.LBtnGetPass_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void BtnOk_Click(object sender, System.EventArgs e)
{
clMmsUnion.SubscribeUser SU = new clMmsUnion.SubscribeUser();
Int64 tmpMobileNum = Int64.Parse(LMobileNum.Text.ToString());
string tmpPassWord = (string)TBoxPass.Text;
switch(SU.Login(tmpMobileNum,tmpPassWord))
{
case 0:
switch(SU.Subscribe(9,tmpMobileNum,tmpPassWord,(int)Session["tmpMasterID"],4))
{
case 0:
Response.Write("<script language=javascript>window.open('http://mms.tom.com')</script>");
break;
case 6:
L1.Visible = true;
L1.Text = "此业务您已订阅,不能重复订阅";
break;
default:
L1.Visible = true;
L1.Text = "服务暂时出错,请稍后再试";
break;
}
break;
case 4:
L1.Visible = true;
L1.Text = "密码不符,请重新输入!";
break;
case 8:
L1.Visible = true;
L1.Text = "记不住密码了嘛,那就明天再试吧!";
break;
default:
L1.Visible = true;
L1.Text = "服务暂时出错,请稍候在试";
break;
}
}

//重发密码
private void LBtnGetPass_Click(object sender, System.EventArgs e)
{
clMmsUnion.SubscribeUser SU = new clMmsUnion.SubscribeUser();
Int64 tmpMobileNum = Int64.Parse(LMobileNum.Text.ToString());
switch(SU.SendPassword(tmpMobileNum))
{
case 0:
Response.Write("<script language=javascript>alert('密码已重新发送到您的手机,请注意查收!')</script>");
break;
case 9:
Response.Write("<script language=javascript>alert('抱歉!您今天重取密码次数过多,请一天后再取!')</script>");
break;
default:
Response.Write("<script language=javascript>alert('服务暂时出错,请稍后再试!')</script>");
break;
}
}
}
}

如果放置在弹出窗口中代码执行正常,比如:
<script language=JavaScript>window.open('http://mms.tom.com/code/book.aspx?id=1002','book','width=298,height=310');</script>
如果用框架调用,则出现错误,比如:
<IFRAME align=center border=0 frameBorder=0 height=310 width=300 name=book src="http://mms.tom.com/code/book.aspx?id=1002</IFRAME>

错误如下:
“/code”应用程序中的服务器错误。
--------------------------------------------------------------------------------

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

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

源错误:

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。

堆栈跟踪:


[NullReferenceException: 未将对象引用设置到对象的实例。]
ColorNote.book2_code2.Page_Load(Object sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +29
System.Web.UI.Page.ProcessRequestMain() +724




--------------------------------------------------------------------------------
版本信息: Microsoft .NET 框架版本:1.0.3705.0; ASP.NET 版本:1.0.3705.0
lorna1996 2003-09-14
  • 打赏
  • 举报
回复
同意chainet的看法
你在同一页面中改变了Li.Visible,page_load中要判断;
另外,book2_code2的page_load是否用了session判断?这里肯定有问题!
layershow 2003-09-14
  • 打赏
  • 举报
回复
准备创业说的没错

Response.Redirect("book2_code2.aspx");
因为这句

请把这个页贴出来。让大家看看
它的Page_Load出错了

你用Session传参吧?
是不是Session名打错了?
layershow 2003-09-14
  • 打赏
  • 举报
回复
准备创业说的没错

Response.Redirect("book2_code2.aspx");
因为这句

请把这个页贴出来。让大家看看
它的Page_Load出错了

你用Session传参吧?
是不是Session名打错了?
wwwshowjicom 2003-09-14
  • 打赏
  • 举报
回复
还是不行,请教高手!
jjcccc 2003-09-14
  • 打赏
  • 举报
回复
先把book2.aspx.cs里的所有使用Session的地方都加一个是否为空的判断:
if (Session["..."]!=null
{
//...............
}
再看看有没有问题,或什么问题?
wwwshowjicom 2003-09-14
  • 打赏
  • 举报
回复
在线等待。
ahoo 2003-09-13
  • 打赏
  • 举报
回复
aspx 的页面内没有对象 L1
chainet 2003-09-13
  • 打赏
  • 举报
回复
上面第三行贴错了。应该是
ColorNote.book2_code2.Page_Load(Object sender, EventArgs e)
chainet 2003-09-13
  • 打赏
  • 举报
回复
错误应该不在这个类中,看清楚!:
[NullReferenceException: 未将对象引用设置到对象的实例。]
ColorNote..Page_Load(Object sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +29
System.Web.UI.Page.ProcessRequestMain() +724

是book2_code2这个类!
——Page_Load中的代码都不一样!
lkk2073 2003-09-13
  • 打赏
  • 举报
回复
L1在设计界面上吗????
lkk2073 2003-09-13
  • 打赏
  • 举报
回复
对...没有定义L1
9tl 2003-09-13
  • 打赏
  • 举报
回复
此处出错了吧!private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
L1.Visible = false;
}
changezhong 2003-09-13
  • 打赏
  • 举报
回复
这种问题一般是在代码中设断点跟踪,找到问题出在哪,然后在看自己的代码对不对
gj0001 2003-09-13
  • 打赏
  • 举报
回复
这样的错误不看你的代码,无法知道错在哪?这样的错误太普遍了。
jjcccc 2003-09-13
  • 打赏
  • 举报
回复
除了楼上王子所说,根据错误提示看,还有错误在Page_Load中,把Page_load中的代码贴出来...
wwwshowjicom 2003-09-13
  • 打赏
  • 举报
回复
还是不行啊?在IE地址栏里执行的是book.axpx?id=1024 然后提交到这个页面中。结果出现上述错误。
panyee 2003-09-13
  • 打赏
  • 举报
回复
string tmpMasterID = (string)Request.QueryString["id"];
是不是没有querystring??
作个判断
string tmpMasterID = "";
if(Request.QueryString["id"] == null)
{
//
}
else
{
tmpMasterID = Request.QueryString["id"].ToString();
}
wwwshowjicom 2003-09-13
  • 打赏
  • 举报
回复
帮帮我,谢谢了。
wwwshowjicom 2003-09-13
  • 打赏
  • 举报
回复
代码如下:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ColorNote
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1_code2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label L1;
protected System.Web.UI.WebControls.TextBox TBoxNum;
protected System.Web.UI.WebControls.Button BtnOk;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
L1.Visible = false;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.BtnOk.Click += new System.EventHandler(this.BtnOk_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void BtnOk_Click(object sender, System.EventArgs e)
{
clMmsUnion.SubscribeUser SU = new clMmsUnion.SubscribeUser();
Int64 tmpMobileNum = 0;
//获得用户ID
string tmpMasterID = (string)Request.QueryString["id"];
if( tmpMasterID == null)
tmpMasterID = "0";
Session["tmpMasterID_code2"] = int.Parse(tmpMasterID);

if(TBoxNum.Text != "")
tmpMobileNum = Int64.Parse((string)TBoxNum.Text);

switch(SU.Register(tmpMobileNum))
{
case 0: // run ok
Session["MobileNum_code2"] = (string)TBoxNum.Text;
Session["passVar_code2"] = 0;
Response.Redirect("book2_code2.aspx");
break;
case 2: // 非服务对象(例如:号码不是移动用户)
L1.Visible = true;
L1.Text = "请输入移动用户的手机!";
break;
case 3: // 用户已注册
Session["MobileNum_code2"] = (string)TBoxNum.Text;
Session["passVar_code2"] = 1;
Response.Redirect("book2_code2.aspx");
break;
default: // 意外错误
L1.Visible = true;
L1.Text = "服务暂时出错,请稍后在试!";
break;
}
}
}
}
加载更多回复(2)

110,499

社区成员

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

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

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