Reason to Stacktrace

Peter_1985 2,866 Reputation points
2021-06-09T09:04:18.34+00:00

Hi,
Is it possible that error below

at System.String.IndexOf(String value, Int32 startIndex, Int32 count, StringComparison comparisonType)
at System.String.IndexOf(String value, Int32 startIndex)
at Validate_File1.Program.Main(String[] args)

is due to this part of code? How to avoid it?

                                                if (Pos4 - 3 - Pos3 > 0)
                                                    Email = l7[j].Substring(Pos3 + 2, Pos4 - 3 - Pos3);
Developer technologies | .NET | .NET Runtime
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-06-10T09:13:13.493+00:00

    The text file you are reading has blank lines (Error.txt).

    When the current line is a blank line, its length is 0, then use IndexOf(Char, Int32), startIndex is greater than the length of the string, this problem will occur.

    Try to add a judgment, if the current line length is 0, use continue to enter the next round of loop.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. cheong00 3,486 Reputation points Volunteer Moderator
    2021-06-09T09:06:58.64+00:00

    No.

    From the variable name, I think the line throwing error should be on assignment of value for Pos4, Pos3 or even Pos2 or Pos1/Pos instead. Try find "IndexOf" with Ctrl-F on that source file.

    Also, if you think these lines throwing that error because of the line number in stack trace, chances are that the source code version is not the same as the binary throwing the exception.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.