【发布时间】:2016-08-27 20:45:27
【问题描述】:
我在让一段代码工作时遇到了一些困难。这是叙述:我有多个具有特定格式的文本文件(因为它们是代码生成的),我需要提取特定的文本块并在文本框中仅显示这些文本块。例如,一个这样的块将是“STN COMMENT”“END COMMENT”。该文件可能有两个、三个甚至 10 个这样的块,并且会有不同类型的块,其中 COMMENT 将替换为 QUESTION、ANNOUNCEMENT 或 TRAFFIC。都以相应的“END”结束。这是我尝试的第一个代码段:
'block to parse only this user's announcements
'dl is variable alias for: Environment.NewLine + Environment.NewLine
txtNetLog.Text = "" 'clears the textbox
TmpFileName = logPath + ByCall + "-" + Format(Now, "dd-MMM-yyyy") + ".tmp" 'sets filename with path defined in module
Try
Using sr As StreamReader = New StreamReader(TmpFileName)
Dim line As String = sr.ReadLine()
While Not (line Is Nothing)
line = sr.ReadLine()
If line.Contains("STN ANNOUNCEMENT") Then '***This is line 238 from exception
Do
txtNetLog.Text = txtNetLog.Text & line + dl
line = sr.ReadLine()
Loop Until line.Contains("END ANNOUNCEMENT")
End If
End While
End Using
Catch ex As Exception
Dim msg As String = "Error: Failure Parsing Temp File For Announcements. Contact Developer" + dl + ex.ToString
Dim title = "Error in StreamReader"
Dim errorFile As System.IO.StreamWriter
Dim errorFileName As String = logPath + "Errors.txt"
errorFile = My.Computer.FileSystem.OpenTextFileWriter(errorFileName, True)
errorFile.Write(msg)
errorFile.Close()
MsgBox(msg, MsgBoxStyle.ApplicationModal, title)
End Try
这产生了以下异常:
System.NullReferenceException:对象引用未设置为对象的实例。 在 C:\Users\KE4NHW\Documents\Visual Studio 2010\Projects\GSPASEC Net Control\GSPASEC Net Control\ReviewNetLogFrm.ParseLog(Int32 DataType, Int32 SortType) 中的 GSPASEC_Net_Control.ReviewNetLogFrm.ParseLog(Int32 DataType, Int32 SortType):第 238 行
我在上面的代码中标记了第 238 行。但是,这是该块的输出:
STN 公告:18:50:54 KE4NHW
ASDFASDFASDFASDF
STN 公告:18:50:57 KE4NHW
DFADFADFASWERASDF
STN 公告:18:51:01 KE4NHW
GHSDETGJNHSDFAW34ASDFG
此输出缺少应放入文本框中的“END ANNOUNCEMENT”行,但除此之外它确实获得了所有公告。它错过了每个文本块(结束行)的一行并抛出 nullreferenceexception;除此之外,如果我可以显示结束语句,那么至少我们将拥有所有需要提取的文本。
我也尝试了以下更改但没有成功,都产生相同的错误:
更改了 While Not (line Is Nothing)...End While to Do...循环直到 line Is Nothing
将其更改为 Do until line is nothing...循环
我最新测试的版本是这样的:
txtNetLog.Text = ""
'block to parse only this user's Comments
TmpFileName = logPath + ByCall + "-" + Format(Now, "dd-MMM-yyyy") + ".tmp"
Try
Using sr As StreamReader = New StreamReader(TmpFileName)
Dim line As String = sr.ReadLine()
Do Until line Is Nothing
line = sr.ReadLine()
If line.Contains("STN COMMENT") Then
Do Until line.Contains("END COMMENT")
txtNetLog.Text = txtNetLog.Text + line + nl + nl
line = sr.ReadLine()
Loop
End If
Loop
End Using
Catch ex As Exception
Dim msg As String = "Error: Failure Parsing Temp File For Comments. Contact Developer" + nl + nl + ex.ToString
Dim title = "Error in StreamReader"
MsgBox(msg, MsgBoxStyle.ApplicationModal, title)
ErrorLogWriter("ErrorLog.txt", msg)
End Try
同样的事情。不在文本框中输入“END COMMENT”,并引发相同的异常。我没有强制 try 块只是使异常静音的想法,但这肯定会导致问题。对这个有什么想法吗?
【问题讨论】:
-
在阅读了上一行之后,您并没有检查
line是否为空。 -
在调试器中单步调试代码会在几分钟内告诉您问题的原因。你应该学会使用它。
-
您也没有检查读取的第一行 (
Dim line ....) 的内容,因此该行表示其中一个块的开始,代码将错过它。如果读取的文件很小,可以将整个文件读入并逐行处理;如果它们很大,您可能应该使用 StringBuilder 作为结果......在调试器上也是如此
标签: vb.net vb.net-2010 streamreader