0

I am kinda new to C#. A problem when using split. I thought it returned a string array. But once it gets to the last line below it crashes and says I cant access it. Out of bounds. Even though in the split it would have found multiple '~'. Any solutions to my problem?

String tempString = " ";

        while ((tempString = streamReader.ReadLine()) != null)
        {
            String [] split = tempString.Split('~');

            typeOfVehicle = split[0];
            manufactuer = split[1];

Thanks very much

Question solved.

6
  • 6
    Have you checked split.Length to see how many (and what) elements are actually in the array? Commented Sep 20, 2011 at 23:55
  • 1
    You're comparing it to null, but not checking if it's empty. If the last string returned cannot be split, then it won't have those indexes -- try using while (!String.IsNullOrEmpty(tempString = streamReader.ReadLine()){...} Commented Sep 20, 2011 at 23:56
  • yes the array is populated with 10 items. The split worked perfectly. Commented Sep 20, 2011 at 23:57
  • The string will never be empty. When I go into the debugger and look at split[1] there is the string I want but it crashes Commented Sep 21, 2011 at 0:00
  • I think i see the problem. When writing to a file is it possible that it places an extra line in that is blank? Commented Sep 21, 2011 at 0:03

2 Answers 2

2

You are assuming that when you split the string you will have at least 2 elements. Never assume. Always check the length of the array before you attempt to access an index.

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

1 Comment

Well, if the OP checks the length and doesn't execute the code then the OP will merely be hiding the fact that he has bad data. If the data is supposed to be clean then this in fact hides a bug. If one is going to check for a condition that should never be false, then the OP should, at most, throw a new exception that will continue to propagate the error so that the bug (wherever it is) can be fixed.
0

Just catch the exception and you'll soon see that you have a problem with the string you're reading.

String[] split = tempString.Split('~');

try
{
    typeOfVehicle = split[0];
    manufactuer = split[1];
}
catch
{
    Console.WriteLine("Oops! It didn't work.");
    Console.WriteLine("The offending string was \"{0}\"", tempString);
}

4 Comments

Unless this is a toy app, writing to the console is perhaps the worst way to communicate errors. (instead, throw an exception with better messaging)
No, I do this in my prod systems all the time. Works great.
If this, for example, is in a method that can be called by consumers that are not a console app (i.e. Windows or ASP.NET), then you have essentially just thrown the error message away. Why adopt a pattern that only works for console apps (and poorly at that, since you lose the stack trace)?
Sorry, should have written "JOKING" next to that comment. Clearly this is a piece of code that OP can cut and paste into a console to identify his problem. One would hope that he isn't experiencing this problem in a real system. OP is 'kinda new' to C#. I'm assuming that he's 'trying things'. Please, shoot me.

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.