This project has moved. For the latest updates, please go here.
1
Vote

Null Reference Exception is thrown for this call: EventAggregator.Publish(new ViewNavigationArgs(typeof(MyView)));

description

Null Reference Exception is thrown for this call:
EventAggregator.Publish(new ViewNavigationArgs(typeof(MyView)));
because Logger is not set in the Jounce built in EventAggregatorService class after this call
CompositionInitializer.SatisfyImports(this) in the ApplicationService class.
This only happens when I export the routing from the View and not when I use moduleIntializer and I do not provide an implementation for ILogger interface.

The reason is that this call CompositionInitializer.SatisfyImports(this) creates an instance of MainPage view when I expose the ViewModelRoute through the view and the call to this statement in
public void OnImportsSatisfied()
{
EventAggregator.Publish(new ViewNavigationArgs(typeof(MenuView)));
}
generates the Null ref exception because no ILogger implementation is found in my code nor the default one has been created yet.

Can the Logger property return the built in Logger when not set yet ?

comments

mokdes wrote Jan 10, 2013 at 6:12 PM

I have updated ApplicationService class and replaced this block of code:
        _mainCatalog = new AggregateCatalog(new DeploymentCatalog()); // empty one adds current  deployment (xap)

        var container = new CompositionContainer(_mainCatalog);

        CompositionHost.Initialize(container);
        CompositionInitializer.SatisfyImports(this);

        if (Logger == null)
        {
            ILogger defaultLogger = new DefaultLogger(logLevel);
            container.ComposeExportedValue(defaultLogger);
        }
        else
        {
            Logger.SetSeverity(logLevel);
        }
with this one and it worked fine: ( the logger is available before objects are created)
       _mainCatalog = new AggregateCatalog(new DeploymentCatalog()); // empty one adds current   deployment (xap)
        _container = new CompositionContainer(_mainCatalog);
        CompositionHost.Initialize(_container);

        var customLogger = _container.GetExportedValues<ILogger>().FirstOrDefault();
        if (customLogger == null)
        {
            ILogger defaultLogger = new DefaultLogger(logLevel);
            _container.ComposeExportedValue(defaultLogger);
        }
        CompositionInitializer.SatisfyImports(this);

        Logger.SetSeverity(logLevel);

wrote Feb 14, 2013 at 8:20 PM