userZZZ userZZZ - 1 year ago 86
Java Question

Loop throws exception before adding records

I try to extract the

for a specific recruiter with "username". So everything executes good and I can print the recruiter username for the jobs I get from looping through the list. but the problem is that:
is not executed and then it directly throws the hibernate exception. Does anyone know what the problem is?

I know I should be using appropriate queries but I get exceptions all the time, my database is very small (school project), so it doesn't matter now.

public List<Job> list(String userName) throws AdException {

try {
Query q = getSession().createQuery("FROM Job");
ArrayList<Job> list = (ArrayList<Job>) q.list();
System.out.println("All Jobs: "+ list);

ArrayList<Job> jobList = new ArrayList<Job>();

for (Job j : list) {
System.out.println("job userName is: "+ j.getRecruiter().getUserName());
if (j.getRecruiter().getUserName().equalsIgnoreCase("userName")){

System.out.println("jobList is: "+ jobList);
return jobList;

} else {


} catch (HibernateException e) {
throw new AdException("Could not get jobList for company: " + userName, e);

return null;

Answer Source

I don't think you want userName to be quoted as a String Literal

if (j.getRecruiter().getUserName().equalsIgnoreCase("userName")){

but rather

if (j.getRecruiter().getUserName().equalsIgnoreCase(userName)){


As John so kindly mentions, your calling methos should also check for null being returned.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download