1

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);
    } 
    %>
5
  • 2
    Probably req ends with a /, so that req.substring(req.lastIndexOf("/")+1)gives an error. Commented Sep 21, 2012 at 20:27
  • Could you please post a stacktrace? Commented Sep 21, 2012 at 20:30
  • Puzzling exceptions are often much clearer if you no what line throws them. So you should not just log he message but also at least the line of the last stack trace entry. Commented Sep 21, 2012 at 20:32
  • 1
    a) Move your code out of the jsp into a proper java class. b) The exception is not thrown in the code block you are showing us. Probably there is some jsp tag where you are trying to get al[4]. Commented Sep 21, 2012 at 20:53
  • you might be right, posted above the next jsp page taking this stuff Commented Sep 21, 2012 at 21:00

4 Answers 4

5

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);
} 
Sign up to request clarification or add additional context in comments.

9 Comments

but the same thing is working in the other servlet , both coming from the same jsp page through a javascript - function editRecord(product_id){ window.location.href="/Store/SelectProduct/"+product_id; } function deleteRecord(product_id){ window.location.href="/Store/DeleteProduct/"+product_id; }
can you post the stacktrace please
am doing it but getting another erorr, am going to post my whole servlet and view to be clearer and the new stacktrace, i will edit the question to a neater version after its resolved, thanks for the effort btw am so grateful to you and everyone
i guess the code is pretty much this, so makes no difference to post more, i will post the new trace
well, here's part of it An error occurred at line: 35 in the jsp file: /admin/editproduct.jsp Syntax error, insert "Finally" to complete TryStatement 32: product_description = productList.get(2).toString(); 33: product_price = (Double) productList.get(3); 34: } 35: } 36: %> 37: 38: <form name="productform" method="post" action="/EditProduct"> An error occurred at line: 138 in the generated java file Syntax error on token "catch", Identifier expected An error occurred at line: 140 in the generated java file out cannot be resolved to a variable
|
3
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);
        } 

2 Comments

thank you, exactly the same notice from chaitanya10, i earlier when tried using 0 it was telling me it starts from 1 so i thought it's always 1, i will need to read about this now and learn.. thanks
@TrackmeifYouCan Sure, np. Gl
0

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.

1 Comment

kind of strange because this servlet simply goes to the edit page,nothing else is active
-1
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

8 Comments

Why should that be an issue? That is totally unwarranted and only creates confusion.
tried to move the initialization here and there, still didn't help
this isn't the problem. y would this piece of code throw indexoutofbound.. check other answers
@DoubleMalt: That is why the word "Might" is used. If the OP is trying to put data into a single arraylist, then that is an error, in this case, because it makes lot of arraylists. If he/she need different arrayLists everytimes the loop runs, then no issue....However, I have clearly told the answer is given by others
@chaitanya10: it is allowed to give suggestions to users, to improve the code. Checking answers is up to the user, not up to us.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.