4
Vote

Null reference handling

description

While testing the ServiceLocator.Current.GetInstance method, I passed a null to verify the libraries design correctness (we use Microsoft Basic Design Guidelines Rules, and try to test our external libraries for them).
 
I ran into this exception when calling:
 
ServiceLocator.Current.GetInstance(null);
 
Stack Trace:
 
at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.FormatActivationExceptionMessage(Exception actualException, Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 149
at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 53
at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 34
at DiscoveryPrototype.ClientApp.Program.Main(String[] args) in D:\Projects\DiscoveryPrototype\DiscoveryPrototype.ClientApp\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
 
 
The ServiceLocatorImplBase should really have null checks in all methods for all used parameters. Although it is an edge case, you could ask for an instance of an object passing in the type that came from the return of a method that DOES return null, and then the exception is not caught at the proper place.

comments

jalalx wrote Apr 15, 2015 at 6:47 AM

And now I'm going insane for that problem!