Based on your comments, I think either your firstEnumerable
or secondEnumerable
objects are null
. But since you did not post how you obtain these objects, I can't comment on why they are null
or how to fix it.
It's okay for them to contain null
entries, or even be fully empty, but they themselves cannot be null
or it will throw an ArgumentNullException
when you call .ToArray()
on them. This corresponds with the "value cannot be null" exception message you're seeing.
The reason why it's not crashing and burning hard is because you are swallowing (and logging?) the exceptions within your iteration loop with a try/catch
block that was not posted in your code sample.
I'm guessing your actual code is something like this:
foreach (Contact contact in Contact.LoadWithPredicate(getAll))
{
try
{
...
object[] firstEnumerable = null;
object[] secondEnumerable = null;
//some logic gates here which under some circumstances do not
//assign a valid instance to firstEnumerable or secondEnumerable
...
Console.WriteLine(i); //this prints every time
//Turn the Enumerables into strings for printing
string firstEnumAsString = String.Join(", ", firstEnumerable.ToArray()); //exception here
string secondEnumAsString = String.Join(", ", secondEnumerable.ToArray()); //or exception here
Console.WriteLine("Email: " + firstString+ ", correspondance: " + secondString+ ", PAM addresses: " + firstEnumAsString+ ", famousPeople: " + secondEnumAsString);
...
}
catch
{
//maybe some logging? maybe not?
}
}
This will print out the i
value each time. But when it attempts to create firstEnumAsString
or secondEnumAsString
it throws an exception and never hits your second Console.WriteLine("Email: " + ...);
thus producing the output you're seeing. It's not that the Console.WriteLine
is failing, it's that you're never calling it in the first place.