Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

object ref not set to instance of object

Posted on 2011-09-03
12
Medium Priority
?
328 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

721 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