exception error caused by reading text file
-
Im using something called syntax high lighting textbox library in one of my project. To be able to easily highlight syntax, i wrote this lil code:
/* Syntax Highlighting Keywords [ BLUE ] */ TextReader syntax_blue = new StreamReader("synHighLib_DB/blue.txt"); while (syntax_blue.ReadLine() != null) { shtb.HighlightDescriptors.Add(new HighlightDescriptor(syntax_blue.ReadLine(), Color.Blue, null, DescriptorType.Word, DescriptorRecognition.WholeWord, true)); } syntax_blue.Close();
It compiles properly, but when i start typing into the text box, i get the unhandled exception error. Im 100% sure this is the code that causing the error, because, when i remove it, it works fine... -
Im using something called syntax high lighting textbox library in one of my project. To be able to easily highlight syntax, i wrote this lil code:
/* Syntax Highlighting Keywords [ BLUE ] */ TextReader syntax_blue = new StreamReader("synHighLib_DB/blue.txt"); while (syntax_blue.ReadLine() != null) { shtb.HighlightDescriptors.Add(new HighlightDescriptor(syntax_blue.ReadLine(), Color.Blue, null, DescriptorType.Word, DescriptorRecognition.WholeWord, true)); } syntax_blue.Close();
It compiles properly, but when i start typing into the text box, i get the unhandled exception error. Im 100% sure this is the code that causing the error, because, when i remove it, it works fine...Posting the exception you're receiving would be very helpful, but it looks like you're calling ReadLine twice without any assurance that you have two (or a multiple of two) lines to read. Readline is going to advance the cursor each time it's called. Additionally, if the code above is inside an error handler, I would point out that it might not be the best choice to open and parse a text file every time a key is pressed. Finally, a TextReader is a disposable type, so I would recommend either inserting a Try...Finally block or, better, a using (TextReader syntax_blue = ...) block instead.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’