Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My Code:
In Login Page:
C#
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)
            {
            }
        }


In UploadJournal.aspx file:
C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (PreviousPage.EmailAddress.ToString() != null)
            {
                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 ?
Posted

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900