At my place of work, we recently updated our code base from .Net 3.5 to .Net 4 (C #). Most of the problems that were discovered have been resolved, however I cannot understand. We initialize the controls and pages through a combination of xaml and code behind (based on developer preferences), however one page throws a NullReferenceException on opening. Here is a piece of code that (one of many controls) throws.
All exceptions for code exceptions are inside the DataTemplate (I realized that this may be relevant)
<TextBox x:Name="Values" Grid.Column="1" Grid.Row="0" Margin="5,2,5,2" Text="{Binding ElementName=Descriptions, Path=SelectedValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource EmptyConverter}}" GotFocus="Column_GotFocus" MinWidth="100" CharacterCasing="Upper" Visibility="{Binding Path=IsValueVisible, Converter={StaticResource VisibilityConverter}}" />
Now in this case, throwing lines:
GotFocus="Column_GotFocus"
Column_GotFocus is located in the code behind the file. A few more facts: before the migration, we had no problems, the exception receives throws continuously, and there are three different events that cause this problem.
Three events throw:
GotFocus="Column_GotFocus" SelectionChanged="Descriptions_SelectionChanged" Click="Search_Click"
Removing these fixes completely fixes the problem, but obviously causes functional problems with the software. Does anyone know what might cause these problems?
EDIT:
Sorry to clarify: an event handler is never called, adding an xaml event handler (GotFocus = "Column_GotFocus", for example) where an exception is thrown.
The exception is:
System.NullReferenceException occurred Message=Object reference not set to an instance of an object. Source= <assembly/namespace> StackTrace: at <assembly/namespace>.<Class>.System.Windows.Markup.IStyleConnector.Connect(Int32 connectionId, Object target) in <XamlFilePath>:line 291 InnerException:
Edit 2:
Method Handler:
private void Column_GotFocus(object sender, RoutedEventArgs e) { ContentPresenter columnContentPresenter =(DependencyObject)sender).FindParent<ContentPresenter>(); Column column = (Column)columnContentPresenter.Content; string message = string.Format("{0} ({1})", column.Name, column.Field); ModuleDescriptor.UpdateStatusBar(message); }