I have a windows form application which allows a user to enter the customer details to textboxes and adds them to a Customer object instance from another class. Currently, when the "Add Details" button is clicked a System.NullException error is thrown as the phone number is validated in the Customer Class. It seems like the textboxes aren't recognising that any text has been entered. The code is below.
private void addCustomerDetailsButton_Click(object sender, EventArgs e)
{
string firstName = firstNameTextBox.Text;
string lastName = lastNameTextBox.Text;
string address = AddressTextBox.Text;
string phoneNo = customerPhoneTextBox.Text;
customer = new Customer(firstName, lastName, address, phoneNo);
}
Edit: Including code from the Customer Class. The exception is being thrown when the PhoneNo property is being validated.
public class Customer
{
public string FirstName { get; }
public string LastName { get; }
public string Address { get; }
private string phoneNo;
//four parameter constructor
public Customer(string firstName, string lastName, string address, string phoneNo)
{
FirstName = firstName;
LastName = lastName;
Address = address;
PhoneNo = phoneNo;
}
//propterty that gets and sets the phone number
public string PhoneNo
{
get
{
return phoneNo;
}
set
{
if (phoneNo.Length != 10)
{
throw new ArgumentOutOfRangeException(nameof(value), value,
$"{nameof(PhoneNo)} must be 10 digits long");
}
phoneNo = value;
}
}