Help with NullReferenceException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • plyable
    New Member
    • Feb 2008
    • 2

    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 "NullReferenceE xception 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.Sub string(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:


    [code=vbnet]
    Public Class Form1


    Dim s As String
    Dim output_data As String
    Dim steps As String
    Dim colon8 As String = "Chr(4) & Chr(49) & Chr(49) & Chr(58) & Chr(56) & Chr(5)"
    'code in hex is 04 31 31 3A 38 05
    'code in ASCII is 11:8




    Private Sub Activate_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles Activate.Click

    COMPort1.Open()

    End Sub

    Private Sub Deactivate_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles Deactivate.Clic k

    COMPort1.Close( )

    End Sub

    Private Sub COMPort1_DataRe ceived(ByVal sender As System.Object, ByVal e As System.IO.Ports .SerialDataRece ivedEventArgs) Handles COMPort1.DataRe ceived

    output_data &= COMPort1.ReadEx isting

    End Sub


    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click

    Timer1.Enabled = Not Timer1.Enabled

    End Sub

    Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As System.EventArg s) Handles Timer1.Tick

    COMPort1.Write( Chr(4) & Chr(49) & Chr(49) & Chr(58) & Chr(56) & Chr(5))


    steps = output_data.Sub string(4)
    steps = steps.Substring (0, steps.Length - 2)
    TextBox1.Text() = steps
    steps = ""


    End Sub

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load


    End Sub
    End Class
    [/code]
    Last edited by Plater; Feb 14 '08, 06:28 PM. Reason: added [CODE] tags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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.

    Comment

    • plyable
      New Member
      • Feb 2008
      • 2

      #3
      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
      [code=vbnet]
      count = count.Substring (4)
      count = count.Substring (0, count.Length - 2)
      [/code]
      in my timer toolbox, but in my comport toolbox. After fixing it. the codes look like this:

      [code=vbnet]
      Public Class Form1

      Dim display As String
      Dim degree As Integer
      Dim output_data As String
      Dim count As String
      Dim colon8 As String = "Chr(4) & Chr(49) & Chr(49) & Chr(58) & Chr(56) & Chr(5)"
      'code in hex is 04 31 31 3A 38 05
      'code in ASCII is 11:8




      Private Sub Activate_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles Activate.Click

      If COMPort1.IsOpen Then
      If Timer1.Enabled Then
      Timer1.Stop()
      End If
      COMPort1.Close( )
      Activate.Text = "Turn On"
      Else
      COMPort1.Open()
      Activate.Text = "Turn Off"
      End If




      End Sub

      Private Sub Deactivate_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles Deactivate.Clic k

      Application.Exi t()

      End Sub

      Private Sub COMPort1_DataRe ceived(ByVal sender As System.Object, ByVal e As System.IO.Ports .SerialDataRece ivedEventArgs) Handles COMPort1.DataRe ceived

      output_data &= COMPort1.ReadEx isting()
      'output_data &= COMPort1.ReadLi ne()
      'supposedly the best way to read data


      count = output_data
      count = count.Substring (4)


      count = count.Substring (0, count.Length - 2)

      End Sub


      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click

      Timer1.Enabled = Not Timer1.Enabled

      End Sub

      Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As System.EventArg s) Handles Timer1.Tick



      COMPort1.Write( Chr(4) & Chr(49) & Chr(49) & Chr(58) & Chr(56) & Chr(5))

      TextBox1.Text = display

      count = ""
      output_data = ""
      End Sub


      End Class

      [/code]


      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 misunderstandin g something.

      What should I consider?
      Last edited by Frinavale; Feb 15 '08, 04:58 PM. Reason: Added [Code] Tags

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        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)

        Comment

        Working...