较新版本的Visual Studio中的C#编译错误 [英] C# Compiling error in newer version Visual Studio
问题描述
你好,
我正在编译最初在Visual C#2005中构建的程序.我正在使用Visual C#2010.在以下功能上,我不断收到未处理NullReference Execption"错误:
该错误发生在使用DataBuffer的行上. DataBuffer只是一个私有字符串,在初始化时设置为
Hello,
I''m compiling a program which was originally build in Visual C# 2005. I''am using visual C# 2010. And I keep getting "NullReference Execption was unhandled" errors on the following function:
the error occurs on the line with DataBuffer. DataBuffer is just an private string set to
null
.
if (DataBuffer.Contains(ok))
{
okFound = true;
}
和
and
string temp = getLine(DataBuffer.Substring(mylocation));
if (!checkTypeFound())
{
if (temp != null)
{
parseDeviceType(temp);
}
checkTypeFound();
}
DataBuffer信息在此函数中加载:
DataBuffer information is loaded in this function:
private void ser1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
while (ser1.BytesToRead > 0)
{
string data = ser1.ReadExisting();
DataBuffer += data;
}
}
串行端口在代码中的其他位置打开.仅编译器不同,代码没有任何更改.我应该添加哪行,以及在哪里解决该错误?
The serial port is opened somewhere else in the code. There have been no changes to the code only the compiler is different. What line should I add, and where to solve this error?
推荐答案
hiii,
添加条件
if(!string.isNullOrEmpty(DataBuffer))
{
if(DataBuffer.Contains(ok))
{
okFound = true;
}
}
hiii,
add Condition
if(!string.isNullOrEmpty(DataBuffer))
{
if(DataBuffer.Contains(ok))
{
okFound = true;
}
}
如果DataBuffer
为null
,则显示的对DataBuffer
的所有3个引用都将引发NullReferenceException
. 为什么不将其初始化为string.Empty
而不是null
?
All 3 of the references toDataBuffer
you have shown will cause theNullReferenceException
to be thrown ifDataBuffer
isnull
.
Why not just initialize it tostring.Empty
instead ofnull
?
这篇关于较新版本的Visual Studio中的C#编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!