How to set a variable from the selected text of a dropdown menu in Unity?

I am trying to set a variable from the value of a DropDown menu in Unity.

I wrote a script as follows, attached it to the dropdown and set it to run it in Unity under: On Value Changed (Int32).

I have confirmed it is running when the dropdown changes from the debug output. However, it is giving me the NullReferenceException: Object reference not set to an instance of an object error when trying to get the dropdown’s value.

The function I am running is ChangeLocationDropDown():

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;  public class LocationSelectorDropDown : MonoBehaviour { public void ChangeLocationDropDown() {         Debug.Log("DROP DOWN CHANGED");         Dropdown m_Dropdown;         m_Dropdown = GetComponent<Dropdown>();         Debug.Log(m_Dropdown.options[m_Dropdown.value].text);     } } 

I get the DROP DOWN CHANGED debug output but when I try to get the second debug it says:

NullReferenceException: Object reference not set to an instance of an object LocationSelectorDropDown.ChangeLocationDropDown ()

This Unity page suggests fixing this error by somehow using GameObject.Find to manually find the dropdown object first by its string name. But this is incredibly impractical and not a proper solution. I may need to make dozens of dropdowns. I need some simple clean way for each value to go into its own variable and/or update a database with their value with every change made.

What is the proper way to handle this?

ie. How do you update a string variable with the new text value of the DropDown?