tags:

views:

24

answers:

2

i have code like

e.CanExecute = tabEditor.IsFocused;

but at runtime, i get a null reference exception error. but in the docs, IsFocused returns only true or false, why is this happening? btw, tabEditor is a <TabItem>

A: 

Most likely, this is happening because at some point tabEditor is being set to null.

Dimitar
the tabEditor is set to null. but why. it refers to a TabItem in my XAML `<TabItem Header="Editor" x:Name="tabEditor">`
jiewmeng
see Marks answer.
Dimitar
+2  A: 

Chances are that either e or tabEditor is null, i.e., they weren't assigned to an object anywhere (or otherwise explicitly assigned to null). If the code is in an event handler, it's possible that the framework hasn't initialized all the variables by the time the event handler is called, in which case you might want to check for null.

Mark Cidade
`tabEditor` is null, but why? the code is inside a `Bold_CanExecute(object sender, CanExecuteRoutedEventArgs e)` and tabEditor refers to `<TabItem Header="Editor" x:Name="tabEditor">` a TabItem inside my XAML. also you may have noticed from the function signature that i am using Routed Commands. i also noticed that i have not explicitly called the command yet. the error is produced on application start
jiewmeng
I updated my answer.
Mark Cidade