Reputation:
I'm really new to coding, so I don't know how to fix this error:
$mcs *.cs -out:main.exe $mono main.exe Insert the first number:
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 RectangleVolume.Program.Main () [0x0000f] in <0c8d91594f2e4f869d1e8405ed48fd66>: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 RectangleVolume.Program.Main () [0x0000f] in <0c8d91594f2e4f869d1e8405ed48fd66>:0
Here is the code:
using System;
namespace RectangleVolume
{
class Program
{
static void Main()
{
int number1;
int number2;
int volume;
int two;
int zero;
Console.Write("Insert the first number: ");
number1 = int.Parse(Console.ReadLine());
Console.Write("Insert the second number: ");
number2 = int.Parse(Console.ReadLine());
two = 2;
two = int.Parse(Console.ReadLine());
zero = 0;
zero = int.Parse(Console.ReadLine());
if (number1 < zero)
{
Console.WriteLine("First number is not a positive");
}
else
{
Console.WriteLine(number1 * two);
}
if (number2 < zero)
{
Console.WriteLine("second number is not a positive");
}
else
{
Console.WriteLine(number2 * two);
}
volume = number1 + number2;
Console.Write("The Volume is: ");
Console.WriteLine(volume);
Console.ReadLine();
}
}
}
If anyone knows how to fix the error I would appreciate your help, ty
Upvotes: 2
Views: 4319
Reputation: 186833
Do not repeat yourself: let's extract a method:
using System.Globalization;
...
private static int ReadNonNegative(string title) {
int result = 0;
while (true) do {
Console.Write($"Insert the {title} number: ");
if (!int.TryParse(Console.ReadLine(), out result)) {
Console.WriteLine();
Console.WriteLine("Syntax error. Not a valid integer value. Please, try again.");
}
else if (result < 0) {
Console.WriteLine();
Console.WriteLine($"{CultureInfo.CurrentCulture.TextInfo.ToTitleCase(title)} number must not be negative.");
}
else
return result;
}
}
Then we can easily use it
static void Main() {
int number1 = ReadNonNegative("first");
int number2 = ReadNonNegative("second");
Console.WriteLine($"{number1 * 2}");
Console.WriteLine($"{number2 * 2}");
int volume = number1 + number2;
Console.Write($"The Volume is: {volume}");
Console.ReadLine();
}
Upvotes: 4