Reputation: 108
Why am I getting this error:
java.lang.IndexOutOfBoundsException: Index: 4 , Size: 4
I am not defining anything and have used the same logic on another servlet that is working fine. In my other servlet I am selecting all products, so I used two arraylists: one inside the other. I tried that here but still have the same error. I clearly understand the error but I have no idea how to resolve it in this syntax.
Thank you.
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
int product_id =Integer.parseInt(req.substring(req.lastIndexOf("/")+1));
ArrayList al = null;
String query = "select * from Product where product_id="+product_id;
try {
ps = connection.prepareStatement(query);
rs = ps.executeQuery(query);
while (rs.next()) {
al = new ArrayList();
al.add(rs.getString("product_id"));
al.add(rs.getString("product_name"));
al.add(rs.getString("product_description"));
al.add(rs.getDouble("product_price"));
}
The next JSP page from this servlet is:
<%!
String product_id="";
String product_name="";
String product_description="";
double product_price = 0;
ArrayList productList=null;
%>
<%
if(request.getAttribute("productList")!=null && request.getAttribute("productList")!="") {
productList = (ArrayList)request.getAttribute("productList");
product_id = productList.get(1).toString();
product_name = productList.get(2).toString();
product_description = productList.get(3).toString();
product_price = (Double) productList.get(4);
}
%>
Upvotes: 1
Views: 2639
Reputation: 112
The error is giving is this:
**java.lang.IndexOutOfBoundsException: Index: 4, Size: 4**
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at org.apache.jsp.admin.editproduct_jsp._jspService(editproduct_jsp.java:82)
And is saying that it tried to get element number 5 (index=4) when the arraylist only had 4 elements. It seems that this stack trace is not an error of that code you are showing on your post. My guess is that error comes later when looping through the "a1" ArrayList which only has 4 elements and tried to get the fifth one.
Upvotes: 0
Reputation: 46428
you possibly are getting that exception on this line
int product_id =Integer.parseInt(req.substring(req.lastIndexOf("/")+1));
from your code, this could be the only case. when you call subString() on req index is going out of bound. the best test would be to put a sysout the length of req
EDIT AFTER POSTING THE STACKTRACE
change your java code to this
al = new ArrayList();
while (rs.next()) {
al.add(rs.getString("product_id")); // stored in the al at index 0
al.add(rs.getString("product_name"));// stored in the al at index 1
al.add(rs.getString("product_description"));// stored in the al at index 2
al.add(rs.getDouble("product_price"));// stored in the al at index 3
}
heres where its throwing the exception
line1: product_id = productList.get(1).toString();
line2: product_name = productList.get(2).toString();
line3: product_description = productList.get(3).toString();
line4 product_price = (Double) productList.get(4); /// its going indexoutfbound here
your list has only 4 elements and you are trying to get the 5th element with above code. }
change them to
product_id = productList.get(0).toString();
product_name = productList.get(1).toString();
product_description = productList.get(2).toString();
product_price = (Double) productList.get(3);
}
Upvotes: 5
Reputation: 10995
int product_id =Integer.parseInt(req.substring(req.lastIndexOf("/")+1));
If you had an url like so: http://www.yahoo.com/
lastIndexOf will give you 21 and +1 gives 22 which is out of bounds.
EDIT: After you pasted the servlet page I noticed that you seem to be confused on the index to access the arrayList. You start from 0 instead of 1 IIRC. So:
if(request.getAttribute("productList")!=null && request.getAttribute("productList")!="")
{
productList = (ArrayList)request.getAttribute("productList");
product_id = productList.get(1).toString();
product_name = productList.get(2).toString();
product_description = productList.get(3).toString();
product_price = (Double) productList.get(4);
}
is
if(request.getAttribute("productList")!=null && request.getAttribute("productList")!="")
{
productList = (ArrayList)request.getAttribute("productList");
product_id = productList.get(0).toString();
product_name = productList.get(1).toString();
product_description = productList.get(2).toString();
product_price = (Double) productList.get(3);
}
Upvotes: 3
Reputation: 23035
while (rs.next()) {
al = new ArrayList();
al.add(rs.getString("product_id"));
al.add(rs.getString("product_name"));
al.add(rs.getString("product_description"));
al.add(rs.getDouble("product_price"));
}
you are initializing the arraylist inside the loop. That can be an issue too, if not, might make issues if that is not what you are expecting. The answer is given by others I guess
Upvotes: -1