Null Reference Exception

in sharp win-form

I added a list control to my form and the elements added, respectively, in the combo box m, trying to execute the element in the select-index element, is assigned to a string, which is passed as a parameter to a function declared as follows:

private void cmbPayment_SelectedIndexChanged(object sender, EventArgs e)
    {
        string pm = cmbLocation.SelectedItem.ToString();
        payment(pm);
    }

FUNCTION:

public void payment(string pym)
    {
        jd.PaymentMode = pym;

    }

alt text http://img42.imageshack.us/img42/8691/adssd.png

+3
source share
5 answers

It seems to me that "cmbLocation" should be "cmbPayment"?

+7
source

There seems to be no element in cmbLocation. If no item is selected, the SelectedItem property will be NULL, and you cannot call toString on null.

cmbLocation cmbPayment? cmbPayment.

- null:

private void cmbPayment_SelectedIndexChanged(object sender, EventArgs e)
{
    if(!cmbPayment.SelectedItem == null)
    {
        string pm = cmbLocation.SelectedItem.ToString();
        payment(pm);
    }
}

, NullPointerExceptions.

+2

, .

, , , , , cmdPayment , . cmbLocation

+1

(ToString()) SelectedItem cmbLocation. SelectedItem null/Nothing ( cmbLocation null/Nothing), NullReferenceException

0

, , SelectedItem .

It is worth considering how you could solve this: When debugging, you should be able to see what is null, presumably SelectedItem? It is also worth noting that you can see the object sender, which is the control that raised the event. In the debugger, you can look at and compare the selection, seeing what can interfere with the implementation cmbLocationcompared to sender.

0
source

Source: https://habr.com/ru/post/1728613/


All Articles