Cross Page:System.NullReferenceException:未将对象引用设置为对象的实例。 [英] Cross Page: System.NullReferenceException: Object reference not set to an instance of an object.

查看:73
本文介绍了Cross Page:System.NullReferenceException:未将对象引用设置为对象的实例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

在登录页面:

My Code:
In Login Page:

public string EmailAddress
        {
            get
            {
                return emailAddress;
            }
            set
            {
                emailAddress = value;
            }
        }
        string emailAddress;

protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
        {
            Login ob = new Login();
            ob.EmailAddress = Login1.UserName.ToString();
            try
            {
                db.Adapter("select COUNT(EmailAddress) from tblLogin where EmailAddress = '" + Login1.UserName.ToString() + "' and Password ='" + Login1.Password.ToString() + "'", "student");
                if (db.dataSet.Tables["student"].Rows[0][0].ToString() == "1")
                {
                    
                    Page.Response.Redirect("UploadJournal.aspx");
                    
                }
                else
                {
                    db.Adapter("select COUNT(EmailAddress) from tblTeacherLogin where EmailAddress = '" + Login1.UserName.ToString() + "' and Password ='" + Login1.Password.ToString() + "'", "teacher");
                    if (db.dataSet.Tables["teacher"].Rows[0][0].ToString() == "1")
                    {
                        Page.Response.Redirect("Teacher.aspx");
                    }
                }
            }
            catch (Exception err)
            {
            }
        }





在UploadJournal.aspx文件中:



In UploadJournal.aspx file:

protected void Page_Load(object sender, EventArgs e)
        {
            if (PreviousPage.EmailAddress.ToString() != null)
            {
                Label1.Text = PreviousPage.EmailAddress;
            }
        }





错误:







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



来源错误:





第20行:protected void Page_Load(object sender,EventArgs e )

第21行:{

第22行:if(PreviousPage.EmailAddress.ToString()!= null)

第23行:{

第24行:Label1.Text = PreviousPage.EmailAddress;




请告诉我如何解决这个问题?



Error:



Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 20: protected void Page_Load(object sender, EventArgs e)
Line 21: {
Line 22: if (PreviousPage.EmailAddress.ToString() != null)
Line 23: {
Line 24: Label1.Text = PreviousPage.EmailAddress;


Please tell me how can I solve this problem ?

推荐答案

此异常是最容易检测和修复的案例之一。它只是意味着某些引用类型的某个成员/变量通过使用和它的实例(非静态)成员解除引用,这要求此成员/变量为非null,但实际上它似乎为null。只需在调试器下执行它,它将停止抛出异常的执行。在该行上设置一个断点,重新启动应用程序并再次到达这一点。评估下一行中涉及的所有引用,并查看哪一个为null,而不需要为null。解决这个问题之后,修复代码:确保将成员/变量正确初始化为非空引用,或者将其检查为null,如果为null,则执行其他操作。



在您的情况下, PreviousPage 可以为null,或 PreviousPage.EmailAddress



参见:要显示按钮点击下一个记录。但得到了在如果下一个记录功能的病症对象引用不设置为一个对象的一个​​实例 [ ^ ]。



-SA
This exception is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

In your case, PreviousPage could be null, or PreviousPage.EmailAddress.

See also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

—SA


这篇关于Cross Page:System.NullReferenceException:未将对象引用设置为对象的实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆