Answered by:
RegisterName throws NullReferenceException

Question
-
Hi I am having a problem registering a controls name, so that I can remove the control when I need to redraw it. when trying to register the name it throws a NullReferenceException, can anyone see a problem with the code?
private void DrawSegment(Point p1, Point p2, Point p3, Point p4, bool reflexangle, Color clr) { // Segment Geometry PathSegmentCollection segments = new PathSegmentCollection(); // First line segment from pt p1 - pt p2 segments.Add(new LineSegment() { Point = p2 }); //Arc drawn from pt p2 - pt p3 with the RangeIndicatorRadius segments.Add(new ArcSegment() { Size = new Size(arcradius2, arcradius2), Point = p3, SweepDirection = SweepDirection.Clockwise, IsLargeArc = reflexangle }); // Second line segment from pt p3 - pt p4 segments.Add(new LineSegment() { Point = p4 }); //Arc drawn from pt p4 - pt p1 with the Radius of arcradius1 segments.Add(new ArcSegment() { Size = new Size(arcradius1, arcradius1), Point = p1, SweepDirection = SweepDirection.Counterclockwise, IsLargeArc = reflexangle }); // Defining the segment path properties Color rangestrokecolor; if (clr == Colors.Transparent) { rangestrokecolor = clr; } else rangestrokecolor = Colors.White; rangeIndicator = new Path() { StrokeLineJoin = PenLineJoin.Round, Stroke = new SolidColorBrush(rangestrokecolor), Fill = new SolidColorBrush(clr), Opacity = 0.65, StrokeThickness = 0.25, Data = new PathGeometry() { Figures = new PathFigureCollection() { new PathFigure() { IsClosed = true, StartPoint = p1, Segments = segments } } } }; //Set Z index of range indicator rangeIndicator.SetValue(Canvas.ZIndexProperty, 150); //give the UI element a name so that we can redraw it rangeIndicator.Name = "RangeIndicatorSeg"; rootGrid.RegisterName(rangeIndicator.Name, rangeIndicator); //this fails with a NullReferencException // Adding the segment to the root grid rootGrid.Children.Add(rangeIndicator); } void RedrawRange() { rootGrid.Children.Remove((UIElement)this.FindName("RangeIndicatorSeg")); DrawRangeIndicator(); }
Many Thanks,
Robin.
Welcome to my worldSaturday, August 7, 2010 1:37 PM
Answers
-
After further investigation I found that I needed to set the namespace programmatically...
This fixed the program.
Many Thanks,
Robin.
Welcome to my world- Marked as answer by Playwolf Thursday, September 9, 2010 12:15 PM
Thursday, September 9, 2010 12:15 PM
All replies
-
Hi Playwolf,
In that line, the only possible reason that will raise the NullRederenceException is that the rootGrid or rangeIndicator is null, so you can check those two variables if they are null.
Hope this helps.
Best regards,
Kevin Pan
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Tuesday, August 10, 2010 12:51 PM -
After further investigation I found that I needed to set the namespace programmatically...
This fixed the program.
Many Thanks,
Robin.
Welcome to my world- Marked as answer by Playwolf Thursday, September 9, 2010 12:15 PM
Thursday, September 9, 2010 12:15 PM