You can check null value before assigning value as shown:
rentTextBox.Text = aptId?.rentTextBox?.Text ?? "Null Value";
rentTextBox.Text = payment?.rentTextBox?.Text ?? "Null Value";
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
You can check null value before assigning value as shown:
rentTextBox.Text = aptId?.rentTextBox?.Text ?? "Null Value";
rentTextBox.Text = payment?.rentTextBox?.Text ?? "Null Value";
I believe you have null value in any one of the following variables.
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
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.
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.
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:
Learn more
For more information and examples, take a look at the following links:
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.