0

Please read through and don't mark this question as duplicate. I have 2 Spinner elements which one of them is populated with String values hardcoded in my app and the other one is populated from my database using an ArrayList. The 2 Spinners are called Names and Sickness.

Code for NamesSpinner:

//method to fill the spinner with data
private void loadNames() {
    //database handler
    LysandrosDatabaseAdapter db = new LysandrosDatabaseAdapter(getApplicationContext());

    List<DataBean> list = db.getAllDat();
    //list for storing the names
    String[] nameList = new String[list.size()];
    //list for storing the ID and keeping it hidden
    employeesList = new int[list.size()];

    for (int i=0; i<list.size(); i++) {
        employeesList[i] = list.get(i).getID();
        nameList[i] = list.get(i).getName() + " " + list.get(i).getSurname();
    }
    //creating adapter for spinner
    ArrayAdapter<String > dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, nameList);
    //drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //attaching data adapter to spinner
    selectName.setAdapter(dataAdapter);
}  


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // On selecting a spinner item
    String list = parent.getItemAtPosition(position).toString();
    employeeId =  employeesList[position];
    //showing selected spinner item
    Toast.makeText(parent.getContext(), "You selected: " + list, Toast.LENGTH_LONG).show();

}` 

In this list, I pass my employees name with surname and ID and I achieve that by using a getters and setters class.

My getters and setters class:

public class DataBean implements Serializable {

//employee fields
protected int _id;
protected String name;
protected String surname;
protected String dateofbirth;
protected String department;
protected String workplace;
protected String number;
protected String bnumber;
protected String email;
protected String address;
protected String notes;


public DataBean (int _id, String name, String surname, String dateofbirth, String department, String workplace, String number, String bnumber, String email, String address, String notes ) {
    this._id = _id;
    this.name = name;
    this.surname = surname;
    this.dateofbirth = dateofbirth;
    this.department = department;
    this.workplace = workplace;
    this.number = number;
    this.bnumber = bnumber;
    this.email = email;
    this.address = address;
    this.notes = notes;
}

public DataBean (String name, String surname, String department, String workplace) {
    this.name = name;
    this.surname = surname;
    this.department = department;
    this.workplace = workplace;

}

public int getID() {return this._id;}
public int setID(int id) {return this._id = _id;}
public String getName() {return this. name;}
public String getSurname() {return this.surname;}
public String getDateofbirth() {return this.dateofbirth;}
public String getDepartment() {return this.department;}
public String getWorkplace() {return this.workplace;}
public String getNumber() {return this.number;}
public String getBnumber() {return this.bnumber;}
public String getEmail() {return this.email;}
public String getAddress() {return this.address;}
public String getNotes() {return this.notes;}


}

The weird thing is happening when my SicknessSpinner comes into play. You see, I add employees in my app through a different form and then they get passed onto here to be used for other purposes.

Here's the deal: If for example, I have 5 values in my Names spinner and 10 values in my Sickness spinner, I will get an ArrayIndexOutOfBoundsException if I select let's say the 5th value in my Names spinner and the 6th value in my Sickness spinner. The same will happen If I select the 7th, 8th, 9th value and so on in my Sickness spinner. So anything selected in the Sickness Spinner that is greater than the size of the Names crashes my application and gives me the exception.

I have no idea why this is happening since my array doesn't have a fixed size length. I've read all about the documentation here and here and I understand how arrays work and that they are indexed from position 0 and not 1. I also searched all over Stack Overflow for an answer which lead me to this answer and this one and many more but didn't really provide me with an answer. I even read multiple times this answer which made me understand how the exception is caused but my case is different.

Exception is happening on this line: employeeId = employeesList[position];

If I remove this line, my code works fine but the purpose of my app is compromised. Purpose of this, is to add an employee sickness by selecting the employee name from the name list and a type of sickness from the sickness list. If I have 3 employees, and 8 sicknesses, I won't be able to do this if I select the 4th or 5th sickness. Any help is greatly appreciated.

This is my logcat:

java.lang.ArrayIndexOutOfBoundsException: length=4; index=6
        at com.example.lysandroslysandrou.marstest4.AddSicknessForm.onItemSelected(AddSicknessForm.java:160)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:897)
        at android.widget.AdapterView.access$200(AdapterView.java:48)
        at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:865)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Community
  • 1
  • 1
Kirie_Zolo
  • 25
  • 9
  • This is not a duplicate, please read through my descriptions. I clearly state how it differs from other questions. – Kirie_Zolo Apr 20 '16 at 15:46
  • There is nothing much that could happen other than that you are trying to access the position in the array that is out of bound. Try printing the size of the array and your `position` before accessing it. – yogidilip Apr 20 '16 at 15:49
  • It's marked bro, no need to clarify now, You are searching for debugging help, Which will be better you debug your codes yourself with breakpoint, It's too lengthy and worst in reading. But I support your question writing pattern. – Shree Krishna Apr 20 '16 at 15:49
  • I've tried everything in my knowledge to fix my problem, that's why I turned to Stack Overflow. it's really strange because my indexing starts from zero, then yet again I get the exception. – Kirie_Zolo Apr 20 '16 at 15:52
  • @DimitrovV One suggestion I can give you is just check the length of array just before accessing it's element, where ever you accessed that. And don't let the accessing index be greater then the length of array. – Shree Krishna Apr 20 '16 at 15:54
  • My array list doesn't have a fixed length though. It increases as values are getting inserted from my db. What makes my brain hurt is why the other spinner is related to this issue. They are completely separate parts of code, apart from the fact that the selected options from both spinners, get stored in the DB with a `onClick` command – Kirie_Zolo Apr 20 '16 at 15:59

0 Answers0