Skip to content

When I use Marker to display and track the mouse position, it prompts when dragging System.NullReferenceException:"Object reference not set to an instance of an object. #5248

Description

@pkq520

Issue: (Describe the bug here)
When I use Marker to display and track the mouse position, it prompts when dragging System.NullReferenceException:"Object reference not set to an instance of an object.

ScottPlot Version:
5.1.58

Code Sample:

private AvaPlot? _plotControl;
private int _lastIndexPointTrack = -1;
private Marker? _pointTrackMarker;
private void AvaPlotPointerMoveTrack(object? sender, PointerEventArgs e)
{
    if (_plotControl != null)
    {
        Pixel mousePixel = new((int)e.GetPosition(_plotControl).X, (int)e.GetPosition(_plotControl).Y);
        Coordinates coordinates = _plotControl.Plot.GetCoordinates(mousePixel);
        (double pointX, double pointY, int pointIndex) = GetPointNearestX(coordinates);
        if (pointIndex != _lastIndexPointTrack)
        {
            RemovePlottable(_pointTrackMarker);
            _pointTrackMarker = _plotControl.Plot.Add.Marker(pointX, pointY, MarkerShape.OpenCircle, 8, Colors.Red);
            _lastIndexPointTrack = pointIndex;
            PlotRefresh();
        }
    }
}

private void AvaPlotPointerLeaveTrack(object? sender, PointerEventArgs e)
{
    if (_plotControl != null && _pointTrackMarker != null)
    {
        RemovePlottable(_pointTrackMarker);
        _lastIndexPointTrack = -1;
        _pointTrackMarker = null;
        PlotRefresh();
    }
}

public void EnableMouseMoveTrack()
{
    if (_plotControl != null)
    {
        _plotControl.PointerMoved += AvaPlotPointerMoveTrack;
        _plotControl.PointerExited += AvaPlotPointerLeaveTrack;
    }
}

Press and drag the left mouse button on the chart to enter the exception mode.

QQ20260612-164302.mp4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions