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

flowerforlife 51 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,894 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.
10,962 questions
{count} votes

7 answers

Sort by: Most helpful
  1. Rijwan Ansari 751 Reputation points MVP
    2022-07-27T14:28:13.477+00:00

    Hi @flowerforlife

    You can check null value before assigning value as shown:

     rentTextBox.Text = aptId?.rentTextBox?.Text ?? "Null Value";  
     rentTextBox.Text = payment?.rentTextBox?.Text ?? "Null Value";  
    
    2 people found this answer helpful.
    0 comments No comments

  2. Sreeju Nair 12,351 Reputation points
    2022-07-27T13:59:23.367+00:00

    I believe you have null value in any one of the following variables.

    • rentTextBox
    • aptId
    • aptId.rentTextBox

    Check the call to the constructor Rent(AptId) and ensure the values are passing correctly. Insert a breakpoint and debug your application.

    Hope this helps

    0 comments No comments

  3. satya karki 986 Reputation points MVP
    2022-07-27T14:34:18.103+00:00

    Hi,

    I believe that the appId or appid.rentTextBox is having null in below method.

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

    You can rewrite those by checking null.

     private void Rent_Load(object sender, EventArgs e)  
         {  
             rentTextBox.Text = (aptId.rentTextBox != null) ? aptId.rentTextBox.Text : "Null" ;  
             rentTextBox.Text = (payment.rentTextBox !=null) ? payment.rentTextBox.Text : "Null";  
         }  
    

    However, please double check those objects.

    0 comments No comments

  4. Reza Aghaei 4,951 Reputation points MVP
    2022-07-27T15:14:57.27+00:00

    When you have a statement like A.B.C = E.F;, and you receive an NullReferenceException 'Object reference not set to an instance of an object.' on that line of code, it basically means either A, or B or E are null.

    To figure out what's wrong there, you can set a breakpoint on the same line, start debugging and then when the breakpoint hits, just check the value of A, B or E.

    When NullReferenceException happens?

    There are some common cases that you may encounter a NullReferenceException. Here is a summary based on NllReferenceEexception article in .NET documentations. I've put the link of the article in the next section in the answer, where you can find some useful example as well.

    A NullReferenceException exception is thrown when you try to access a member on a type whose value is null. A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios:

    • You've forgotten to instantiate a reference type.
    • You've forgotten to dimension an array before initializing it.
    • You get a null return value from a method, and then call a method on the returned type.
    • You're using an expression to retrieve a value and one of the intermediate values in the expression returns null.
    • You're enumerating the elements of an array that contains reference types, and an element is null.
    • A NullReferenceException exception is thrown by a method that is passed null.

    Learn more

    For more information and examples, take a look at the following links:

    0 comments No comments

  5. Karen Payne MVP 35,426 Reputation points
    2022-07-27T16:43:14.703+00:00

    Rather than guess, set breakpoints and examine variable values via the debugger. Once known what is causing System.NullReferenceException then figure out how to fix it and/or prevent it in the first place.

    • It's a great time to get to know breakpoints, working with local window and the debugger in general.
    • Another option is to place this code into a class than at this point unit test can be written against the code which can do two things, expose issues and tighten up your code overall.
    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.