|
-
Jul 14th, 2005, 06:02 AM
#1
Thread Starter
Lively Member
[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?
-
Jul 14th, 2005, 06:10 AM
#2
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.
-
Jul 14th, 2005, 07:04 AM
#3
Thread Starter
Lively Member
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:
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? :-/
-
Jul 14th, 2005, 10:01 AM
#4
Re: An unhandled exception has occured...
Why are you using Goto 
VB Code:
line = sr2.ReadLine()
While Not (line Is Nothing) 'ToDo: Unsupported feature: assignment within expression. "=" changed to "<="
fields = line.Split(separator)
'skip if not valid - should reconsider
If fields.Length <> 5 Then
[hl=#FFFF00] GoTo ContinueWhile3[/hl]
End If
fields(0) = fields(0).Trim()
fields(1) = fields(1).Trim()
fields(2) = fields(2).Trim()
ap.AddAnswer(New AnswerPool.Answer(fields(2), Int32.Parse(fields(0)), Int32.Parse(fields(1))))
ContinueWhile3:
line = sr2.ReadLine()
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.
-
Jul 14th, 2005, 10:03 AM
#5
Thread Starter
Lively Member
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...
-
Jul 14th, 2005, 10:07 AM
#6
Re: An unhandled exception has occured...
 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.
-
Jul 14th, 2005, 10:18 AM
#7
Thread Starter
Lively Member
Re: An unhandled exception has occured...
 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...
-
Jul 14th, 2005, 01:29 PM
#8
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.
-
Jul 15th, 2005, 04:12 AM
#9
Thread Starter
Lively Member
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.
-
Jul 15th, 2005, 10:14 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|