john84
john84

Reputation: 2451

NullReferenceException when accessing GUI Components in Controllers constructor

In Mono I have a simple NSWindow with Controller on it I drop a NSSplitView and a NSButton.

If I try to access the NSSplitView out of the Contstructor or Initialize() Method I get a nullReferenceException. Instead if I try to access the NSSplitView from a ButtonClicked Method, it works.

Well, it seems that the Framework creates the GUI Components after the controller's Construtor is called. But where should I put my code to configure GUI Components if not in the Constructor?

Thanks in advance. Johannes

Upvotes: 3

Views: 196

Answers (2)

rFlex
rFlex

Reputation: 460

You should use

public override void ViewDidLoad() {
    base.ViewDidLoad();
}

for configuring your view because that's the method which is called when the view just loaded, not when you instantiated the controller. If you configure your view in AwakeFromNib() you'll force the instantiation of your view whereas you maybe don't need them now. Indeed, when you create a controller the view are not created until someone try to access viewController.View.

Upvotes: 0

Laurent Etiemble
Laurent Etiemble

Reputation: 27889

You should use the AwakeFromNib method in your controller class. It is called once all the objects have been loaded and properly connected.

public override void AwakeFromNib ()
{
    base.AwakeFromNib ();

    // Do something here with the outlets
}

Upvotes: 4

Related Questions