Link to home
Start Free TrialLog in
Avatar of razza_b
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_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> 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
Avatar of Gary Davis
Gary Davis
Flag of United States of America image

Well the selectedItem or selectedItem.Name must be null to cause the obj ref error (use a breakpoint to tell which). My guess it the Name property is null. Then check your code to see why.

Gary Davis
Webguild

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
Avatar of razza_b
razza_b

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
Avatar of razza_b

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"))
                {
ASKER CERTIFIED SOLUTION
Avatar of pivar
pivar
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of razza_b

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("Locator") || items.Name.Equals("Localizar"))
                            {
                                Default def = new Default();
                                BDContent.Child = def;
                                break;
                            }
                            if (items.Name.Equals("Transaction Details") || items.Name.Equals("Transação Miudezas"))
                            {
                                Welcome def = new Welcome();
                                BDContent.Child = def;
                                break;
                            }
Avatar of razza_b

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
before you recreate your treeview save the selected node of the treeview. after creation set selected node to the saved node.
Avatar of razza_b

ASKER

how do i save the selected node to set for new tree?

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;
		}

Open in new window

Avatar of razza_b

ASKER

no probs ill give it a try and take it from there thanks for your help :)