吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 601|回复: 6
收起左侧

[已解决] asp.net 未将对象引用设置到对象的实例。

[复制链接]
marcus1982 发表于 2022-8-11 20:34
本帖最后由 marcus1982 于 2022-8-11 22:14 编辑

前台:
[HTML] 纯文本查看 复制代码
<%@ 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>

后台:
[C#] 纯文本查看 复制代码
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

堆栈跟踪:

[NullReferenceException: 未将对象引用设置到对象的实例。]   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

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

不忘初心哟 发表于 2022-8-11 20:54
public StudentInfo EditStudentInfo { set; get; } 这个默认是空 相当于是null null.StudentId 相当于空指针 就会报未将对象引用到实例
李玉风我爱你 发表于 2022-8-11 20:56
 楼主| marcus1982 发表于 2022-8-11 21:27
不忘初心哟 发表于 2022-8-11 20:54
public StudentInfo EditStudentInfo { set; get; } 这个默认是空 相当于是null null.StudentId 相当于空指 ...

好像明白了点,请问如何避免这个情况
 楼主| marcus1982 发表于 2022-8-11 21:30
李玉风我爱你 发表于 2022-8-11 20:56
断点看看 29行的 studentInfo是什么?

第一次载入页面显示:
{System.Data.Entity.DynamicProxies.StudentInfo_603A87C76AC597BEDC599A5AC96267CAF6D0DC292EBAC8AE44321A5D7DA71E2E}
    [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"
当修改后,点击确认:
就直接报错了
gongjing457 发表于 2022-8-11 21:59
第29行 StudentInfo studentInfo = db.StudentInfo.SingleOrDefault(S => S.StudentId == studentId);
从数据库查到的有内容吗?
空指针说明studentInfo是空的
 楼主| marcus1982 发表于 2022-8-11 22:13
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");就没问题了。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-31 18:16

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表