较新版本的Visual Studio中的C#编译错误 [英] C# Compiling error in newer version Visual Studio

查看:234
本文介绍了较新版本的Visual Studio中的C#编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在编译最初在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;
}
}


如果DataBuffernull,则显示的对DataBuffer的所有3个引用都将引发NullReferenceException. 为什么不将其初始化为string.Empty而不是null?
All 3 of the references to DataBuffer you have shown will cause the NullReferenceException to be thrown if DataBuffer is null.
Why not just initialize it to string.Empty instead of null ?


这篇关于较新版本的Visual Studio中的C#编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆