How can I fix the error: System.NullReferenceException: 'Object reference not set to an instance of an object.'

flowerforlife 56 Reputation points
2022-07-27T13:15:08.23+00:00

The following is my code:

namespace RegistrationForm  
{  
    public partial class Rent : Form  
    {  
        AptId aptId;  
        Payment payment;  
        public Rent(AptId apt)  
        {  
            InitializeComponent();  
            this.aptId = apt;  
              
        }  
  
        public Rent(Payment pay)  
        {  
            InitializeComponent();  
            this.payment = pay;  
        }  
  
        private void Rent_Load(object sender, EventArgs e)  
        {  
            rentTextBox.Text = aptId.rentTextBox.Text;  
            rentTextBox.Text = payment.rentTextBox.Text;  
        }  
        private void button1_Click(object sender, EventArgs e)  
        {  
            DateTime current = DateTime.Now;  
            MessageBox.Show("Your transaction on " + current.ToString() + " of BHD " + rentTextBox.Text + " was successful!!!");  
  
            this.Close();  
        }  
  
          
    }  
}  

I am getting error on this statement: rentTextBox.Text = aptId.rentTextBox.Text;

Error: System.NullReferenceException: 'Object reference not set to an instance of an object.'

Doesn't make sense to me cause the statement under it is the same just a different name and different table referencing name, but it doesn't show any such error.

I would really appreciate if my error is solved in any way.

Thanks in advance

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,921 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,270 questions
{count} votes

7 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 70,776 Reputation points
    2022-07-27T17:34:19.753+00:00

    in the line:

    rentTextBox.Text = aptId.rentTextBox.Text;

    the error means one of the objects is null (aptId) in this case. you have a basic logic error. the code:

             private void Rent_Load(object sender, EventArgs e)  
             {  
                 rentTextBox.Text = aptId.rentTextBox.Text;  
                 rentTextBox.Text = payment.rentTextBox.Text;  
             }  
    

    assume both aptId and payment are defined, but no code path does this. Your constructors only initial one of them, so no matter which constructor you call. as you are writing to the same field you only wanted to execute one f the statements:

                 private void Rent_Load(object sender, EventArgs e)  
                 {  
                     if (aptId != null)  
                         rentTextBox.Text = aptId.rentTextBox.Text;  
                     else if (payment != null)  
                         rentTextBox.Text = payment.rentTextBox.Text;  
                     else  
                         rentTextBox.Text = "constructor called with null value";  
                 }  
    
    0 comments No comments

  2. ShuaiHua Du 636 Reputation points
    2022-07-27T18:01:23.427+00:00

    Add null value check to your Rent_load():

    private void Rent_Load(object sender, EventArgs e)  
    {  
    	if(aptId != null && aptId.rentTextBox != null)  
    	{  
    		rentTextBox.Text = aptId.rentTextBox.Text;  
    	}  
    	if(aptId != null && payment.rentTextBox != null)  
    	{  
    		rentTextBox.Text = payment.rentTextBox.Text;  
    	}  
    }  
      
    

    If right,please Accept.
    Enjoy programming!!!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.