473,412 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Help with NullReferenceException

Hi there. I made a program that reads data from a device through a serial port which then displays it in a textbox. As the data comes out in ASCII form, I have to filter out the unnecessary parts for it to be useful. Example is shown below.

:8+3423,

to

3423


I have managed to to find the necessary code to perform this filtering. But when used in the program I made I get an error that says "NullReferenceException was unhandled" and that "Object reference not set to an instance of an object". It then suggest that I use the "new" keyword to create an object instance. The codes I use for the filtering is:

steps = output_data.Substring(4)
steps = steps.Substring(0, steps.Length - 2)

I use these as since the position of the ASCII is fixed.

Please advice

Codes for program are as shown below:


Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.  
  4.     Dim s As String
  5.     Dim output_data As String
  6.     Dim steps As String
  7.     Dim colon8 As String = "Chr(4) & Chr(49) & Chr(49) & Chr(58) & Chr(56) & Chr(5)"
  8.     'code in hex is 04 31 31 3A 38 05
  9.     'code in ASCII is 11:8
  10.  
  11.  
  12.  
  13.  
  14.     Private Sub Activate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Activate.Click
  15.  
  16.         COMPort1.Open()
  17.  
  18.     End Sub
  19.  
  20.     Private Sub Deactivate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Deactivate.Click
  21.  
  22.         COMPort1.Close()
  23.  
  24.     End Sub
  25.  
  26.     Private Sub COMPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles COMPort1.DataReceived
  27.  
  28.         output_data &= COMPort1.ReadExisting
  29.  
  30.     End Sub
  31.  
  32.  
  33.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  34.  
  35.         Timer1.Enabled = Not Timer1.Enabled
  36.  
  37.     End Sub
  38.  
  39.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  40.  
  41.         COMPort1.Write(Chr(4) & Chr(49) & Chr(49) & Chr(58) & Chr(56) & Chr(5))
  42.  
  43.  
  44.         steps = output_data.Substring(4)
  45.         steps = steps.Substring(0, steps.Length - 2)
  46.         TextBox1.Text() = steps
  47.         steps = ""
  48.  
  49.  
  50.     End Sub
  51.  
  52.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  53.  
  54.  
  55.     End Sub
  56. End Class
  57.  
Feb 14 '08 #1
3 1316
Plater
7,872 Expert 4TB
I see this as a problem:
TextBox1.Text() = steps

It should be:
TextBox1.Text = steps


Although I would recomend otherways of filtering out the data, your substring seems to be ok. But it could throw index out of range exceptions if not as much data came in as you think shoud have.
Feb 14 '08 #2
Thanks for the reply. I manage to find out how to fix the problem, though I do not understand why doing so fixes the problem. Apparently I cannot declare
Expand|Select|Wrap|Line Numbers
  1. count = count.Substring(4)
  2. count = count.Substring(0, count.Length - 2)
  3.  
in my timer toolbox, but in my comport toolbox. After fixing it. the codes look like this:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Dim display As String
  4.     Dim degree As Integer
  5.     Dim output_data As String
  6.     Dim count As String
  7.     Dim colon8 As String = "Chr(4) & Chr(49) & Chr(49) & Chr(58) & Chr(56) & Chr(5)"
  8.     'code in hex is 04 31 31 3A 38 05
  9.     'code in ASCII is 11:8
  10.  
  11.  
  12.  
  13.  
  14.     Private Sub Activate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Activate.Click
  15.  
  16.         If COMPort1.IsOpen Then
  17.             If Timer1.Enabled Then
  18.                 Timer1.Stop()
  19.             End If
  20.             COMPort1.Close()
  21.             Activate.Text = "Turn On"
  22.         Else
  23.             COMPort1.Open()
  24.             Activate.Text = "Turn Off"
  25.         End If
  26.  
  27.  
  28.  
  29.  
  30.     End Sub
  31.  
  32.     Private Sub Deactivate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Deactivate.Click
  33.  
  34.         Application.Exit()
  35.  
  36.     End Sub
  37.  
  38.     Private Sub COMPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles COMPort1.DataReceived
  39.  
  40.         output_data &= COMPort1.ReadExisting()
  41.         'output_data &= COMPort1.ReadLine()
  42.         'supposedly the best way to read data
  43.  
  44.  
  45.         count = output_data
  46.         count = count.Substring(4)
  47.  
  48.  
  49.         count = count.Substring(0, count.Length - 2)
  50.  
  51.     End Sub
  52.  
  53.  
  54.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  55.  
  56.             Timer1.Enabled = Not Timer1.Enabled
  57.  
  58.     End Sub
  59.  
  60.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  61.  
  62.  
  63.  
  64.         COMPort1.Write(Chr(4) & Chr(49) & Chr(49) & Chr(58) & Chr(56) & Chr(5))
  65.  
  66.         TextBox1.Text = display
  67.  
  68.         count = ""
  69.         output_data = ""
  70.     End Sub
  71.  
  72.  
  73. End Class
  74.  
  75.  

So I now have a new problem. Because I am using the command ReadExisting, My data is read into my software in two phases. All this while I have been using the '&=' to combine the data together, but apparently there is the possibility of me losing data that way. So I was wondering if you could show me how to use ReadLine as it seems that it is the best way to read data. I have not been sucessful so far as when I use that command, the software hangs. I was told that I need a LF at the end of my data to get it to work.

My data is basically ASCII, and will normally look like this:

:8+63632

the last two symbols do not look like LF to me so I do not know if I am misunderstanding something.

What should I consider?
Feb 15 '08 #3
Plater
7,872 Expert 4TB
Read line looks for the character string provided to the SerialPort object (there's a property for it in there, line terminator or something) and will continue to try and read data until it encounters that character string.
You would need to find out exactly what character code those last bytes are and use THAT as your line terminator. Then calling ReadLine() will block until it gets an instance of those characters. (Be sure to set ReadTimeOuts if you don't want to wait forever)
Feb 15 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
5
by: TT (Tom Tempelaere) | last post by:
Hi, Once in a while my application throws an NullReferenceException at startup, however it appears to be occurring in an unknown module. If I debug it and ask to 'break', then there is no source...
1
by: Ghada Ayed via DotNetMonster.com | last post by:
Hello every body, please help me in this problem i have written the following C# code in VS.Net and when i run the window application i have got 'System.NullReferenceException' so please if...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
0
by: noe | last post by:
I have done a DLL in unmanaged code C++ and in that dll I have defined a function that use memcpy.This dll run ok.I use that dll in a aplication in C# managed code in Visual Studio .NET. I import...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
9
by: Xero | last post by:
could anybody describes a scenario where this error will occur? i have visited the page about the NullReferenceException in msdn library but i still have no idea what it is about. thanks. ...
1
by: Jeff | last post by:
hey asp.net 2.0 The line "e.Item.Parent.ChildItems.Remove(e.Item);" gives this error: 'e.Item' threw an exception of type 'System.NullReferenceException' System.Web.UI.WebControls.MenuItem...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.