Problem with listing an ArrayList in JAVA JSP

0

I have the following problem:

I'm doing a shopping cart, a task, I'm a novice in this Java Server Pages, and I have a Product class where I have a method that returns a List and invoke that method from a JSP called a catalog, see here:

//Metodo
public List<Object> ver() {
    List<Object> p = new ArrayList<>();
    Connection con;
    Statement pred;
    ResultSet rst;
    String qry="SELECT * FROM productos";
    try {
        Class.forName(bd.getDriver());
        con = DriverManager.getConnection(bd.getUrl(), bd.getU(), bd.getP());
        pred = con.createStatement();
        rst = pred.executeQuery(qry);
//Contador para el indexado del array por cada objeto
        int cont = 0;
        while (rst.next()){
            p.add(cont,new producto( rst.getInt("ID_PRODUCTO"),
                                rst.getString("COD_COLOR"),
                                rst.getString("COD_SIZE"),
                                rst.getString("NOMBRE"),
                                rst.getDouble("PRECIO"),
                                rst.getString("IMAGEN")));
            cont++;
        }
        con.close();
    }catch (Exception e){
                e.printStackTrace();
    }
    return p;
}

Then I have a JSP called a catalog as I mentioned earlier that it has the following code:

producto prod = new producto();
List<Object> arrayprod = prod.ver();
int valor = arrayprod.size();
int id = (int) arrayprod.get(0);
String color = (String) arrayprod.get(1);
String size = (String) arrayprod.get(2);
String nombre = (String) arrayprod.get(3);
double precio = (double) arrayprod.get(4);
String imagen = (String) arrayprod.get(5);
int cont = 1;
%>
<section>
<%

    for (int i = 0;i<=valor;i++){
        while (i==6){
%>
        <div id="prod">
        <img src="<%=imagen%>">
        Nombre:<p><%=nombre%></p>
        Precio:<p><%=precio%></p>
        Color:<p><%=color%></p>
        Tamaño:<p><%=size%></p>
        <input type="button" name="send" value="<%=id%>" onclick="agregar()">
        </div>
<%
            i=0;
            cont++;
        }
        if (cont==3){
%>
            <br>
<%
            cont=1;
        }
    }
%>
</section>

The problem is that when the JSP is executed it sends me the following error:

HTTP Status 500 - Internal Server Error

Type Exception Report

Message An exception occurred processing JSP page [/catalogo.jsp] at line [16]

Description The server encountered an unexpected condition that prevents it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing JSP page [/catalogo.jsp] at line [16]

//Esto no es parte del error
13:     producto prod = new producto();
14:     List<Object> arrayprod = prod.ver();
15:     int valor = arrayprod.size();
16:     int id = (int) arrayprod.get(0);
17:     String color = (String) arrayprod.get(1);
18:     String size = (String) arrayprod.get(2);
19:     String nombre = (String) arrayprod.get(3);

It says that on line 16 of my code there is something wrong, and I notice the Root Case that throws the error is the following line:

java.lang.IndexOutOfBoundsException: Index 0 out-of-bounds for length 0

It says that the index is outside the limit of length 0 (I can not interpret it well).

Note: The database is connected to the database and contains data.

So here I do not know what would be working or not, if it is my logic that is wrong or that, if someone can help me, I would greatly appreciate it.

    
asked by Francisco Castle 05.04.2018 в 00:28
source

1 answer

0

The error occurred when you inserted into the array

     p.add(cont,new producto( rst.getInt("ID_PRODUCTO")

When you insert and pass the index, you have to make sure that the index is less than the size of the Arraylist, since you have not inserted anything the size is zero. To simply add elements to the end of the array simply call add without the index

     p.add(new producto( rst.getInt("ID_PRODUCTO")

On the other hand I recommend that you change the way you are mapping the information, instead of making a list create an object with the properties you need. Example:

class Producto{
      private int id;
      private String color;
      ...
      //aqui colocas las demas propiedades y su gettter and setter
}

and call the setters to fill in the data and getter to read them

   Producto p = new Producto();
   while (rst.next()){
        p.setId( rst.getInt("ID_PRODUCTO"));
        p.setColor( rst.getString("COD_COLOR"));
    }
    
answered by 05.04.2018 в 22:57