aapadilla
Posts: 6
Joined: Tue Jun 06, 2006 12:25 am
Location: Mexico

Help Null Reference Exception on Read()

Sun Dec 16, 2007 9:51 pm

Hi I'm using the serial tools for a project with .Net CF with C# under windows ce 5.0. I use the read command to read after write and AT command and always used with the syntax Read(null, "\r\n",0); and works but somehow the applications hangup and didn't finish after working a while. So i decide to use a timeout and do the the syntax as read(null,"\r\n",2000) but everytime i run it, produce a sytem.nullreference exception even is the only instruction running. Any ideas what is missing?[:(]

User avatar
johan
GpsGate
Posts: 14389
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: Help Null Reference Exception on Read()

Sun Dec 16, 2007 10:15 pm

Check which exact line you get the error from. You will most likely see that the error happens on one of the rows you have written. And it happens because a variable is null, and you use it as it wasn't null.

Note that Read() returns null if no data was returned until it timed out.

Regards,
Johan

Franson Support

aapadilla
Posts: 6
Joined: Tue Jun 06, 2006 12:25 am
Location: Mexico

RE: Help Null Reference Exception on Read()

Sun Dec 16, 2007 10:51 pm

i check and look for what you said, just is there a way to check if the port is enabled or not?

User avatar
johan
GpsGate
Posts: 14389
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: Help Null Reference Exception on Read()

Sun Dec 16, 2007 10:53 pm

Yes. Port.Enabled will return information about that.

Please take a look at the reference manual, it will give you information about how method, properties and classes works in SerialTools:
http://franson.com/serialtools/reference_manual.asp?platform=net

Regards,
Johan

Franson Support

aapadilla
Posts: 6
Joined: Tue Jun 06, 2006 12:25 am
Location: Mexico

RE: Help Null Reference Exception on Read()

Sun Dec 16, 2007 10:55 pm

ok so the syntax is

if(port.enabled)
....

if true is enabled if false is not

aapadilla
Posts: 6
Joined: Tue Jun 06, 2006 12:25 am
Location: Mexico

RE: Help Null Reference Exception on Read()

Sun Dec 16, 2007 11:13 pm

I did check for other instructions and i found that the cause of the exception is on an "if"

string result = ""
result = read(null,"\r\n",500);

if (result.Equals(null))
Write_log("nothing");

... the result produces nothing but after that i'm not using it and still produce and exception, like the if is not able to complete. Any ideas how to test for a null value out of the read?

[:(][:(]

User avatar
johan
GpsGate
Posts: 14389
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: Help Null Reference Exception on Read()

Sun Dec 16, 2007 11:21 pm

Code: Select all

if(result == null)
{
  Write_log("nothing");
}
You cannot call a method on a pointer that is null. I recommend that you start by reading some book about C# for beginners.

Regards,
Johan

Franson Support

Return to “SerialTools”