Home > Blockchain >  Error return new SearchInput. Object reference not set to an instance of an object
Error return new SearchInput. Object reference not set to an instance of an object

Time:09-10

I am trying this code and I got an error say System.NullReferenceException: Object reference not set to an instance of an object. when I run the website even though this code worked well in the tutorial.

private SearchInput GetSearchInput()
        {
            return new SearchInput()
            {
                id = txt_id.Text,
                email = txt_email.Text,
                gender = ddl_gender.SelectedItem.Text,
                fname = txt_fname.Text,
                lname = txt_lname.Text
            };

        }

I am coding on ASP.NET and C#. Please help.

I tried to put a variable

private SearchInput GetSearchInput()
        {
            SearchInput newinput = new SearchInput()
            {
                id = txt_id.Text,
                email = txt_email.Text,
                gender = ddl_gender.SelectedItem.Text,
                fname = txt_fname.Text,
                lname = txt_lname.Text
            };
            return newinput;
        }

but it still did not work.

CodePudding user response:

I would say that the problem is not with the object you are returning but with ddl_gender.SelectedItem.Text. My guess is either ddl_gender is null or ddl_gender.SelectedItem is null

  • Related