我編譯這是使用Visual C#2010年最初建立在Visual C#2005年我'一個方案,並就以下功能在運行程序時,我不斷收到「NullReference Execption是未處理的」錯誤: DataBuffer在線上發生錯誤。 DataBuffer是一個私有字符串,在初始化時設置爲null。編譯錯誤Nullreference
if (DataBuffer.Contains(ok))
{
okFound = true;
}
和
string temp = getLine(DataBuffer.Substring(mylocation));
if (!checkTypeFound())
{
if (temp != null)
{
parseDeviceType(temp);
}
checkTypeFound();
}
當我檢查什麼DataBuffer的值在上面的代碼(當我得到的錯誤),這是不爲空。它實際上包含我期望的數據。 的DataBuffer信息加載在這個函數:
private void ser1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
while (ser1.BytesToRead > 0)
{
string data = ser1.ReadExisting();
DataBuffer += data;
}
}
串行端口打開其他地方的代碼。代碼沒有改變,只有編譯器不同。我應該添加什麼行,以及在哪裏解決這個錯誤?請注意,我可以使用if和try-catch語句來防止發生此錯誤。但是這不是我正在尋找的,我需要這個代碼才能工作。 此應用程序未經編譯器以外的任何其他方式更改。
你已經證明*片段的代碼*,但你沒有給出爲什麼你所期望的'DataBuffer'是由非空指示你到達第一個片段的時間。 –
這個錯誤是否真的發生在編譯過程中?或在運行時?什麼是'DataBuffer',什麼時候分配一個值? –
另一個注意事項:重複連接重複是一種虛假 - 真的應該使用StringBuilder –