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

时间:2012-07-14 04:38:01

标签: hibernate struts2 indexoutofboundsexception

我有一个表单,其中有一个文本字段。用户在文本字段中输入电子邮件地址,当用户提交表单时,Action类从uid表中找到用户的Users并显示{{1}在JSP页面上作为带有uid的消息。我收到<s:actionmessage/>

我的JSP表单是:

IndexOutOfBoundsException

<s:form action="okadddqs" method="post" cssClass="text"> <s:textfield label="Your Email address " name="email"/> <s:submit value="Find Uid"/> </s:form> 类的代码是:

Action

当我删除所有try catch块时,我得到例外:

package com.rambo.action;

import beans.Users;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.hibernate.Session;

/**
 *
 * @author ROMO
 */
@ManagedBean
@SessionScoped
public class NewQuestion extends ActionSupport {

    private String email;

    private List<Users> ul = new ArrayList<Users>();

    public List<Users> getUl() {
        return ul;
    }

    public void setUl(List<Users> ul) {
        this.ul = ul;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
/* i have used many where the getUid() and getEmail() even in the error and messages to find what the value it is getting on error.  */
    @Override
    public String execute() throws Exception {
        Session session = null;
        int d;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            try {
                ul = (List<Users>) session.createQuery("from Users c where c.email = '" + getEmail() + "'");
                if (!ul.isEmpty()) {
                    d = ul.get(0).getUid();
                } else {
                    this.addActionError("Oops. Sorry. You are Not Allowed now. Please Try Again Later." + getEmail() + ","+ ul.get(0).getUid());
                    session.close();
                    return ERROR;
                }
            } catch (Exception e) {
                this.addActionError("Oops. There was an error." + getEmail() + ","+ ul.get(0).getUid());
                session.close();
                return ERROR;
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            this.addActionError("Oops. An Error Encountered...! Email address " + getEmail() + ","+ ul.get(0).getUid()+" not registered. Try with your new email address.");
            session.close();
            return ERROR;
        }
        this.addActionMessage("Your Uid is : " + ul.get(0).getUid());
        return SUCCESS;
    }

   @Override
    public void validate() {
       if ( "".equals(getEmail()) || getEmail() == null ) {
            this.addActionError("Email is Compulsory to input..!" + getCname());
        } else if (getEmail().indexOf("@") < 0 || getEmail().indexOf(",") > 0 || getEmail().indexOf(".") < 0 || lastEmailFieldTwoCharsOrMore(getEmail()) == false) {
            this.addActionError("Please Input a valid email address." + getCname());
        }
    }
}

我已经使用POJO正确完成了bean的表和映射。有人可以指出错误吗?我正在使用Hutsnate和Struts。

1 个答案:

答案 0 :(得分:2)

代码中的以下行不正确:

ul = (List<Users>) session.createQuery(
    "from Users c where c.email = '" + getEmail() + "'"
);

createQuery返回一个查询对象。如果您需要该查询的结果,请使用Query.list()

ul = (List<Users>) session.createQuery(
    "from Users c where c.email = '" + getEmail() + "'"
).list();