Search code examples
c#arraysargumentsprogram-entry-pointindexoutofrangeexception

System.IndexOutOfRangeException in console app - Likely system based


This one's a puzzling one for me. I know most of these types of errors are from trying to access something outside of the bounds of an array (and the error says that too), but it doesn't add up here. The following is the error:

System.IndexOutOfRangeException: Index was outside the bounds of the array at MyProgram.Program.Main(String[] args) in Program.cs:line 47

The only problem here being that the first line of my program is at line 47 (which just defines a MailAddress variable). If I comment out that line, the second line errors, which is defining an SMTP client. Up until yesterday, this same program ran every morning for the past two months or so. I have not changed the code, recompiled, or done anything to it in the interim (though since then I've tried some debugging to figure it out after it broke).

The program itself is pretty simple, it just uses System.Net.Mail and System.IO to look through some text files and send an e-mail of the contents.

My question, while I know it's not advisable to post without any code, is a general one. Since I know it works on another system (I just ran it on there), my question is what could cause this behavior/error?

Things I've tried:

  • Repair .NET Framework 4.5.2 (the target Framework on the program is 4.5).
  • Tried recompiling for x64 (was previously x86).
  • Restarted (obviously, just wanted to point that out).
  • Disabled Windows Firewall to test (unlikely because of the error).

Is there something obvious here I'm missing?

EDIT:

        static void Main(string[] args)
    {

            MailAddress sendEmailFromNice = new MailAddress("email@email.com", "Nice Email Name");
            SmtpClient client = new SmtpClient();
            client.Port = smtpport;
            client.Host = smtphost;
            client.EnableSsl = true;
            client.Timeout = 25000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(sendEmailFrom, sendEmailFromPass);

            ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            { return true; };

            // gotten list
            List<int> alreadyGottenList = new List<int>();
            string line = null;
            string[] lineSplit = null;
            int currentInt = 0;
            if (File.Exists(storedIntList))
            {
                using (var reader = new StreamReader(storedIntList))
                {
                    line = null;
                    while ((line = reader.ReadLine()) != null)
                    {
                        currentInt = int.Parse(line.Trim());
                        alreadyGottenList.Add(currentInt);
                        currentInt = 0;
                    }
                } 
            }

            List<int> newIntsToGet = new List<int>();
            if (File.Exists(LogFile))
            {
                using (var readerPrimaryToday = new StreamReader(LogFile))
                {
                    while ((line = readerPrimaryToday.ReadLine()) != null)
                    {
                        lineSplit = line.Split(',');

                        if (lineSplit[0].ToString() != "header")
                        {
                            currentInt = int.Parse(lineSplit[8].ToString().Trim());
                        }
                        if (!alreadyGottenList.Contains(currentInt))
                        {
                            newIntsToGet.Add(currentInt);
                            alreadyGottenList.Add(currentInt);
                            File.AppendAllText(storedIntList, currentInt.ToString() + Environment.NewLine);
                        }
                    }
                }
            }

    string emailBody = "BodyHere ";
    string emailSubject = "Subject here";

    foreach (var thisVar in newIntsToGet)
    {
        emailBody = emailBody + thisVar.ToString() + Environment.NewLine;
    }

    MailMessage messageToSend = new MailMessage();
    messageToSend.Subject = emailSubject;
    messageToSend.Body = emailBody;
    messageToSend.IsBodyHtml = false;

    messageToSend.To.Add(sendEmailTo);
    client.Send(messageToSend);
}

It just has a .csv file with numbers, then checks a text file for any numbers I haven't already seen. If it finds one, it adds it to an email and to the stored .csv file.


Solution

  • I'm not sure, but looking at your code, IndexOutOfArrayException may be related with the following lines:

    if (lineSplit[0].ToString() != "header")
    {
        currentInt = int.Parse(lineSplit[8].ToString().Trim());
    }
    

    You do not seem to check if there was anything returned in lineSplit. If for example the LogFile that you read was somehow modified and now contains a new line, it would cause an issue for the lineSplit[0] -- or if the log was modified and now you don't have the 9th column value for the lineSplit[8]