Solved

object ref not set to instance of object

Posted on 2011-09-03
12
321 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:razza_b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 18

Expert Comment

by:Gary Davis
ID: 36477728
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

0
 
LVL 22

Expert Comment

by:pivar
ID: 36477745
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
0
 
LVL 1

Author Comment

by:razza_b
ID: 36478466
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
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 1

Author Comment

by:razza_b
ID: 36478485
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"))
                {
0
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 36478495
Either save selected item before recreation and reselect the item in newly created list or just check for null after

 m selecting)..
        private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var selectedItem = (UserRoleTreeData.Level)e.NewValue;
             if (selectedItem != null) {
            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"))                        {

.
.
.
}
0
 
LVL 22

Expert Comment

by:pivar
ID: 36478508
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.
0
 
LVL 1

Author Comment

by:razza_b
ID: 36478512
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;
                            }
0
 
LVL 1

Author Comment

by:razza_b
ID: 36478532
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
0
 
LVL 22

Expert Comment

by:pivar
ID: 36478548
before you recreate your treeview save the selected node of the treeview. after creation set selected node to the saved node.
0
 
LVL 1

Author Comment

by:razza_b
ID: 36478636
how do i save the selected node to set for new tree?

Thanks
0
 
LVL 22

Expert Comment

by:pivar
ID: 36478695
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

0
 
LVL 1

Author Comment

by:razza_b
ID: 36478810
no probs ill give it a try and take it from there thanks for your help :)
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question