object reference not set to an instance of an object

Posted on 2009-04-23
Last Modified: 2012-06-27
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

Question by:JessyRobinson1234
    LVL 10

    Expert Comment

    I would recommend putting a breakpoint on the line and check in the
    debugger if "r" and the other variables are initialized correctly.

    Author Comment

    The variables are initialized correctly
    LVL 10

    Expert Comment

    Try this, and see if it helps...
    Dim r As New Random(CInt(random(100)))
    LVL 10

    Expert Comment

    Ignore the last reply, it should make no difference.

    Accepted Solution

    What I am trying to achieve is an auto update of the chart every 3 minutes. Is this the way to do it?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now