object reference not set to an instance of an object

I am building a real time chart using a timer and AJAX Callback. I am getting the error on :

  Dim newYValue = lastYValue + r.Next(-10, 10)
            Dim newXValue = lastXValue + 1

Any help is greatly appreciated!

Imports Dundas.Charting.WebControl
' Random value
Private r As New Random()
' <summary>
' Page Load event handler.
' </summary>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' lets only series to be serialized.
        Me.Chart1.CallbackStateContent = CallbackStateContent.SeriesDataOnly
        ' Register page startup script that will be executing chart callback command every second
        Me.Page.ClientScript.RegisterStartupScript(GetType(Dundas.Charting.WebControl.Chart), "Chart1_Callback", "window.setInterval(""" + Me.Chart1.CallbackManager.GetCallbackAsyncMethodReference(Timer, "") + """, 500);", True)
    End Sub
' <summary>
' Chart Callback event handler.
' </summary>
    Protected Sub Chart1_Callback(ByVal sender As Object, ByVal e As CommandEventArgs) Handles Chart1.Callback, Chart1.CallbackComplete
        ' Add random point into each series
        Dim series As Series
        For Each series In Me.Chart1.Series
            Dim lastYValue As Double = series.Points(series.Points.Count - 1).YValues(0)
            Dim lastXValue As Double = series.Points(series.Points.Count - 1).XValue
            Dim newYValue = lastYValue + r.Next(-10, 10) ---> object reference not set to an instance of an object
            Dim newXValue = lastXValue + 1
            If newYValue > 100 Or newYValue < 0 Then
                newYValue = lastYValue
            End If
            series.Points.AddXY(newXValue, newYValue)
        ' Remove points from the left chart side if number of points exceeds 100.
        While Me.Chart1.Series(0).Points.Count > 100
        End While
        ' Adjust categorical scale
        Dim axisMinimum As Double = Me.Chart1.Series(0).Points(0).XValue
        Me.Chart1.ChartAreas("Default").AxisX.Minimum = axisMinimum
        Me.Chart1.ChartAreas("Default").AxisX.Maximum = axisMinimum + 100
        'End If
    End Sub 'Chart1_Callback

Open in new window

Who is Participating?
JessyRobinson1234Author Commented:
What I am trying to achieve is an auto update of the chart every 3 minutes. Is this the way to do it?
I would recommend putting a breakpoint on the line and check in the
debugger if "r" and the other variables are initialized correctly.
JessyRobinson1234Author Commented:
The variables are initialized correctly
Try this, and see if it helps...
Dim r As New Random(CInt(random(100)))
Ignore the last reply, it should make no difference.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.