2
我試圖實例化一個Java bean,但是它拋出異常:java.lang.IndexOutOfBoundsException:指數:0,大小:0
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at com.application.jsf.BooksTable.getLastId(BooksTable.java:54)
at com.application.jsf.BooksManager.<init>(BooksManager.java:24)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:374)
at java.lang.Class.newInstance(Class.java:327)
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)
... 55 more
這裏的BooksManager
類的相關代碼:
private BooksTable booksTable;
private BookGenerator bookGenerator;
public BooksManager() {
booksTable = new BooksTable();
bookGenerator = new BookGenerator(booksTable.getLastId());
}
而且BooksTable
類:
private ArrayList<BookBean> booksList;
private int BOOKS_ON_PAGE = 10;
BooksTable(){
this.booksList = new ArrayList<BookBean>();
}
public int getLastId(){
if(this.booksList == null){
return -1;
}
return this.booksList.get(this.booksList.size()).getBookId();
}
這是怎麼造成的,我該如何解決呢?
你讓'所致指數:0 ,大小:0'。這與JSF完全沒有關係。這個例外是來自'java.lang'包,它指出了一個基本的Java SE問題。今後請更加註意。用'main()'方法在純Java應用程序中隔離問題,而不用所有JSF代碼噪聲。我至少從問題中刪除了不相關的JSF噪音,以便更容易回收。 – BalusC