using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace real_state
{
class Address
{
//Data fields
private string street;
private string city;
private string province;
private string zipCode;
//Properties
public string Street
{
get => street;
set => street = value;
}
public string City
{
get => city;
set => city = value;
}
public string Province
{
get => province;
set => province = value;
}
public string ZipCode
{
get => zipCode;
set => zipCode = value;
}
/*no-arg constructor that creates a default Address*/
public Address()
{
street = "";
city = "";
zipCode = "";
province = "";
}
/*constructor that creates an address with the specified street, city, state, and zipCode*/
public Address(string street, string city, string province, string zipCode)
{
this.street = street;
this.city = city;
this.province = province;
this.zipCode = zipCode;
}
public override string ToString()
{
return "Street: " + street + "\nCity: " + city + "\nState: " + province + "\nZipCode: " + zipCode;
}
}//end of Address class
class Residence
{
//Data fields:
private int bedrooms;
private int bathrooms;
private double price;
private double squareFeet;
private int yearBuilt;
private Address address;
//Properties
public int Bedrooms
{
get => bedrooms;
set => bedrooms = value;
}
public int Bathrooms
{
get => bathrooms;
set => bathrooms = value;
}
public double Price
{
get => price;
set => price = value;
}
public double SquareFeet
{
get => squareFeet;
set => squareFeet = value;
}
public int YearBuilt
{
get => yearBuilt;
set => yearBuilt = value;
}
public Address Address
{
get => address;
set => address = value;
}
// no-arg constructor that creates a default Residence.
public Residence()
{
bedrooms = 0;
bathrooms = 0;
price = 0;
squareFeet = 0;
yearBuilt = 0;
address = new Address();
}
/*constructor that creates a Residence with the specified bedrooms,
* bathrooms, price, squareFeet, yearBuilt and address.*/
public Residence(int bedrooms, int bathrooms, double price, double squareFeet, int yearBuilt, Address address)
{
this.bedrooms = bedrooms;
this.bathrooms = bathrooms;
this.price = price;
this.squareFeet = squareFeet;
this.yearBuilt = yearBuilt;
this.address = address;
}
/*returns the commission amount. The commission rate is set up at 2%.
*/
public double CalculateCommission()
{
return price * 0.02;
}
public override string ToString()
{
return "Number of Bedrooms: " + bedrooms + "\nNumber of Bathrooms: " + bathrooms
+ "\nPrice: " + price.ToString("C") + "\nSquare Feet: " + squareFeet +
"\nYear Built: " + yearBuilt + "\nAddress: " + address;
}
}
//End of Residence class
class Program
{
static void Main(string[] args)
{
// demonstrates the Address class
//Prompts the user to enter the street name
Console.Write("Enter Street Name: ");
string street = Console.ReadLine();
//Prompts the user to enter city name
Console.Write("Enter City Name: ");
string city = Console.ReadLine();
//Prompts the user to enter the province name
Console.Write("Enter Province Name: ");
string province = Console.ReadLine();
//Prompts the user to enter the zip code
Console.Write("Enter ZipCode Name: ");
string zipcode = Console.ReadLine();
//Creates an instance called testAddress
Address testAddress = new Address(street, city, province, zipcode);
//Displays this information using ToString().
Console.WriteLine(testAddress);
//==========================================================
///*prompt the user to enter the numbers of bedrooms
// * and bathrooms, price, square Feet and year built.
// * */
Console.Write("Enter Number Of Bedrooms: ");
int bedroom = int.Parse(Console.ReadLine());
Console.Write("Enter Number Of Bathrooms: ");
int bathrooms = int.Parse(Console.ReadLine());
Console.Write("Enter years built: ");
int year = int.Parse(Console.ReadLine());
Console.Write("Enter price: ");
double price = double.Parse(Console.ReadLine());
Console.Write("Enter square feet: ");
double sqft = double.Parse(Console.ReadLine());
//Create an instance of the residence class with the entered data
Residence residence = new Residence(bedroom, bathrooms, price, sqft, year, testAddress);
//Display the information about the residence using ToString().
Console.WriteLine("\nResidence\n" + residence);
Console.WriteLine("Commission: " + residence.CalculateCommission().ToString("C"));
//pause
Console.WriteLine("Enter continue to terminate...");
Console.ReadLine();
}
}
}
After running the code its shows argumentnullexception error. I would like to know where I went wrong since am new to this language. I have tried .TryParse but did not work for me. If you would recommend proper solutions that can work I will appreciate. The error is shown below
$mcs *.cs -out:main.exe $mono main.exe Enter Street Name: Enter City Name: Enter Province Name: Enter ZipCode Name: Street: City: State: ZipCode: Enter Number Of Bedrooms: Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: String at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00003] in <902ab9e386384bec9c07fa19aa938869>:0 at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00015] in <902ab9e386384bec9c07fa19aa938869>:0 at System.Int32.Parse (System.String s) [0x00007] in <902ab9e386384bec9c07fa19aa938869>:0 at real_state.Program.Main (System.String[] args) [0x00061] in <9ffe6a983e2f4da38c46a8d2bda39011>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentNullException: Value cannot be null. Parameter name: String at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00003] in <902ab9e386384bec9c07fa19aa938869>:0 at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00015] in <902ab9e386384bec9c07fa19aa938869>:0 at System.Int32.Parse (System.String s) [0x00007] in <902ab9e386384bec9c07fa19aa938869>:0 at real_state.Program.Main (System.String[] args) [0x00061] in <9ffe6a983e2f4da38c46a8d2bda39011>:0