I have been googling this problem for 2 days, and all the solutions I’ve seen doesn’t quite fit my scenario, or the fix doesn’t work.
I have a WPF desktop application. When I’m debugging the project everythings is working fine and 100%. But the moment I deploy my app (even on the PC I developed it), it crashes. It seems to crash on the first class instantiation it finds and throws an exception who’s innerException is always a nullreference exception. The former exception keeps changing though, like TargetInvocationException, TypeInitializationException etc.
Here is the stacktrace:
System.TypeInitializationException: The type initializer for 'eTute.BusinessLayer' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at eTute.BusinessLayer..cctor()
--- End of inner exception stack trace ---
at eTute.BusinessLayer.EncryptPassword(String password)
at eTute.MainWindow.imgLock_MouseDown(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
The “BusinessLayer” is like my Data Access layer (DAL). At first it was instance driven, but i changed the whole class to be static when it started crashing on the BusinessLayer’s contructor. Now it seems to just crash at the next instantiation.
NOTE: To create the installer, I just used Visual Studio’s Setup Wizard. The app uses a SQL Express database, but it never even gets to use it, since it crashes on any little instance.
Please, any help or comment will be profoundly appreciated. I have never done an install before, but feel that it shouldn’t be this complicated!
UPDATE
The problem seemed to be related to accessing the connectionStrings in the app.config file. When I replaced the code that said ConfigurationManager.ConnectionStrings["name"].ConnectingString with a hardcoded string variable, the problem disapeared.
But then how am I supposed to use the app.config file when my app is deployed?
So after a few struggles, I was able to determine the problem with my install file. I followed a tutorial on a website and it actually taught me to do something that should actually not be done, as is why the error occurred.
The problem was that the app.config file was not in the same folder as the exe of the app. Therefore, the exe could not locate the app.config file upon start up, and thus the app crashed.
I hope this helps someone with a similar problem. Was a quite easy fix, if I only new why it occurred. 😉