razza_b
asked on
object ref not set to instance of object
Hi Experts
Im getting the error - object ref not set to instance of object. What it is is i have a silverlight treeview and it can be displayed in english or brazil portuguese when i choose the langauge from a combobox. Selecting any language is fine and tree displays no bother.
for example if i select english langauage from combo i can go into treeview and select any node, but if i then go to combo and choose brazil portuguese thats when i get the error and vice versa.
it fails at line below in bold(thats the child node im selecting)..
private void treeView1_SelectedItemChan ged(object sender, RoutedPropertyChangedEvent Args<objec t> e)
{
var selectedItem = (UserRoleTreeData.Level)e. NewValue;
foreach (UserRoleTreeData.Level item in treeView1.Items)
{
#region General User Tools
if (item.Name.Equals("General User Tools") || item.Name.Equals("Geral Usuário Ferramentas"))
{
foreach (UserRoleTreeData.Level items in item.Children)
{
if (selectedItem.Name.Equals( "Locator") || selectedItem.Name.Equals(" Localizar" )) {
please see attachments
Thanks
multi-lingual-treeviews.docx
Im getting the error - object ref not set to instance of object. What it is is i have a silverlight treeview and it can be displayed in english or brazil portuguese when i choose the langauge from a combobox. Selecting any language is fine and tree displays no bother.
for example if i select english langauage from combo i can go into treeview and select any node, but if i then go to combo and choose brazil portuguese thats when i get the error and vice versa.
it fails at line below in bold(thats the child node im selecting)..
private void treeView1_SelectedItemChan
{
var selectedItem = (UserRoleTreeData.Level)e.
foreach (UserRoleTreeData.Level item in treeView1.Items)
{
#region General User Tools
if (item.Name.Equals("General
{
foreach (UserRoleTreeData.Level items in item.Children)
{
if (selectedItem.Name.Equals(
please see attachments
Thanks
multi-lingual-treeviews.docx
Hi,
The error say that either selectedItem or selectedItem.Name is NULL. You can set a breakpoint at that line and check which one it is.
My guess is that when you change language you recreate the treeview and do not reselect the, in previous language, selected node.
/peter
The error say that either selectedItem or selectedItem.Name is NULL. You can set a breakpoint at that line and check which one it is.
My guess is that when you change language you recreate the treeview and do not reselect the, in previous language, selected node.
/peter
ASKER
Hi
The selectedItem is null when i change langauge.
How would i be able to make sure when i change langauge that the node selected is still selected or just to make sure that i dont get the error and make user re-select a node?
Thanks
The selectedItem is null when i change langauge.
How would i be able to make sure when i change langauge that the node selected is still selected or just to make sure that i dont get the error and make user re-select a node?
Thanks
ASKER
Ive just realized im not really doing anything with 2nd for loop, im just doing selectedItem, but what i notice is this part seems to be fine when i change language...
foreach (UserRoleTreeData.Level item in treeView1.Items)
{
if (item.Name.Equals("General User Tools") || item.Name.Equals("Geral Usuário Ferramentas"))
{
foreach (UserRoleTreeData.Level item in treeView1.Items)
{
if (item.Name.Equals("General
{
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes this is fine since you loop through the list of existing items which is not null.
foreach (UserRoleTreeData.Level item in treeView1.Items)
{
if (item.Name.Equals("General User Tools") || item.Name.Equals("Geral Usuário Ferramentas"))
{
selectedItem comes from the current selected item. And since there is no selection, this is null.
foreach (UserRoleTreeData.Level item in treeView1.Items)
{
if (item.Name.Equals("General
{
selectedItem comes from the current selected item. And since there is no selection, this is null.
ASKER
but i need to use this value to get the new value..
var selectedItem = (UserRoleTreeData.Level)e. NewValue;
and if i use 2nd for loop it seems fine when i change language but when i select another node i.e. transaction details it still goes into locator code..
foreach (UserRoleTreeData.Level items in item.Children)
{
if (items.Name.Equals("Locato r") || items.Name.Equals("Localiz ar"))
{
Default def = new Default();
BDContent.Child = def;
break;
}
if (items.Name.Equals("Transa ction Details") || items.Name.Equals("Transaç ão Miudezas"))
{
Welcome def = new Welcome();
BDContent.Child = def;
break;
}
var selectedItem = (UserRoleTreeData.Level)e.
and if i use 2nd for loop it seems fine when i change language but when i select another node i.e. transaction details it still goes into locator code..
foreach (UserRoleTreeData.Level items in item.Children)
{
if (items.Name.Equals("Locato
{
Default def = new Default();
BDContent.Child = def;
break;
}
if (items.Name.Equals("Transa
{
Welcome def = new Welcome();
BDContent.Child = def;
break;
}
ASKER
your right pivar using this works great..
if (selectedItem != null)
it makes user just reselect the node they want without error when changing language, but how could i make the prevoius selected node still be selected?
Thanks
if (selectedItem != null)
it makes user just reselect the node they want without error when changing language, but how could i make the prevoius selected node still be selected?
Thanks
before you recreate your treeview save the selected node of the treeview. after creation set selected node to the saved node.
ASKER
how do i save the selected node to set for new tree?
Thanks
Thanks
Something like this.
UserRoleTreeData.Level selected = (UserRoleTreeData.Level)treeView1.SelectedItem; Save selected node
// Recreate the treeview
SelectNode(treeView1.Items, selected.Header.ToString()); // Select previous node
}
private UserRoleTreeData.Level SelectNode(ItemCollection collection, string header)
{
if (collection == null)
{
return null;
}
foreach (UserRoleTreeData.Level node in collection)
{
if (node.Header.Equals(header))
{
node.IsSelected = true;
return node;
}
if (node.Items != null)
{
UserRoleTreeData.Level childNode = this.SelectNode(node.Items, header);
if (childNode != null)
{
node.IsExpanded = true;
return childNode;
}
}
}
return null;
}
ASKER
no probs ill give it a try and take it from there thanks for your help :)
Gary Davis
Webguild