环境
Eclipse Indigo + Hibernate3.2 + Oracle 11g R2 + SQLPlus
问题
运行Hibernate程序,报如下异常
java.lang.IndexOutOfBoundsException: Index: 5, Size: 5 at java.util.ArrayList.RangeCheck(ArrayList.java:547) at java.util.ArrayList.get(ArrayList.java:322) at com.addbook.dao.impl.ContactorDaoImpl.getAllContactorId(ContactorDaoImpl.java:123) at com.addbook.service.impl.ContactorServiceImpl.getAllContactorId(ContactorServiceImpl.java:81) at com.addbook.dao.impl.TestContactor.testGetAllContactorId(TestContactor.java:84) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
解决
程序逻辑出错。
以前的程序:
@SuppressWarnings("unchecked") public List<Integer> getAllContactorId(){ List<Integer> list = new ArrayList<Integer>(); session = ConnUtil.getSession(); sql = "select id from contactor"; query = session.createSQLQuery(sql); list = query.list(); for (int i = 0; i < list.size(); i++) { int temp = ((BigDecimal)query.list().get(i)).intValue(); list.add(temp); } return list; }
修改后的程序:
@SuppressWarnings("unchecked") public List<Integer> getAllContactorId(){ List<Integer> list = new ArrayList<Integer>(); session = ConnUtil.getSession(); sql = "select id from contactor"; query = session.createSQLQuery(sql); list = query.list(); System.out.println(list.size()); int count = list.size(); int num[] = new int[count]; for (int i = 0; i < count; i++) { num[i] = ((BigDecimal)query.list().get(i)).intValue(); } list = new ArrayList<Integer>(); for (int i = 0; i < num.length; i++) { list.add(num[i]); } return list; }
问题分析
出现错误的原因:
1. list.size()在不断的改变;
2. list再次add会叠加以前的数据,所以需要重新创建对象。