I have a form in which there is a text field. The user inputs the email address in the text field, and when the user submits the form, the Action class finds the uid of the user from the Users table and displays the uid on the JSP page as a message with <s:actionmessage/>. I am getting an IndexOutOfBoundsException.
My JSP form is:
<s:form action="okadddqs" method="post" cssClass="text">
<s:textfield label="Your Email address " name="email"/>
<s:submit value="Find Uid"/>
</s:form>
The code for the Action class is:
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());
}
}
}
when i remove all the try catch blocks i get the exception as :
java.lang.ClassCastException: org.hibernate.impl.QueryImpl cannot be cast to java.util.List
I have done the beans and mapping of tables properly with POJO. Can someone point out the error? I am using Hibernate with Struts.
The following line in your code is incorrect:
createQuery returns back a query object. If you want the results of that query, use Query.list().