1

There are two spinners in my application.when i select a value from one spinner, according to the selection, values will be loaded to second spinner. The problem is when i select the second spinner, the application unfortunately stops. Thanks in advance.

public class CreateAppointment extends AppCompatActivity {

String[] lect_name = new String[100];
String[] spinner_subjects_load;
Spinner spSubject,spLecturer;
String chosedSubject;
String chosedLectName;
String chosedDateString;
RequestQueue requestQueue;
String url = "http://quick-appointment.b2creations.net/getLecturerNames.php";
CustomRequest customRequest;
String dayOfWeekString;
Calendar currentDate,chosedDate;
User user;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_appointment);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Intent intent = getIntent();
    user = (User) intent.getExtras().getSerializable("user");

    currentDate = Calendar.getInstance();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.format(currentDate.getTime());   //get current date


    spinner_subjects_load = new String[]{"SPDC", "HCI", "MAD", "SEIII", "SEP", "DAA", "ITP"};

    spSubject = (Spinner) findViewById(R.id.spSubject);
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner_subjects_load);
    spSubject.setAdapter(adapter1);

    requestQueue = Volley.newRequestQueue(this);

    spSubject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            chosedSubject = spSubject.getSelectedItem().toString();

            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("subj_name", chosedSubject);

            customRequest = new CustomRequest(Request.Method.POST, url, hashMap, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    int count = 0;
                    try {

                        JSONArray jsonArray = response.getJSONArray("Lecturers");
                        while (count < jsonArray.length()) {
                            JSONObject jo = jsonArray.getJSONObject(count);
                            lect_name[count] = jo.getString("name");
                            count++;
                        }

                        spLecturer = (Spinner) findViewById(R.id.spLecturer);

                        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(CreateAppointment.this, android.R.layout.simple_spinner_item, lect_name);
                        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spLecturer.setAdapter(adapter2);

                        chosedLectName = spLecturer.getSelectedItem().toString();
                        Toast.makeText(CreateAppointment.this, chosedLectName, Toast.LENGTH_SHORT).show();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

            requestQueue.add(customRequest);

        }


        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

}
António Paulo
  • 351
  • 3
  • 18
Nibras
  • 329
  • 1
  • 6
  • 23

1 Answers1

0

Looks like you initialize the second spinner spLecturer, and then immediately after you make a call to spLecturer.getSelectedItem().toString(), which I believe will return null at this point.

Perhaps you intended to add an OnItemSelectedListener to it first?

James Wierzba
  • 16,176
  • 14
  • 79
  • 120
  • Do i have to add the event OnItemSelectedListener to second spinner spLecturer?but i'm not going to handle anything for that spinner. – Nibras Apr 05 '16 at 15:01
  • Well, then you must change your code. Here is what you are currently doing: 1) create spinner, load items into it 2) call getSelectedItem. No item has been selected yet. Logic does not make sense – James Wierzba Apr 05 '16 at 15:08