0

I have a form where the first question is, does the person want to answer these questions? If it equals "No" then hide the other inputs. When i hide the inputs i want to erase the fields so they are blank.

When i hide my combobox and use: calledUs.SelectedIndex = -1; it returns an error saying

Object reference not set to an instance of an object

but it does what i want it to, just with an error.

Am i missing something to overcome the Null reference? I have been reading the following Stack Overflow questions:

Combobox text when clearing items

What is a NullReferenceException, and how do I fix it?

Any help would be great.

UPDATE:

Code Example:

 private void wantToAnswer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (wantToAnswer.SelectedItem.ToString() == "Yes")
            {
                //THIS IS THE PANEL THE COMBOBOX IS WITHIN
                clothingCaughtFire.Visible = true; 
                Refresh();
            }
            else
            {
                calledUs.SelectedIndex = -1;
                //THIS IS THE PANEL THE COMBOBOX IS WITHIN
                clothingCaughtFire.Visible = false;
                Refresh();
            }
        }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Gary Henshall
  • 72
  • 2
  • 11
  • 3
    Are you using `calledUs.SelectedIndex = -1;` in the constructor? If so, do you use `InitializeComponent();` before or after using that? – Nathangrad Jun 15 '17 at 09:01
  • Or do you have a SelectedIndexChanged event handler? And the exception comes from that code. – Steve Jun 15 '17 at 09:05
  • I use `InitializeComponent();` before using the `SelectedIndex` statement. – Gary Henshall Jun 15 '17 at 09:05
  • When you set the SelectedIndex to -1 the event handler is called but at that point the SelectedItem is null and calling ToString on it raises the exception – Steve Jun 15 '17 at 09:13
  • 1
    `wantToAnswer.SelectedItem` can well be `null` and `wantToAnswer.SelectedItem.ToString()` will throw the exception – Dmitry Bychenko Jun 15 '17 at 09:14
  • However it is not clear what relationship exists between the calledUS combobox and the wantToAnswer_SelectedIndexChanged event handler. Is the latter binded to the OnSelectedIndexChanged event handler of the calledUS combobox? – Steve Jun 15 '17 at 09:17
  • @DmitryBychenko Did you propose to use `.SelectedItem?.toString()` ? This has solved the issue not in this block of code but further down in my code. If you want to add the answer i will accept and edit it to show the actual fix? – Gary Henshall Jun 15 '17 at 09:26
  • 1
    @Gary Henshall: `wantToAnswer.SelectedItem == null` is a possible cause of the exception; `.SelectedItem?.toString()` doesn't spoil anything and that's why worth trying – Dmitry Bychenko Jun 15 '17 at 09:28
  • @DmitryBychenko Put this an answer please and i will accept it. It fixed the issue. – Gary Henshall Jun 15 '17 at 09:30

4 Answers4

1

As a quick amendment I suggest adding ? in the condition:

  ...

  // ?. instead of . :
  // in case wantToAnswer.SelectedItem == null the condition now is null == "Yes"
  if (wantToAnswer.SelectedItem?.ToString() == "Yes")

  ...

Since wantToAnswer.SelectedItem can well be null and in this case wantToAnswer.SelectedItem.ToString() will throw NullReferenceException exception

In Addition to this. The problem was further down where i was doing the check on the calledUs Combobox. See below:

private void wantToAnswer_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (wanttoAnswer.SelectedItem?.ToString() == "Yes")
        {
            clothingCaughtFire.Visible = true;
            Refresh();
        }
        else
        {
            calledUs.SelectedIndex = -1;
            clothingCaughtFire.Visible = false;
            Refresh();
        }
    }

    private void calledUs_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (calledUs.SelectedItem?.ToString() == "Yes - Other")
        {
            otherClothingFire.Visible = true;
            Refresh();

        }
        else
        {
            otherClothingFire.Visible = false;
            otherSpecify.Text = "";
            Refresh();
        }
    }
Gary Henshall
  • 72
  • 2
  • 11
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • This wouldn't be the combobox that i am setting to `SelectedIndex = -1`. This is the combobox that is driving the decision if we hide the other elements or not. And in this case wouldn't be Null. Would this still be needed? – Gary Henshall Jun 15 '17 at 09:19
1

You're getting the error because you're trying to set a property of an object that is null.

Consider the following

Dog rex;
rex.Colour = "Brown";

You can't set rex's colour until after you've instantiated him. For example,

Dog rex = new Dog();
rex.Colour = "Brown";

Trying to do this calledUs.SelectedIndex = -1 is the same thing. Until calledUs is instantiated you can't access its SelectedIndex.

You can avoid this from happening by checking that it exists before you access it. For example,

if (calledUs != null) // Do what you need to do
Gareth
  • 913
  • 6
  • 14
0

That error indicates that the calledUs object is null at the time. Make sure the object has been instantiated before setting the selected index.

CathalMF
  • 9,705
  • 6
  • 70
  • 106
0

put your code after InitializeComponent(), or in Form_Load() where your controls need to be initialized inorder to access

Vicky S
  • 762
  • 4
  • 16
  • Yes this may reduce the likelihood of the problem occurring but it won't solve the problem. If `calledUs` is null at any point you will still get the exception. – Gareth Jun 15 '17 at 09:25
  • yes you are right, is it possible that a control can be null even after initialized i don't think a control will become null unless we manually update – Vicky S Jun 15 '17 at 09:30