-3

I am trying to run some code, it worked smoothly before, but now it says System.ArgumentNullExpection has been thrown.

I have tried running the code but that message now always appears

Console.Write("Mata in antal mjökpaket som är kvar: ");
            int mjölkpaket = int.Parse(Console.ReadLine());

            if (mjölkpaket < 10)
            {
                Console.WriteLine("Beställ 30 paket");
            }
            else if (mjölkpaket >= 10 && mjölkpaket <= 20)
            {
                Console.WriteLine("Beställ 20 paket");
            }
            else
            {
                Console.WriteLine("Behöver inte beställa någon mjölk");
            }

            Console.WriteLine();
            Console.WriteLine("Tryck på enter för att avsluta");
            Console.ReadLine();
d219
  • 2,707
  • 5
  • 31
  • 36
Tony Tech
  • 11
  • 3
  • 2
    On what line does the error occur? None of these look to me to be likely candidates for ArgumentNullException in most cases*, but int.Parse() might throw a FormatException if what the user types cannot be converted to int. *apparently ReadLine can return null if ctrl-z is pressed – Caius Jard Aug 08 '19 at 21:06
  • Have you tried to rebuild your whole solution? – Phil1970 Aug 08 '19 at 22:11

1 Answers1

0

This code works on my end. Remember to input a valid integer number in Console.ReadLine().

using System;

public class Program
{
    public static void Main()
    {
    Console.Write("Mata in antal mjökpaket som är kvar: ");
    int mjölkpaket = int.Parse(Console.ReadLine()); 
    if (mjölkpaket < 10) { 
        Console.WriteLine("Beställ 30 paket"); 
    }
    else if (mjölkpaket >= 10 && mjölkpaket <= 20) 
    { 
        Console.WriteLine("Beställ 20 paket"); } 
    else { 
        Console.WriteLine("Behöver inte beställa någon mjölk"); } 
        Console.WriteLine(); 
        Console.WriteLine("Tryck på enter för att avsluta"); 
        Console.ReadLine();
    }
}
David Donari
  • 599
  • 7
  • 17