title: filling two spinners based on selection of the first
body: <p>i am trying to populate two spinners based on the selection of the first so when i choose one of the options in the first spinner the second one is filled with the appropriate string array. I am kind of new in android and i can't find the mistake, i am getting an error on createFromResource inside the if loop . Here is my code:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
  Spinner spinner = (Spinner) findViewById(R.id.spinner);
      ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(
              this, R.array.locations, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(adapter);
  
      Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
      adapter = ArrayAdapter.createFromResource(
              this, R.array.locations, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner2.setAdapter(adapter);
  
      spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
              if(position==5) {
                  Spinner spinner2 = (Spinner) findViewById(R.id.spinner);
                  ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(this,
                          R.array.startlocations, android.R.layout.simple_spinner_item);
                  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                  spinner2.setAdapter(adapter);
              }
              else{
                  Spinner spinner2 = (Spinner) findViewById(R.id.spinner);
                  ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(this,
                          R.array.destinations, android.R.layout.simple_spinner_item);
                  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                  spinner2.setAdapter(adapter);
              }
  
  
              Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " is selected", Toast.LENGTH_LONG).show();
  
          }
  
          @Override
          public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
          }
      });
  </code></pre>