If this is a bad technique I would be more than happy to change it to a more standard technique. Any suggestions on how to improve my code are always welcome.
thanks for the suggestions thus far. Adam On 10/19/06, Chris Pratt <[EMAIL PROTECTED]> wrote:
I don't think I've ever seen that technique before, but you have to remember that size() is 1 based and index (or get(index) ) is 0 based. So when someone calls getNumProducts(5), your while loop fills in indexes 0, 1, 2, 3, and 4 (because then index and this.results.size() are both 5), then you proceed to ask for results.get(5), which doesn't exist. You should be able to change your while list to something like: while(index >= this.results.size() + 1) { this.results.add(new Product()); } (*Chris*) On 10/19/06, Adam K <[EMAIL PROTECTED]> wrote: > > Here are the 2 get methods that I have. > > thanks again for all the help. > > > public int getNumProducts() { > return numProducts; > } > > > public int getNumProducts(int index) > { > if(this.results== null) > { > this.results = new ArrayList(); > } > while(index >= this.results.size()) > { > this.results.add(new Product()); > } > > Product p = (Product) results.get(index); > return p.getNumProducts(); > } > > On 10/19/06, Puneet Lakhina < [EMAIL PROTECTED]> wrote: > > > > On 10/19/06, Adam K <[EMAIL PROTECTED]> wrote: > > > > > > Hi all I have been working on this probelm for the past couple of > days, > > > and > > > don't seem to be making any progress on it. I am fairly certain it is > a > > > problem with my understanding of struts, and as such it makes it quite > > > difficult for me to solve the problem myself. > > > I am trying to use a textfield and pull values from it. The following > > > explains the scenario. User clicks on a page, page loads with a form > > that > > > has a textfield. User enters search criteria and submits form. This > > > works > > > fine. Page returns with the search form and textfield, as well as a > > > second > > > form that displays the results of the search. This also works > > fine. The > > > user then goes through the results filling in numbers for each result > > > indicating how many of each they would like to order and then submit > the > > > form. This is where the problem results. I end up gettting an error > > from > > > the page anytime I submit the form where there is 1 or more results. > > > Submitting with no results works fine and the page loads correctly. > > > Submitting with 1 result errors out with > > > > > > java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 > > > > > > Submitting with 10 results errors out with: > > > java.lang.IndexOutOfBoundsException: Index: 2, Size: 0 > > > > > > Submitting with 100 results errors with : > > > java.lang.IndexOutOfBoundsException: Index: 6, Size: 0 > > > > > > Submitting with 200 results errors with : > > > java.lang.IndexOutOfBoundsException: Index: 155, Size: 0 > > > > > > The number seems to be random (I am guessing it is how far into the > > > form it gets before the error is encountered) > > > > > > Any help on this would be much appreciated. > > > I am including all the information that I believe to be important but > > > would have no problem including more. > > > > > > In the jsp form there is: > > > > > > <html:form action="/skuSearch" method="post" > > > > <center> > > > <table width="400" border="1" align="center" > > > cellpadding="0" cellspacing="0"> > > > <tr> > > > <td> > > > <table border="0" > > cellspacing="1" > > > cellpadding="1" width="100%" > > > > <tr align="center"> > > > <td><html:text > > > property="searchString" size="30" > > > maxlength="30"/></td> > > > > > > > > <td><html:submit>Search</html:submit></td> > > > </tr> > > > <tr><td > > colspan="2"></td></tr> > > > </table> > > > </td> > > > </tr> > > > </table> > > > </center> > > > </html:form> > > > > > > > > > <html:form action="/searchResults" method="post" > > > > <center><p><html:submit>Add To Order</html:submit></p></center> > > > <table align ="center" width="90%" border=1> > > > <tr> > > > <td> Product </td> > > > <td> Product Desc </td> > > > <td> Quantity </td> > > > </tr> > > > > > > <logic:notEmpty name="SkuSearchForm" property="results"> > > > <logic:iterate name="SkuSearchForm" property="results" > > > id="results"> > > > <tr> > > > <td> <bean:write name="results" property="product" > > > /> </td> > > > <td> <bean:write name="results" > > property="description" > > > /> </td> > > > <td> <html:text name="results" > property="numProducts" > > > indexed="true" /> > > > > > > post the getter method for the numProducts. that might be causing the > > problem. > > > > </td> > > > </tr> > > > </logic:iterate> > > > </logic:notEmpty> > > > > > > <logic:empty name="SkuSearchForm" property="results"> > > > <tr><td colspan="3" align="center">NO > RESULTS</td></tr> > > > </logic:empty> > > > </table> > > > </html:form> > > > > > > In the action (this action is only for the search results): > > > public ActionForward execute(ActionMapping mapping, ActionForm > > > form, > > > HttpServletRequest request, HttpServletResponse response) throws > > > Exception > > > { > > > SkuSearchForm skuform = (SkuSearchForm) form; > > > HttpSession session = request.getSession(); > > > User user = new User(); > > > Order order = new Order(); > > > ArrayList products = new ArrayList(); > > > > > > ArrayList results = new ArrayList(); > > > user = (User)session.getAttribute("User"); > > > order = user.getOrder(user.getCurrOrder()); > > > products = order.getProducts(); > > > int number = 0; > > > int count = 0; > > > > > > Iterator iter = results.iterator(); > > > //there is nothing happening in here as there is no results for some > > > reason. > > > while(iter.hasNext()) > > > { > > > Product p = (Product) iter.next(); > > > if(p.getNumProducts() != 0 ) > > > { > > > products.add(p); > > > } > > > > > > count++; > > > } > > > { > > > count = 1; > > > Product p = new Product("TestProd " + > > > count, "TestDesc"+count + " > > > " +count, 10, new BigDecimal("101.0")); > > > products.add(p); > > > results.add(p); > > > count++; > > > } > > > > > > order.setProducts(products); > > > order.setOrderId("ID12"); > > > user.changeOrder(order, "ID12"); > > > skuform.setResults(results); > > > > > > return mapping.findForward("success"); > > > } > > > > > > In the form (The methods I thought were appropriate): > > > > > > public void setResults(ArrayList results) > > > { > > > this.results=results; > > > } > > > > > > public ArrayList getResults() > > > { > > > return this.results; > > > } > > > > > > public Product getResult(int index) > > > { > > > if(this.results == null) > > > { > > > this.results = new ArrayList(); > > > } > > > while(index>= this.results.size()) > > > { > > > this.results.add(new Product()); > > > } > > > > > > return (Product)results.get(index); > > > } > > > > > > > > > > > > Thanks so much in advance for your time on this matter. > > > > > > Adam > > > > > > > > > > > > -- > > Puneet > > > > > >