https://www.yljianzhan.com/安装了个聊天小程序,用visual studio 2017测试运行后,显示错误:
未将对象引用设置到对象的实例。
本人初学,什么语法也不懂!! 谁要这些小网站程序块,联系我!qq:466074134
原主页聊天代码:(https://yljianzhan.com,
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
int P_int_current = Convert.ToInt32(Application["current"]);
Application.Lock();
if (P_int_current == 0 || P_int_current > 20)
{
P_int_current = 0;
Application["chats"] = Session["userName"].ToString() + "说:" + txtMessage.Text.Trim() + "(" + DateTime.Now.ToString() + ")";
}
else
{
Application["chats"] = Application["chats"].ToString() + "," + Session["userName"].ToString() + "说:" + txtMessage.Text.Trim() + "(" + DateTime.Now.ToString() + ")";
}
P_int_current += 1;
Application["current"] = P_int_current;
Application.UnLock();
}
protected void btnExit_Click(object sender, EventArgs e)
{
Application.Lock();
string P_str_userName = Application["user"].ToString();
Application["user"] = P_str_userName.Replace(Session["userName"].ToString(), "");
Application.UnLock();
Response.Write("");
}
}
以下是运行后错误显示:
“/”应用程序中的服务器错误。
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 28: {
行 29: P_int_current = 0;
行 30: Application["chats"] = Session["userName"].ToString() + "说:" + txtMessage.Text.Trim() + "(" + DateTime.Now.ToString() + ")";
行 31: }
行 32: else
源文件: c:\Users\Administrator\source\repos\07\Default.aspx.cs 行: 30
堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
_Default.btnSend_Click(Object sender, EventArgs e) in c:\Users\Administrator\source\repos\07\Default.aspx.cs:30
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9782354
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.7.3190.0
QQ:46607414
请问了老师,修改了代码,好了,还要在(或服务器)iis管理器中添加你的网站首页,并重新启动IIS:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int P_int_judge = 0;
P_int_judge = Convert.ToInt32(Request["value"]);
if (!IsPostBack)
{
if (P_int_judge == 1)
Response.Write("");
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
Application.Lock();
int P_int_num; //在线人数
string P_str_name; //登录用户
string P_str_names; //已在线的用户名
string[] P_str_user; //用户在线数组
P_int_num = int.Parse(Application["userNum"].ToString());
if (TextBox1.Text == "")
{
Response.Write("");
TextBox1.Focus();
}
else
{
P_str_name = TextBox1.Text.Trim();
P_str_names = Application["user"].ToString();
P_str_user = P_str_names.Split(',');
for (int i = 0; i <= P_int_num - 1; i++)
{
if (P_str_name == P_str_user[i].Trim())
{
int P_int_judge = 1;
Response.Redirect("Login.aspx?value=" + P_int_judge);
}
}
if (P_int_num == 0)
Application["user"] = P_str_name.ToString();
else
Application["user"] = Application["user"] + "," + P_str_name.ToString();
P_int_num += 1;
Application["userNum"] = P_int_num;
Session["userName"] = TextBox1.Text.Trim();
Application.UnLock();
Response.Redirect("Default.aspx");
}
}
protected void btnExit_Click(object sender, EventArgs e)
{
Response.Write("");
}
}
————————————————
版权声明:本文为CSDN博主「anyong2016」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/anyong2016/article/details/104349694