0

I am working on a project where I have designed a custom control, and I am trying to add it to and locate it within a group box.

The steps of initialising and adding the control to the box work fine, but then anything I attempt to move or resize the control causes an exception.

        //initialise using object in outputs collection
        VitalsVisual vitalsVisual = vitalOutputs.getVitalsVisual();
        //add to relevant groupbox
        grpbxIntraOp.Controls.Add(vitalsVisual);
        //change location (coordinates within groupbox)
        vitalsVisual.Location = new Point(249, 256); //THROWS EXCEPTION
        //resize
        vitalsVisual.Size = new Size(494, 342); //THROWS EXCEPTION IF REACHED

All I get on either of the indicated lines is "Object reference not set to an instance of an object". I don't really understand this, as it would point to the VitalsVisual vitalsVisual not being initialised, but the constructor is called and the Controls.Add() command is working. Surely if it had not been initialised, this command would throw the same exception.

Can anybody spot what might be wrong here? Would really appreciate a nudge in the right direction!

Thanks, Mark

marcuthh
  • 509
  • 9
  • 35
  • Does `getVitalsVisual` actually return a value or does it return null? What does the `VitalsVisual` control inherit from? – kagelos May 21 '16 at 11:25
  • The proposed answer explains why you've failed to debug this correctly, but doesn't really solve the _problem_, i.e. that you have a null reference. Please review the extensive advice found in the marked duplicate for how to debug a `NullReferenceException`, so that you can get to a point where your question is more specific than "why am I getting this exception?" If you still have problems at that point, ask a new question in which you've provided a good [mcve] that reliably reproduces the problem, along with a precise explanation of what _specifically_ you're having trouble with. – Peter Duniho May 21 '16 at 20:54
  • Not a duplicate as he had wrongly assumed that a control had could add to a Controls collection can't be null. I have explained why this is a wrong assumption. Debugging the `getVitalsVisual` is the next step, but not the question here, really.. – TaW May 29 '16 at 08:46

1 Answers1

1

Interesting, but that is by design.

Test:

Button button = null;
this.Controls.Add(button);
button.Location = Point.Empty;

This does just the same, i.e. it throws at the last line, not when adding..

So it is allowed trying to add null objectes to a Controls collection.

I wrote 'trying' because the Controls.Add actually fails quietly:

Button button = null;
Console.WriteLine( this.Controls.Count + " controls now.";
this.Controls.Add(button);
Console.WriteLine( "Still " + this.Controls.Count + " controls.";

No changes in the count.

A look at the sources confirms this:

public virtual void Add(Control value) { if (value == null) return; ...

So to sum up: your function surely returns a null object but the error is suppressed. Is it a bug or a feature?

TaW
  • 48,779
  • 8
  • 56
  • 89