Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
null
on initialisation.
C#
if (DataBuffer.Contains(ok))
{
    okFound = true;
}

and
C#
string temp = getLine(DataBuffer.Substring(mylocation));
if (!checkTypeFound())
{
    if (temp != null)
    {
        parseDeviceType(temp);
    }
    checkTypeFound();
}

DataBuffer information is loaded in this function:
C#
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?
Posted

hiii,

add Condition

if(!string.isNullOrEmpty(DataBuffer))
{
if(DataBuffer.Contains(ok))
{
okFound = true;
}
}
 
Share this answer
 
Comments
pieterjann 4-Sep-12 4:58am    
This does not solve the problem, but only removes the error. I must have the problem that results to the error solved. Thanks anyway!
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 ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900