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
Solved

object ref not set to instance of object

Posted on 2011-09-03
12
320 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…

840 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