0

System.ArgumentNullException: 'Value cannot be null. (Parameter 's')'

I am getting this error ^.

I want to make it so after I press CTRL Z on the product code it will stop the loop and proceed to the next lines of code. BUT I put in ctrl z and it goes to the Quantity line and breaks after that. If anyone knows how to fix this error, I think its because the Quantity cant return a Null value since its not setup like product is but I cant find a way around it.

public class Transaction
{
    public string Name { get; set; }
    public int productCode { get; set; }
    public int productQuantity { get; set; }

    //constructor

    public Transaction(string cName)//, int pCode, int pQuantity
    {
        Name = cName;
    }

    public void AssessProducts()
    {

        int codeInput;
        int qtyInput = 0;
        decimal transTotal = 0;
        double qty201 = 0;
        double qty556 = 0;
        double count201 = 0;
        double count556 = 0;
        double count558 = 0;
        double count909 = 0;
        double count910 = 0;

        Console.WriteLine("Product Code: ");
        string ctrlzInput = Console.ReadLine();
        Console.WriteLine("Quantity: ");
        qtyInput = int.Parse(Console.ReadLine());

        while (ctrlzInput != null)
        {
            codeInput = int.Parse(ctrlzInput);

            if (codeInput == 201)
            {
                count201 = count201 + 19.99;
            }
            else if (codeInput == 556)
            {
                count556 = count556 + 59.99;
            }
            else if (codeInput == 558)
            {
                count558 = count558 + 20.50;
            }
            else if (codeInput == 909)
            {
                count909 = count909 + 105.99;
            }
            else if (codeInput == 910)
            {
                count910 = count910 + 16.99;
            }
            else
            {
                Console.WriteLine("Error! Wrong product code.");
            }

            transTotal = (decimal)((double)transTotal + count201 + count556 + count558 + count909 + count910);
            qty201 = qty201 + qtyInput;
            qty556 = qty556 + qtyInput;


          Console.WriteLine("Product Code: ");
          ctrlzInput = Console.ReadLine();
          Console.WriteLine("Quantity: ");
          qtyInput = int.Parse(Console.ReadLine());

        }
                
       Console.WriteLine($"Customer Name: {Name} \n\n");
       Console.WriteLine("Items Ordered:\n\n");
       Console.WriteLine("Product #    Product Name              Price            Qty         Line Total\n");
        if (count201 >= 1)
        {
            Console.Write($"Product 210 - ACME Anvil               @ $19.99       *{qty201}      = {qty201 * 19.99} \n");
        }
        if (count556 >= 1)
            Console.Write($"Product 556 - ACME Dynamite            @ $59.99       *{qty556}      = {qty556 * 59.99} \n");
        }
              
    }

Attached the image error as well

3
  • 7
    Please don't post code as image, post as text Commented Mar 28, 2021 at 7:20
  • Int.Parse will only work for real integers. Use int.TryParse for a careful check Commented Mar 28, 2021 at 7:28
  • please check null or empty values passing. Commented Mar 28, 2021 at 7:47

1 Answer 1

5

From documentation:

Returns

String

The next line of characters from the input stream, or null if no more lines are available.

And:

If the Ctrl+Z character is pressed when the method is reading input from the console, the method returns null. This enables the user to prevent further keyboard input when the ReadLine method is called in a loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.