title: Get the text from an EditText created dynamically by a function
body: <p>I am working on an Android application using Java. I'm using a spinner where it offers different options for a user to select and then I try to update the UI according to the selection, adding more user input for additional information or removing some of it. Everything works until I call the saveInfo() function which is supposed to print the text inside this new text input field that has been created with the addInput() function. Every time I print it, it returns null (&quot;java.lang.NullPointerException&quot;). Would anyone have an idea on how I can use a function to create a new EditText input and at the same time be able to retrieve information from it? If not, is there another solution someone would recommend to dynamically add multiple EditTexts to the screen? Thank you!</p>
  <p>Here is my code:</p>
  <pre><code>public class UserInfo extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
  
      private String[] bankNames={&quot;Teacher&quot;,&quot;Student&quot;,&quot;Parent&quot;,&quot;Worker&quot;,&quot;Another&quot;};
      //Commum variables:
      private EditText nameInput;
      //Teacher:
      private EditText inSchoolTitleInput;
      //Parent:
      //...
  
      private EditText neutralInput;
      private String neutralInputText;
  
      //String from spinner:
      private String selectedName;
  
      private LinearLayout layout;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_user_info);
  
          //Getting the instance of Spinner and applying OnItemSelectedListener on it
          Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
          layout = findViewById(R.id.linearLayout);
          spin.setOnItemSelectedListener(this);
  
          //Creating the ArrayAdapter instance having the bank name list
          ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
          aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          //Setting the ArrayAdapter data on the Spinner
          spin.setAdapter(aa);
      }
  
       @Override
      public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
          selectedName = bankNames[position];
          addFields();
  
      }
  
      @Override
      public void onNothingSelected(AdapterView&lt;?&gt; parent) {}
  
      //Checks what was selected from spinner and updates the screen
      public void addFields() {
          addCommonFields();
          switch (selectedName) {
              case &quot;Teacher&quot;: addTeacherFields();
                              break;
              //...
          }
      }
  
      public void addCommonFields() {
          layout.removeAllViewsInLayout();
          addInput(nameInput, &quot;Lucas Slepetys&quot;);
      }
  
      private void addTeacherFields() {
          addInput(inSchoolTitleInput, &quot;Math&quot;);
      }
  
      public void addInput(EditText field, String hint) {
          neutralInput = field;
          neutralInput = new EditText(this);
          neutralInput.setHint(hint);
          layout.addView(neutralInput);
      }
      
      public void saveInfo(View v) {
          switch (selectedName) {
              case &quot;Teacher&quot;:
                  System.out.println(neutralInput.getText().toString());
                  break;
              //...
          }
       }
  }
  
  </code></pre>
  <p>Thank you!</p>