天天看点

List下标越界

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

 at java.util.ArrayList.RangeCheck(Unknown Source)

 at java.util.ArrayList.get(Unknown Source)

 at com.szallway.phr2.portal.daoImpl.AccountDaoImpl.findbyEmail(AccountDaoImpl.java:324)

 at com.szallway.phr2.portal.action.AccountAction.registerAccount(AccountAction.java:274)

 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

 at java.lang.reflect.Method.invoke(Unknown Source)

 ......

出现这种情况一般就是你的List 是一个没有值的对象(不是null),也就是里面什么对象也没有存(即:List.size()==0)。

但是,你有取它下标为0值的操作。所以,数组越界了!!比如List.get(0);

 //根据邮箱查看账号信息

 @Override

 public Account findbyEmail(String email) throws Exception {

  // TODO Auto-generated method stub

  Account acc=new Account();

  Session session=HibernateUtil.getSession();

  String hql="from Account as acc where acc.email='"+email+"'";

  Query q=session.createQuery(hql);

  List list=q.list();

  if(list!=null)

  {

   acc =(Account)list.get(0);    //这里出错了

  }

  else

  {

   acc=null;

  }

  session.close();

  return acc;

 }

2011-02-21 10:35:20