Solved

object ref not set to instance of object

Posted on 2011-09-03
12
314 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now