2
votes

In my subclass of NSWindowController, [self window] is null.

In my nib file THERE IS a link between the File's Owner (my subclass) and the Window View.

Why do I get this error ?

It stopped to work when I refactored the File Owner's class (the subclass of NSWindowController). I've updated it in the nib so I don't understand why it stopped to work.

Crashing line:

session = [NSApp beginModalSessionForWindow:[self window]];



   2011-10-25 12:27:14.377 MyApp [13161:b0f] *** Assertion failure in -[CBApplication _commonBeginModalSessionForWindow:relativeToWindow:modalDelegate:didEndSelector:contextInfo:], /SourceCache/AppKit/AppKit-1138.23/AppKit.subproj/NSApplication.m:3861
    2011-10-25 12:27:14.377 MyApp[13161:b0f] An uncaught exception was raised
    2011-10-25 12:27:14.378 MyApp[13161:b0f] Modal session requires modal window
    2011-10-25 12:27:14.380 MyApp[13161:b0f] (
        0   CoreFoundation                      0x92e01d87 __raiseError + 231
        1   libobjc.A.dylib                     0x9317e149 objc_exception_throw + 155
        2   CoreFoundation                      0x92d69619 +[NSException raise:format:arguments:] + 137
        3   Foundation                          0x9c41c36f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
        4   AppKit                              0x958a987d -[NSApplication _commonBeginModalSessionForWindow:relativeToWindow:modalDelegate:didEndSelector:contextInfo:] + 725
        5   AppKit                              0x958a1973 -[NSApplication beginModalSessionForWindow:] + 72
        6   MyApp                     0x00042ca3 -[CBWindowController showModal:] + 131
        7   MyApp                     0x00023c46 -[CBDocument showLinkWindow:shouldLinkAndUpdate:selectedOnly:] + 1174
        8   MyApp                     0x00023cb1 -[CBDocument linkAllRootItems:] + 81
        9   MyApp                     0x0002a9b4 -[CBApplicationDelegate linkAllItems:] + 100
        10  CoreFoundation                      0x92d57091 -[NSObject performSelector:withObject:] + 65
        11  AppKit                              0x956e1cb3 -[NSApplication sendAction:to:from:] + 232
        12  AppKit                              0x957d5caf -[NSMenuItem _corePerformAction] + 536
        13  AppKit                              0x957d592c -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 171
        14  AppKit                              0x957d4fb5 -[NSMenu _performActionWithHighlightingForItemAtIndex:sendAccessibilityNotification:] + 79
        15  AppKit                              0x95aaddab -[NSMenu performActionForItemAtIndex:] + 65
        16  AppKit                              0x95aaddde -[NSMenu _internalPerformActionForItemAtIndex:] + 45
        17  AppKit                              0x95ab200f -[NSMenuItem _internalPerformActionThroughMenuIfPossible] + 106
        18  AppKit                              0x9591ba10 -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] + 172
        19  AppKit                              0x9574a916 NSSLMMenuEventHandler + 452
        20  HIToolbox                           0x9b175920 _Z22_InvokeEventHandlerUPPP25OpaqueEventHandlerCallRefP14OpaqueEventRefPvPFlS0_S2_S3_E + 36
        21  HIToolbox                           0x9aff1803 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1602
        22  HIToolbox                           0x9aff0c80 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 482
        23  HIToolbox                           0x9b005aa9 SendEventToEventTarget + 76
        24  HIToolbox                           0x9b175de4 _ZL18SendHICommandEventmPK9HICommandmmhPKvP20OpaqueEventTargetRefS5_PP14OpaqueEventRef + 482
        25  HIToolbox                           0x9b175e4e SendMenuCommandWithContextAndModifiers + 70
        26  HIToolbox                           0x9b1e0697 SendMenuItemSelectedEvent + 275
        27  HIToolbox                           0x9b0423f9 _ZL19FinishMenuSelectionP13SelectionDataP10MenuResultS2_ + 129
        28  HIToolbox                           0x9b1d1574 _ZL14MenuSelectCoreP8MenuData5PointdmPP13OpaqueMenuRefPt + 608
        29  HIToolbox                           0x9b03a0b2 _HandleMenuSelection2 + 636
        30  HIToolbox                           0x9b039e31 _HandleMenuSelection + 53
        31  AppKit                              0x95646356 _NSHandleCarbonMenuEvent + 302
        32  AppKit                              0x955d662e _DPSNextEvent + 2196
        33  AppKit                              0x955d58ab -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 113
        34  AppKit                              0x955d1c22 -[NSApplication run] + 911
        35  AppKit                              0x9586618a NSApplicationMain + 1054
        36  MyApp                     0x000042f4 main + 36
        37  MyApp                     0x00002e06 start + 54

How the NSWindowController is created:

    CBWindowController *windowController = [[subClass alloc] init];
[windowController setRanAsModal:YES];
[windowController setDelegate:self];
[windowController setRootDocument:[NSApp mainWindowDocument]];
[windowController loadWindow];
[windowController centerOnMainWindow:sender];
2
Can you show a bit more context, e.g. what is self and how does it get made, what is window and how is it assigned? Refactoring doesn't change any nib names that you have coded in (e.g. @"MyNibName") so this is probably where your problem lies. - jrturton
@jrturton The filename of the xib file is not supposed to change. What I've updated in the xib file is the File Owner Class. Basically I've refactored the file owner name class. The code is supposed to work anyway. - aneuryzm
OK, some context would still be good though. My second guess is that you haven't reconnected the outlets after changing the file's owner class. - jrturton
@jrturton All my 50 outlets are already connected. are you saying that I should unlink them and reconnect them ? :( - aneuryzm
50!! Only as a last resort, then! Maybe just the window one, or check what window is pointing to in your line of code above. - jrturton

2 Answers

2
votes

[self window] must point to a valid window object. From your comments, it is not.

You must check and re-connect the window outlet, or, if there is no such outlet, ensure that a valid object is held in that variable.

Automatic refactoring doesn't appear to catch everything - so a search of your project for the old name would seem to be a worthwhile exercise after refactoring to prevent issues like this in the future.

2
votes

I'm willing to bet you are initializing it wrong. You need to specifiy the NIB name when you initialize your class, like so:

CBWindowController *windowController =
    [[subClass alloc] initWithWindowNibName:@"MyWindow" owner:nil];

Better yet, just fold that code into your window controller's init method.

Edit: Also, do NOT call loadWindow on your object, that method is called automatically when the window is accessed, as mentioned in the documentation.