jsp - Why is my array list size zero when i initialised it to 30? -
why array list size 0 when initialised 30?
i java.lang.indexoutofboundsexception: index: 1, size: 0
when addrecord() called
(note: calling setinitialvalues jsp doesnt help.)
(note: shootrecord implements serializable)
servlet procesrequest method
protected void processrequest(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { printwriter pw = resp.getwriter(); string address = null; httpsession session = req.getsession(); shootmeet meetbean = (shootmeet)session.getattribute("shootmeetbean"); if(meetbean == null){ pw.print("initialising meet \n"); meetbean = new shootmeet(); meetbean.setinitialvalues(30); } shootrecord recordsent = (shootrecord)session.getattribute("shootrecordbean"); if(recordsent == null){ recordsent = new shootrecord(); } // **record sent created here** try{ meetbean.addrecord(recordsent); } ... } // doget , dopost call processrequest
shootmeet
public class shootmeet implements serializable{ private arraylist<shootrecord> listofrecords; private string date; private int numtargets; public shootmeet(){} public void setinitialvalues(int numtarg){ numtargets = numtarg; listofrecords = new arraylist<shootrecord>(numtargets); } public void addrecord(shootrecord s){ if(listofrecords.size() == 0){ system.out.println("size of list of records 0 before adding record"); } listofrecords.add(s.targetnumber, s); } //...other setters/getters }
index.jsp
<%@page import="servlet.shootrecord"%> <%@page import="servlet.shootrecordmap"%> <%@ page contenttype="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@page contenttype="text/html" pageencoding="utf-8"%> <jsp:usebean id="shootmeetbean" class="servlet.shootmeet" scope = "session"/> <%--<jsp:setproperty name = "shootmeetbean" property = "initialvalues" value = "1"/> </jsp:usebean>--%> <jsp:usebean id="shootrecordbean" class="servlet.shootrecord" scope = "session"/> <html> // ... jstl tags </html>
i confusing list capacity list size, , functionality of arrays of arraylists.
size number of elements in list; capacity how many elements list can potentially accommodate without reallocating internal structures. when call new arraylist(10), setting list's initial capacity, not size. in other words, when constructed in manner, array list starts life empty.
Comments
Post a Comment