Home>

I want to know if the error of Index: 15, Size: 15 type is displayed.
I want to know how to get the information contained in List from Listtype

List<Report>contains a reports table, but I want to get the employee_id of this
+ ------------------- + -------------- + ------ + ----- +- -------- + ----------------- +
Field | Type | Null | Key | Default | Extra |
+ ------------------- + -------------- + ------ + ----- +- -------- + ----------------- +
id | int (11) | NO | PRI | NULL | auto_increment |
| content | longtext | NO | | NULL | |
| created_at | datetime | NO | | NULL | |
| reaction_nice_cnt | int (11) | NO | | NULL | |
| report_date | date | NO | | NULL | |
| title | varchar (255) | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| employee_id | int (11) | NO | MUL | NULL | |
+ ------------------- + -------------- + ------ + ----- +- -------- + ----------------- +


Report.java

package models;
import java.sql.Date;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@Table (name = "reports")
@NamedQueries ({
    @NamedQuery (
            name = "getAllReports",
            // Get all daily report information
            query = "SELECT r FROM Report AS r ORDER BY r.id DESC"
            ),
    @NamedQuery (
            name = "getReportsCount",
            // Get the previous number of daily reports
            query = "SELECT COUNT (r) FROM Report AS r"
            ),
    @NamedQuery (
            name = "getMyAllReports",
            // Sort your daily report in the order you registered earlier
            query = "SELECT r FROM Report AS r WHERE r.employee =: employee ORDER BY r.id DESC"
            ),
    @NamedQuery (
            name = "getMyReportsCount",
            // Check if the specified daily report already exists in the database
            query = "SELECT COUNT (r) FROM Report AS r WHERE r.employee =: employee"
            ),
})
@Entity
public class Report {
    @Id
    @Column (name = "id")
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    private Integer id;// Define many-to-one associations with @ManyToOne
    @ManyToOne
    // Specify the column to be used for integration with @JoinColumn
    @JoinColumn (name = "employee_id", nullable = false)
    private Employee employee;
    // date type is date only
    @Column (name = "report_date", nullable = false)
    private Date report_date;
    @Column (name = "title", length = 255, nullable = false)
    private String title;
    // @Lob also saves line breaks in the database
    @Lob
    @Column (name = "content", nullable = false)
    private String content;
    @Column (name = "created_at", nullable = false)
    private Timestamp created_at;
    @Column (name = "updated_at", nullable = false)
    private Timestamp updated_at;
    @Column (name = "reaction_nice_cnt", nullable = false)
    private Integer reaction_nice_cnt;
    public Integer getId () {
        return id;
    }
    public void setId (Integer id) {
        this.id = id;
    }
    public Employee getEmployee () {
        return employee;
    }
    public void setEmployee (Employee employee) {
        this.employee = employee;
    }
    public Date getReport_date () {
        return report_date;
    }
    public void setReport_date (Date report_date) {
        this.report_date = report_date;
    }
    public String getTitle () {
        return title;
    }
    public void setTitle (String title) {
        this.title = title;
    }
    public String getContent () {
        return content;
    }
    public void setContent (String content) {
        this.content = content;
    }
    public Timestamp getCreated_at () {
        return created_at;
    }
    public void setCreated_at (Timestamp created_at) {
        this.created_at = created_at;
    }
    public Timestamp getUpdated_at () {
        return updated_at;
    }
    public void setUpdated_at (Timestamp updated_at) {
        this.updated_at = updated_at;}
    public Integer getReaction_nice_cnt () {
        return reaction_nice_cnt;
    }
    public void setReaction_nice_cnt (Integer reactio_nice_cnt) {
        this.reaction_nice_cnt = reactio_nice_cnt;
    }
}
HTTP Status 500 --Index: 15, Size: 15
type exception report
Message Index: 15, Size: 15
Description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.IndexOutOfBoundsException: Index: 15, Size: 15
    java.util.ArrayList.rangeCheck (ArrayList.java:653)
    java.util.ArrayList.get (ArrayList.java:429)
    controllers.reports.ReportsIndexServlet.doGet (ReportsIndexServlet.java:79)
    javax.servlet.http.HttpServlet.service (HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service (HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:52)
    filters.LoginFilter.doFilter (LoginFilter.java:74)
    filters.EncodingFilter.doFilter (EncodingFilter.java:55)
Note All stack traces of the cause are logged in Apache Tomcat/8.0.43 `` ``
Error message

ReportIndexServlet

package controllers.reports;
import java.io.IOException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Employee;
import models.Report;
import utils.DBUtil;
/ **
* Servlet implementation class ReportsIndexServlet
* /
@WebServlet ("/ reports/index")
public class ReportsIndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/ **
* @see HttpServlet # HttpServlet ()
* /
public ReportsIndexServlet () {
super ();
// TODO Auto-generated constructor stub
}
/ **
* @see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse response)
* /
protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/ *
Execute createEntityManager () of DBUtil to create EntityManager
Create EntityManager type variable em * /
EntityManager em = DBUtil.createEntityManager ();
// Define int type variable page
int page;try {
/ *
Get page from form with request.getParameter and cast it to int type
Put it in the variable page * /
page = Integer.parseInt (request.getParameter ("page"));
// Exception handling (exceptions other than Error and RuntimeException, also known as checked exceptions)
} catch (Exception e) {
// Put 1 in the variable page
page = 1;
}
// Get all daily report data
List<Report>reports = em.createNamedQuery ("getAllReports", Report.class)
// Specify the first position to learn the search results
.setFirstResult (15 * (page -1))
// Set the maximum number of results to learn
.setMaxResults (15)
// Execute a SELECT query and return the query result as an untyped list
// use getResultList () method to get more than one
.getResultList ();
// Count the registered daily reports
long reports_count = (long) em.createNamedQuery ("getReportsCount", Long.class)
// Execute a SELECT query and return the query result as an untyped list
.getSingleResult ();
// Get the logged-in id from session scope
Employee login_employee = (Employee) request.getSession (). getAttribute ("login_employee");
// I want to get the data of List type reports one by one
for (int i = 0;i<= reports_count --1;i ++) {
// I want to get one daily report from reports and get the id of the registrant of that daily report
Employee r_id = reports.get (i) .getEmployee ();
// Check if i am following the acquired daily report
long followCheck = em.createNamedQuery ("FollowedCheck", Long.class)
.setParameter ("my_id", login_employee)
// Daily report creator id
.setParameter ("employee_id", r_id)
.getSingleResult ();
request.setAttribute ("followChecks", followCheck);
}
// Close em (EntityManager)
em.close ();

// Make the variable reports available as reports in jsp
request.setAttribute ("reports", reports);
request.setAttribute ("reports_count", reports_count);
request.setAttribute ("page", page);
// To prevent the updated flush etc. from being displayed even though I just returned
// Execute when flush is not null
if (request.getSession (). getAttribute ("flush")! = null) {
// Make flush available in flush with jsp
request.setAttribute ("flush", request.getSession (). getAttribute ("flush"));
// Remove flushe from session scope
request.getSession (). removeAttribute ("flush");
}
// Specify /reports/index.jsp for the view
RequestDispatcher rd = request.getRequestDispatcher ("/WEB-INF/views/reports/index.jsp");
// Call the jsp file as the response screen
rd.forward (request, response);
}
}
  • Answer # 1

    "Reports" is a maximum of 15 because 15 is specified in setMaxResults (15),
    Since "reports_count" does not limit the number of acquisitions, it is estimated that 15 or more are obtained.
    When the for statement goes around 15 times or more and comes to the 16th time, I think that there is no 16th case in reports and it is an error.

    I think that you do not have to worry about the index if you process all reports with the extended for statement.

    for (Report report: reports) {
        // I want to get one daily report from reports and get the id of the registrant of that daily report
        Employee r_id = report.getEmployee ();
    --Omitted--
    }

  • Answer # 2

    In order to get employee_id, if you create the following field definition and getter/setter in class Report, it will be stored automatically.

      @Column (name = "employee_id", nullable = false)
        private Integer employeeId;