asp.net 未将对象引用设置到对象的实例。
本帖最后由 marcus1982 于 2022-8-11 22:14 编辑前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StudentEdit.aspx.cs" Inherits="考试系统.teacher.StudentEdit" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
学号:<input type="text" name="txtStudentId" value="<%=EditStudentInfo.StudentId %>" /><br />
姓名:<input type="text" name="txtStudentname" value="<%=EditStudentInfo.StudentName %>" /><br />
<input type="submit" value="编辑确认" />
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 考试系统.teacher
{
public partial class StudentEdit : System.Web.UI.Page
{
kaoshidbEntities db = new kaoshidbEntities();
public StudentInfo EditStudentInfo { set; get; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowEditStundenInfo();
}
else
{
UpdateStudentInfo();
}
}
protected void ShowEditStundenInfo()
{
string studentId = Request.QueryString["studentid"].ToString();
StudentInfo studentInfo = db.StudentInfo.SingleOrDefault(S => S.StudentId == studentId);
if (studentInfo != null)
{
EditStudentInfo = studentInfo;
}
else
{
Response.Redirect("/MyErrorPage.aspx");
}
}
protected void UpdateStudentInfo()
{
string studentId = Request.QueryString["studentid"].ToString();
StudentInfo studentInfo = db.StudentInfo.SingleOrDefault(S => S.StudentId == studentId);
if (studentInfo != null)
{
studentInfo.StudentId = Request.Form["txtStudentId"].ToString();
studentInfo.StudentName = Request.Form["txtStudentname"].ToString();
}
int r = db.SaveChanges();
if (r > 0)
{
Response.Write("<script languge='javascript'>alert('添加成功!');window.location.href='StudentManage.aspx'</script>");
}
else
{
Response.Redirect("/MyErrorPage.aspx");
}
}
}
}
提交后,出现以下错误,但是查看数据库后,修改也能成功。新手求救。
“/”应用程序中的服务器错误。未将对象引用设置到对象的实例。说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 11: <form id="form1" runat="server">行 12: <div>行 13: 学号:<input type="text" name="txtStudentId" value="<%=EditStudentInfo.StudentId %>" /><br />行 14: 姓名:<input type="text" name="txtStudentname" value="<%=EditStudentInfo.StudentName %>" /><br />行 15:
源文件: c:\kaoshi2022\考试系统\考试系统\teacher\StudentEdit.aspx 行: 13
堆栈跟踪:
ASP.teacher_studentedit_aspx.__Renderform1(HtmlTextWriter __w, Control parameterContainer) in c:\kaoshi2022\考试系统\考试系统\teacher\StudentEdit.aspx:13 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +103 System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +177 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32 System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +360 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +128 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +287 System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +53 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +197 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +9 System.Web.UI.Page.Render(HtmlTextWriter writer) +30 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +128 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +287 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +27 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5628
版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.8.4494.0 public StudentInfo EditStudentInfo { set; get; } 这个默认是空 相当于是null null.StudentId 相当于空指针 就会报未将对象引用到实例 断点看看 29行的 studentInfo是什么? 不忘初心哟 发表于 2022-8-11 20:54
public StudentInfo EditStudentInfo { set; get; } 这个默认是空 相当于是null null.StudentId 相当于空指 ...
好像明白了点,请问如何避免这个情况 李玉风我爱你 发表于 2022-8-11 20:56
断点看看 29行的 studentInfo是什么?
第一次载入页面显示:
{System.Data.Entity.DynamicProxies.StudentInfo_603A87C76AC597BEDC599A5AC96267CAF6D0DC292EBAC8AE44321A5D7DA71E2E}
: {System.Data.Entity.DynamicProxies.StudentInfo_603A87C76AC597BEDC599A5AC96267CAF6D0DC292EBAC8AE44321A5D7DA71E2E}
ExamStudent: Count = 5
StudentClass: {System.Data.Entity.DynamicProxies.StudentClass_7A9923E5D75ABD30E8E924195BB9FB298B712AF8102178D26296EAF8EE7D713A}
StudentClassId: 1
StudentGender: "女"
StudentId: "1001"
StudentJieDa: Count = 1
StudentName: "张11112"
StudentPwd: "111"
当修改后,点击确认:
就直接报错了 第29行 StudentInfo studentInfo = db.StudentInfo.SingleOrDefault(S => S.StudentId == studentId);
从数据库查到的有内容吗?
空指针说明studentInfo是空的 gongjing457 发表于 2022-8-11 21:59
第29行 StudentInfo studentInfo = db.StudentInfo.SingleOrDefault(S => S.StudentId == studentId);
从 ...
能查到内容,我把Response.Write("<script languge='javascript'>alert('添加成功!');window.location.href='StudentManage.aspx'</script>");改成Response.Redirect("StudentManage.aspx");就没问题了。
页:
[1]