java - IndexOutOfBoundException on method called in a JSP Page -
i want retreive number of istances of class flat file flat.ser , aim wrote method here reported:
flat.java
public static arraylist<flat> retreiveflat() throws classnotfoundexception, ioexception { fileinputstream fis = new fileinputstream("path/flat.ser"); objectinputstream ois = new objectinputstream(fis); flat f; arraylist<flat> al = new arraylist<flat>(); while (fis.available() > 0) { object retrieved = ois.readobject(); if(retrieved instanceof flat) { f = (flat) retrieved; if(f.gettypeofflat().equals("double")) { al.add(f); } } } ois.close(); return al; } the method used in jsp page:
page.jsp
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ page import="entity.flat" %> <%@ page import="java.util.*" %> <% arraylist<flat> al = new arraylist<flat>(); al = flat.retreiveflat(); int = 0; while(al.get(i) != null){ //do i++;}%> the problem istance of al empty in jsp page though have assigned return of method retreiveflat() , though in debug mode see inside body of retreiveflat() method list not empty. exception of type indexoutofboundsexception when call get() method on al. tell me make mistake?
i don't know why list empty, attempt iterate on list indeed throw indexoutofboundsexception. should use for-each loop (also called enhanced for).
replace
while(al.get(i) != null){ // al.get(i) i++;} with this:
for(flat flat : al) { // flat }
Comments
Post a Comment