0
string[] numbers = new string[10];
bool found = false;
for(int i = 0; i < numbers.Length; i++)
{
    numbers[i] = Console.ReadLine();
    if(numbers[i] == "x")
    {
        break;
    }
}
for (int i = 0; i < numbers.Length; i++)
{
    if(numbers[i] != "x")
    {
        if (Int32.Parse(numbers[i]) % 2 == 0)
        {
            found = true;
            Console.WriteLine(numbers[i]);
        }
    }
}
if(found == false)
{
     Console.WriteLine("N/A");
}
Console.ReadLine();

The following message is shown: Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 's') at System.Int32.Parse(String s)

The program continouslly ask for user inputed integers, until "X" is introduced. It will show the even numbers from user inputs.

lee-m
  • 2,269
  • 17
  • 29
  • Why are you breaking from the first for loop, then immediately continuing to the next for loop? – gunr2171 Oct 06 '22 at 18:32
  • 1
    When you step through your code in a debugger, what are the values in `numbers` when you enter the second loop? The error is telling you at least one of those values is `null`. What did you expect the values to be? Why? – David Oct 06 '22 at 18:33
  • Not sure why this was closed as a duplicate of a `NullReferenceException` question, since this code isn't producing that exception. Voting to re-open... – David Oct 06 '22 at 18:39
  • 1
    You don't break out of your second loop when you find the value `x`. You can change that `for` loop to `for (int i = 0; i < numbers.Length && !found ; i++)` – Eric J. Oct 06 '22 at 18:39
  • @David The same steps there will lead to the correct solution (which I also gave in the comment above). – Eric J. Oct 06 '22 at 18:40

1 Answers1

1

Your intent here is to stop populating the array once the user enters "x":

if(numbers[i] == "x")
{
    break;
}

And if the array isn't fully populated, the rest of its contents are null values.

Then in the following loop you never break from the loop at all, always trying to process every element in the array. But any element after "x" will be null.

You can also check for null in your condition:

if(numbers[i] != "x" && numbers[i] != null)

Or potentially break from the second loop when you encounter "x":

for (int i = 0; i < numbers.Length; i++)
{
    if(numbers[i] == "x")
    {
        break;
    }
    if (Int32.Parse(numbers[i]) % 2 == 0)
    {
        found = true;
        Console.WriteLine(numbers[i]);
    }
}

Other approaches could include using a List<string> instead of a string[] array, since a List<> is not fixed size and only needs to be populated with the values you want.

David
  • 208,112
  • 36
  • 198
  • 279
  • What you describe is covered in the question I closed this for and you reopened. https://stackoverflow.com/a/4660186/141172 – Eric J. Oct 06 '22 at 18:42
  • 1
    @EricJ.: The same basic debugging steps of "debug, check what value is `null`, handle the `null` case" are indeed true, but I don't think closing as an exact duplicate was appropriate because the exception itself (and thus the specific cause, which in that case is *dereferencing* a `null` reference) is different and that would easily confuse the OP. I can see how this is a bit of an edge case on which we can disagree. As long as the OP is helped, that's what matters. – David Oct 06 '22 at 18:46
  • `Person[] people = new Person[5]; people[0].Age = 20 // people[0] is null. The array was allocated but not initialized` is specifically called out as a case. – Eric J. Oct 06 '22 at 18:58