0

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

  • Perhaps, `Console.ReadLine()` returns null, because you don't enter anything? – Yeldar Kurmangaliyev Oct 22 '18 at 10:24
  • @Barr I believe that it's not. While the cause of both exceptions are unexpected `null`, these are two different exceptions. One of them is thrown by CLR, and another one is thrown by a developer on purpose. – Yeldar Kurmangaliyev Oct 22 '18 at 10:26
  • its not a null reference exception, – TheGeneral Oct 22 '18 at 10:27
  • 5
    Dont use `int.Parse` to parse user input (**EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER** **EVER EVER EVER EVER EVER**), use `int.TryParse` ... fix that and you fixed your error – TheGeneral Oct 22 '18 at 10:28
  • Why are you setting `two`, then setting it again to the result of reading from the console? – Tom W Oct 22 '18 at 10:30
  • @TheGeneral Or any other `.Parse()`, if there is `.TryParse()`. – SᴇM Oct 22 '18 at 10:32
  • 1
    @SeM indeed, too right – TheGeneral Oct 22 '18 at 10:33

1 Answers1

2

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();
   } 
Dmitry Bychenko
  • 149,892
  • 16
  • 136
  • 186