On Item Selection Issue...

I am missing something here and getting an error. I am new to c# so please excuse the trivial question I am sure.

"System.NullReferenceException: 'Object reference not set to an instance of an object.'"


Here is the code for SelectionChanged Event

private void lvAddressTypes_SelectionChanged(object sender, Syncfusion.WinForms.ListView.Events.ItemSelectionChangedEventArgs e)

        {

            //========================================

            // Populate textbox upon selection

            //========================================

            string oldAddressType = (lvAddressTypes.SelectedItems[0] as dsBigTopERP.tbl_AddressTypeRow).AddressTypeText.ToString();

            txtAddAddressType.Text = oldAddressType;

        }


Tried to follow this sample code posted here in the above code.

            string Sele��o = (sfListView1.SelectedItems[0] as USState).LongName.ToString();
           
            MessageBox.Show(Sele��o);

3 Replies

GT Gokul Thanudhas Syncfusion Team July 20, 2022 03:21 PM UTC

Hi Donald


The Nullexception can be handled by using the null check condition. Please refer to the link for your reference to handle null exceptions.



If the issue persists, please share the issue reproducible sample so that we can try to solve the issue from our end.


Regards,

Gokul T



DO Donald July 20, 2022 05:16 PM UTC

This item should not be null though. The listview has be databound and has 5 entries in it. When I click on an entry the code above gives me the null exception.

Form Load Event

lvAddressType.DataSource = dsBigTopERP.GetAddressTypes();

lvAddressType.DisplayMember = "AddressTypeText"

lvAddressType.ValueMemner = "idAddressType"

...


On Selection Click Event

string oldAddressType = (lvAddressTypes.SelectedItems[0] as dsBigTopERP.tbl_AddressTypeRow).AddressTypeText.ToString();


As far as the code, I could share with you privately only.



BA BagavathiPerumal Apranandham Syncfusion Team July 25, 2022 05:03 AM UTC

Hi Donald,


We have already created ticket for the mentioned query under your account. Please follow the ticket for further updates. And you can share the files in the ticket.



Regards,
Bagavathi Perumal A


Loader.
Up arrow icon