0

When I set The data it says

String enrloment = studentData.get(position).getEnrollments().get(position).getCourse().getTitle();

this line

java.lang.IndexOutOfBoundsException: Index: 2, Size: 1 at java.util.ArrayList.get(ArrayList.java:437)

my app crashed i am using retrofit library for get data

This My Adapter Class

Blockquote

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.StudentViewholde> {

private Context context;
private List<StudentInformation>studentData;







public StudentAdapter(Context context, List<StudentInformation> studentData) {
    this.context = context;
    this.studentData = studentData;

}


public void setStudentData(List<StudentInformation> studentData) {
    this.studentData = studentData;
    notifyDataSetChanged();
}



@NonNull
@Override
public StudentViewholde onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.single_raw,parent,false);


    return new StudentViewholde(view);
}


@Override
public void onBindViewHolder(@NonNull StudentViewholde holder, int position) {
    
    StudentInformation studentInformation = studentData.get(position);

    holder.idTv.setText(studentInformation.getID().toString());
    holder.nameTv.setText(studentInformation.getFirstMidName());
    holder.enrolmentId.setText(studentInformation.getEnrollments().get(position).getStudentID().toString());


}

@Override
public int getItemCount() {
    return studentData.size();
}

class StudentViewholde extends RecyclerView.ViewHolder{
    TextView idTv,nameTv,enrolmentId;
    public StudentViewholde(@NonNull View itemView) {
        super(itemView);
        idTv = itemView.findViewById(R.id.idTv);
        nameTv = itemView.findViewById(R.id.name);
        enrolmentId = itemView.findViewById(R.id.enrollmentDate);
    }
}

}

it is my Student Information Pojo class

public class StudentInformation {

@SerializedName("Enrollments")
@Expose
private List<Enrollment> enrollments ;
@SerializedName("ID")
@Expose
private Integer iD;
@SerializedName("LastName")
@Expose
private String lastName;
@SerializedName("FirstMidName")
@Expose
private String firstMidName;
@SerializedName("EnrollmentDate")
@Expose
private String enrollmentDate;

public List<Enrollment> getEnrollments() {
    return enrollments;
}

public void setEnrollments(List<Enrollment> enrollments) {
    this.enrollments = enrollments;
}

public Integer getID() {
    return iD;
}

public void setID(Integer iD) {
    this.iD = iD;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getFirstMidName() {
    return firstMidName;
}

public void setFirstMidName(String firstMidName) {
    this.firstMidName = firstMidName;
}

public String getEnrollmentDate() {
    return enrollmentDate;
}

public void setEnrollmentDate(String enrollmentDate) {
    this.enrollmentDate = enrollmentDate;
}

}

it is my enrollment pojo class

public class Enrollment {

@SerializedName("Course")
@Expose
private Course course;
@SerializedName("EnrollmentID")
@Expose
private Integer enrollmentID;
@SerializedName("CourseID")
@Expose
private Integer courseID;
@SerializedName("StudentID")
@Expose
private Integer studentID;
@SerializedName("Grade")
@Expose
private Integer grade;

public Course getCourse() {
    return course;
}

public void setCourse(Course course) {
    this.course = course;
}

public Integer getEnrollmentID() {
    return enrollmentID;
}

public void setEnrollmentID(Integer enrollmentID) {
    this.enrollmentID = enrollmentID;
}

public Integer getCourseID() {
    return courseID;
}

public void setCourseID(Integer courseID) {
    this.courseID = courseID;
}

public Integer getStudentID() {
    return studentID;
}

public void setStudentID(Integer studentID) {
    this.studentID = studentID;
}

public Integer getGrade() {
    return grade;
}

public void setGrade(Integer grade) {
    this.grade = grade;
}

}

3 Answers3

1

The exception means that you are trying to access the third element (Index: 2) of a list of size 1 (Size: 1). The line of code where you get the error is:

String enrloment = studentData.get(position).getEnrollments().get(position).getCourse().getTitle();

In this line you try to access studentData and enrollments with the method .get(position). The index you use is position which I assume is equal to 2. So the issue is that studentData or enrollments have less than 3 elements inside.

Ryan1729
  • 940
  • 7
  • 25
Marc
  • 2,738
  • 1
  • 17
  • 21
0

Change this

String enrloment = studentData.get(position).getEnrollments().get(position).getCourse().getTitle();

to

ArrayList<Enrollment> enrloment = studentData.get(position).getEnrollments()
ArrayList<String> enrolmentData = new ArrayList();
for(int i =0;i<enrloment.size();i++){
      enrolmentData.add(enrloment.get(i).getCourse().getTitle());
}

Hope it helps

0

let's say you have 5 students in studentInfoList. and the 3rd student has only 2 enrollments.

if you try String enrloment = studentData.get(position).getEnrollments().get(position).getCourse().getTitle();

i.e, String enrloment = studentData.get(2).getEnrollments().get(2).getCourse().getTitle();

here is the problem. you got only 2 enrollments and calling for the 3rd enrollment which is not there.. hope you got it.

Daya A L
  • 88
  • 6