Results 1 to 10 of 10

Thread: [RESOLVED] An unhandled exception has occured...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Resolved [RESOLVED] An unhandled exception has occured...

    A friend of mine made a program for me as a favor. The program basically reads up CSV files and generates questions and answers onto the screen, and all the answers are stored in another CSV file.

    Now when he made it, it was set to work for a maximum of 5 answers per question, and worked fine. I have fiddled with the code to up that to 9, and it displays fine, no errors, but when i select and answer and click "Next" I get the following error:

    An unhandled exception has occured in your application. If you click Continue the application will ignore this error and attempt to continue. If you click Quit, the application will be shut down immediately. Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index.

    Now unfortunately I can't contact my friend for help as he has gone away on vacation, and this is for work, so I need to get it working pretty soon...

    Here is the code:

    http://cyber-lane.com/Personal/form1.txt

    Is anyone able to help me out here please?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: An unhandled exception has occured...

    No way. Too much code to read. You need to run it in the debugger and narrow down where the exception is occurring. You may have set a loop to run from zero to the value of the Count property of a collection, instead of (Count - 1). You may also have set a loop to run from 0 to (Count - 1) on an empty collection. You should always specify a step of 1 when iterating over a collection that may be empty. If you can narrow it down a bit and post just a small chunk of code, I and others would be happy to help. I'm not sure too many will be prepared to read that much code though.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: An unhandled exception has occured...

    OK, well I've never been able to run it in debug mode, even when it was working code... as i'm told the following line is an error:

    VB Code:
    1. line = sr2.ReadLine()

    As for the array count, it used to work, I merely have extended it, so the counter shouldn't be a problem as it counts from 0 to X, where X is the total records in the array...

    Any other ideas? :-/

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: An unhandled exception has occured...

    Why are you using Goto

    VB Code:
    1. line = sr2.ReadLine()
    2.                     While Not (line Is Nothing) 'ToDo: Unsupported feature: assignment within expression. "=" changed to "<="
    3.                         fields = line.Split(separator)
    4.                         'skip if not valid - should reconsider
    5.                         If fields.Length <> 5 Then
    6.                          [hl=#FFFF00]   GoTo ContinueWhile3[/hl]
    7.                         End If
    8.                         fields(0) = fields(0).Trim()
    9.                         fields(1) = fields(1).Trim()
    10.                         fields(2) = fields(2).Trim()
    11.  
    12.                         ap.AddAnswer(New AnswerPool.Answer(fields(2), Int32.Parse(fields(0)), Int32.Parse(fields(1))))
    13. ContinueWhile3:
    14.                         line = sr2.ReadLine()
    15.                     End While

    The problem might be in some value being inserted into line. Try to add a Quickwatch on line on the sr2.readline() line.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: An unhandled exception has occured...

    Sorry, I don't know very much VB.NET - What is a quick watch and how do I insert one? As for the "goto", as I said... my friend did this for me as a favor, and I am merely trying to modify it as he is un-contactable at present...

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: An unhandled exception has occured...

    Quote Originally Posted by Cyber-Drugs
    it counts from 0 to X, where X is the total records in the array...
    That sounds like what I said in my previous post. An array is indexed from 0 To (Length - 1), so if you try to get the element at an index of Length, then you are beyond the bounds of the array. Voila! Exception. If you have X records in your array, your loop counter should go from 0 to (X - 1). Also, I think you need to see to that error you mention when calling ReadLine. If it doesn't work in the debugger how can you expect it to work for real.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: An unhandled exception has occured...

    Quote Originally Posted by jmcilhinney
    That sounds like what I said in my previous post. An array is indexed from 0 To (Length - 1), so if you try to get the element at an index of Length, then you are beyond the bounds of the array. Voila! Exception. If you have X records in your array, your loop counter should go from 0 to (X - 1). Also, I think you need to see to that error you mention when calling ReadLine. If it doesn't work in the debugger how can you expect it to work for real.

    Read up ^^^

    It worked for real WITH that error...
    The counter works fine, as it worked counting up to 5

    I merely have increased it to 9, and now am having issues as I think I have missed something out...

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: An unhandled exception has occured...

    You really need to get it working in the debugger. That's the only way you can test it properly and, well, debug it. I suggest you run it until you get an error and deal with that error. Once that's sorted, run it again until the next error comes up. If you can let us know what the error message is and what line it occurs on then we can help.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: An unhandled exception has occured...

    I found the problem was a bug in VB...

    It was an If then else statement with "And". I broke it up into 2 if statements, and it works just fine.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] An unhandled exception has occured...

    I could be wrong but I doubt that it was a bug in VB. My guess is that changing And to AndAlso would have fixed the issue. If you don't mind, could you show the old code and what you changed it to so I can confirm this. If it is the case, I can explain why and that's one less problem you'll have to worry about in the future.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width