0

Hi I have written a program in WPF C# to scan for robot controllers, display their data then start and stop running when the specific button is pressed.

I can scan and display the information but when I double click on the listview item, its information is not assigned to controller.

    private void listView1_DoubleClick(object sender, MouseButtonEventArgs e)
    {
        ListViewItem item = sender as ListViewItem; //this.listView1.SelectedItems[0];
        if (item != null)
        {
            ControllerInfo controllerInfo = (ControllerInfo)item.Tag;
            if (controllerInfo.Availability == Availability.Available)
            {
                if (controllerInfo.IsVirtual)
                {
                    this.controller = ControllerFactory.CreateFrom(controllerInfo);
                    this.controller.Logon(UserInfo.DefaultUser);
                    listView1.Items.Clear();
                    listView1.Items.Add(item);
                    EnableControllerFunctionality();
                }

thanks

1 Answers1

0

You could easily debug this by setting a breakpoint, but yes, I also assume that controller is null. You need to check the place where controller is set and why controller is set to null.


The following segment seems to assign a new controller from the controllerInfo you get from the list item. You say that it doesn't. While a simple breakpoint and a step-through debugging process would shed more light on what's going on, I can write down some wild guesses:

Possible problems:

  1. item == null: The code block doesn't execute at all
  2. controllerInfo.Availability != Availability.Available: The code block doesn't execute
  3. controllerInfo.IsVirtual == false: The assignment doesn't execute
  4. ControllerFactory.CreateFrom returns null: You should get an exception in the line this.controller.Logon(UserInfo.DefaultUser);
  5. Possibly you set this.controller to null somewhere further down in your code.

So which one is it?

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • I can now see that controller = null, the second segment of code is my listview double click event handler, this should assign the value of the selected item to controller, but it doesn't. Any idea why? – MechatronicsStudent May 19 '14 at 11:39
  • I edited my answer. You *must* debug your code to see what the problem is. Your code looks alright, but the problem can not be diagnosed by just looking at the code. – Thorsten Dittmar May 19 '14 at 12:35
  • I have gone through and debugged the code to see where the first mistake is `ListViewItem item = sender as ListViewItem;` seems to be the problem as item is null – MechatronicsStudent May 19 '14 at 13:24
  • Possibly 'as' is not working as a converter and therefore returning `item=null` – MechatronicsStudent May 19 '14 at 13:29
  • Do I need to bind the selected object to 'item'? if so how? – MechatronicsStudent May 19 '14 at 14:00
  • `sender` is not the selected item, but the instance of `ListView` that you clicked. So it is clear that `sender as ListViewItem` returns `null` - if you cast directly, you get an exception instead of `null`. Stop playing and *debug* your original code! – Thorsten Dittmar May 19 '14 at 14:19
  • Just had a meeting with my supervisor and results are apparently more important than a working method. Dumbing down the ListView to only display 1 Robot controller, then no selection issues as I can copy from where the listview is getting the info from. Hopefully after I have results I can go back and make it friendly to selecting different controllers – MechatronicsStudent May 19 '14 at 15:44
  • I know I'm repeating myself, but how long could it have taken to simply set a breakpoint and analyze what's going on by single-stepping through the code... – Thorsten Dittmar May 20 '14 at 06:44
  • I actually set breakpoints to see which variables were not being a signed properly and it was `item` which was not being assigned the selected list view item, I unfortunately exhausted the time assigned to this part of the project and needed results, hopefully I can work out why `listview1.selecteditem` doesn't work properly at a later date – MechatronicsStudent May 21 '14 at 07:27