Refer to :
I would like to fix the error when I add data table with legend keys to a MS Chart in VB.Net.
Here is the error Message:
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error:
Line 515: End If
Line 516: For Each dp As DataPoint In S.Points
Line 517: Dim t As String = dp.YValues(0).ToString(S.Tag.ToString())
Line 518: If dp.YValues(0) = 0 Then
Line 519: dp.IsValueShownAsLabel = False
|
Source File: D:\Sing Files\ASP_Final - Copy\NQI_CHART\NQI_CHART\About.aspx.vb Line: 517
Private Sub Ratechart_PrePaint(ByVal sender As Object, ByVal e As ChartPaintEventArgs)
Dim cell As LegendCell = TryCast(e.ChartElement, LegendCell)
If cell IsNot Nothing AndAlso cell.Tag Is Nothing Then
Dim r As RectangleF = e.ChartGraphics.GetAbsoluteRectangle(e.Position.ToRectangleF())
e.ChartGraphics.Graphics.DrawRectangle(Pens.DimGray, System.Drawing.Rectangle.Round(r))
If cell.CellSpan <> 1 Then e.ChartGraphics.Graphics.DrawLine(Pens.White, r.Left, r.Top + 1, r.Left, r.Bottom - 1)
End If
End Sub
Private Sub addValuesToLegend(ByVal L As Legend, ByVal S As Series, ByVal addYValues As Boolean)
Dim newItem As LegendItem = New LegendItem()
newItem.MarkerStyle = S.MarkerStyle
newItem.MarkerColor = S.Color
If S.MarkerStyle = MarkerStyle.None Then
newItem.ImageStyle = LegendImageStyle.Rectangle
newItem.BorderColor = Color.Transparent
newItem.Color = S.Color
Else
newItem.ImageStyle = LegendImageStyle.Marker
End If
newItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleCenter)
newItem.Cells.Add(LegendCellType.Text, If(addYValues, S.Name, ""), ContentAlignment.MiddleLeft)
newItem.Cells(1).CellSpan = 2
If Not addYValues Then
newItem.ImageStyle = LegendImageStyle.Line
newItem.Color = Color.Transparent
newItem.Cells(0).Tag = "*"
End If
For Each dp As DataPoint In S.Points
Dim t As String = dp.YValues(0).ToString(S.Tag.ToString())
If Not addYValues Then t = DateTime.FromOADate(dp.XValue).ToString("M\/d\/yyyy")
newItem.Cells.Add(LegendCellType.Text, t, ContentAlignment.MiddleCenter)
Next
For Each cell In newItem.Cells
cell.Margins = New System.Web.UI.DataVisualization.Charting.Margins(25, 20, 25, 20)
Next
L.CustomItems.Add(newItem)
End Sub