From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Help with NullReferenceException in "OnTaskCreated"

What am I doing wrong here and what do I need to do to fix it??  The following code is in the "OnTaskCreated" sub created by MS for custom task settings.  I am declaring the scales (ch0Scale, ch1Scale, ...) above this code in the same Partial Public Class DaqTask1Component.  What do I need to set the scale to and where in the code so that I am no longer getting the NullReferenceException error?

 

Thank you for any assistance provided.

 

Partial Public Class DaqTask1Component
    Private ch0 As AIChannel, ch1 As AIChannel, ch2 As AIChannel, ch3 As AIChannel, ch4 As AIChannel, ch5 As AIChannel, _
        ch6 As AIChannel, ch7 As AIChannel, ch8 As AIChannel, ch9 As AIChannel, ch10 As AIChannel, ch11 As AIChannel, _
        ch12 As AIChannel, ch13 As AIChannel, ch14 As AIChannel, ch15 As AIChannel
    Private ch0Scale As NationalInstruments.DAQmx.RangeMapScale, ch1Scale As NationalInstruments.DAQmx.RangeMapScale, _
        ch2Scale As NationalInstruments.DAQmx.RangeMapScale, ch3Scale As NationalInstruments.DAQmx.RangeMapScale, _
        ch4Scale As NationalInstruments.DAQmx.RangeMapScale, ch5Scale As NationalInstruments.DAQmx.RangeMapScale, _
        ch6Scale As NationalInstruments.DAQmx.RangeMapScale, ch7Scale As NationalInstruments.DAQmx.RangeMapScale, _
        ch8Scale As NationalInstruments.DAQmx.RangeMapScale, ch9Scale As NationalInstruments.DAQmx.RangeMapScale, _
        ch10Scale As NationalInstruments.DAQmx.RangeMapScale, ch11Scale As NationalInstruments.DAQmx.RangeMapScale, _
        ch12Scale As NationalInstruments.DAQmx.RangeMapScale, ch13Scale As NationalInstruments.DAQmx.RangeMapScale, _
        ch14Scale As NationalInstruments.DAQmx.RangeMapScale, ch15Scale As NationalInstruments.DAQmx.RangeMapScale






    ''' <summary>
    ''' Initializes a new instance of the component.
    ''' </summary>
    Protected Overrides Sub Initialize()

        MyBase.Initialize()

        'TODO: Perform custom initialization here.
        '      This method is called when the component is created.
    End Sub

    ''' <summary>
    ''' Clean up any resources being used.
    ''' </summary>
    ''' <param name="disposing">True if managed resources need to be disposed; otherwise, False.</param>
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        'TODO: Perform custom clean-up here.
        '      
        '      Refer to "Implementing a Dispose Method" in the .NET Framework documentation for more
        '      information on the Disposable Pattern.

        If disposing Then
        End If

        MyBase.Dispose(disposing)
    End Sub

    ''' <summary>
    ''' Raises the TaskCreated event.
    ''' </summary>
    ''' <param name="e">The event argument of the TaskCreated event.</param>
    Protected Overrides Sub OnTaskCreated(ByVal e As EventArgs)
        'TODO: Perform custom DAQmx task configuration, such as advanced timing and triggering, here.
        '      
        '      Refer to "Adding Custom Functionality to a .NET DAQ Component" in the NI Measurement
        '      Studio Help for an example of advanced DAQmx task configuration.
        'Dim arrayVoltMin() As NationalInstruments.UI.WindowsForms.NumericEditArray

        ch0 = Task.AIChannels(0)
        ch1 = Task.AIChannels(1)
        ch2 = Task.AIChannels(2)
        ch3 = Task.AIChannels(3)
        ch4 = Task.AIChannels(4)
        ch5 = Task.AIChannels(5)
        ch6 = Task.AIChannels(6)
        ch7 = Task.AIChannels(7)
        'ch8 = Task.AIChannels(8)
        'ch9 = Task.AIChannels(9)
        'ch10 = Task.AIChannels(10)
        'ch11 = Task.AIChannels(11)
        'ch12 = Task.AIChannels(12)
        'ch13 = Task.AIChannels(13)
        'ch14 = Task.AIChannels(14)
        'ch15 = Task.AIChannels(15)

        ch0Scale.PreScaledMin = frmMain.arrayVoltMin(0).Value
        ch0Scale.PreScaledMax = frmMain.arrayVoltMax(0).Value
        ch0Scale.ScaledMin = frmMain.arrayScaleMin(0).Value
        ch0Scale.ScaledMax = frmMain.arrayScaleMax(0).Value



        Task.Control(TaskAction.Verify)




        MyBase.OnTaskCreated(e)

    End Sub

    Public Sub SetTaskRange(ByVal minRange As Double, ByVal maxRange As Double)
        'If ch0 Is Nothing Then
        'OnTaskCreated(Nothing)
        'End If

        'If minRange > maxRange Then
        ' Exit Sub
        ' End If

        'ch0.RangeLow = minRange
        'ch0.RangeHigh = maxRange

        'ch0.CustomScaleName = "test"
        'ch0Scale.PreScaledMin = frmMain.arrayVoltMin(0).Value
    End Sub
End Class
0 Kudos
Message 1 of 2
(4,026 Views)

As far as I can tell here, the main problem I see is, that there's no declaration of the frmMain object, are you doing this in another portion of code here? Since as far as I can tell there's no problem with the declaration of the scales here.

0 Kudos
Message 2 of 2
(3,987 Views)