Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:37:47+00:00 2026-06-12T08:37:47+00:00

Why am I getting this error: java.lang.IndexOutOfBoundsException: Index: 4 , Size: 4 I am

  • 0

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);
    } 
    %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:37:48+00:00Added an answer on June 12, 2026 at 8:37 am

    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);
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the below code I'm getting the following error : java.lang.IndexOutOfBoundsException: Index: 1, Size:
I am getting this error: java.net.MalformedURLException: Protocol not found[java.lang.StringBuilder] When the following line is
I keep getting this error: java.lang.NoClassDefFoundError: org/apache/lucene/index/memory/MemoryIndex Is there any way i can go
I am getting this error: Caused by: java.lang.RuntimeException: Your content must have a ListView
I am getting this error when ever deploying my jsf application java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config at
I am getting this error: java.lang.ClassCastException: $Proxy0 cannot be cast to rmi.engine.Call at Main.main(Main.java:39)
Iam getting this error on my log 07-06 06:22:07.419: ERROR/AndroidRuntime(2618): java.lang.SecurityException: SECURE PERMISSION: Neither
I'm simulating a overload of a server and I'm getting this error: java.lang.OutOfMemoryError: unable
I keep getting this error 07-14 23:53:03.653: ERROR/AndroidRuntime(14995): java.lang.NullPointerException 07-14 23:53:03.653: ERROR/AndroidRuntime(14995): at com.fttech.organizeit.meeting_list$meetingHolder.populateFrom(meeting_list.java:110)
Hello all I'm getting this very strange error: java.lang.NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lor g/hibernate/classic/Session; at org.cometd.hibernate.util.HibernateUtil.getSessionFactory(HibernateUt il.java:29)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.