userZZZ userZZZ - 7 months ago 9
Java Question

Loop throws exception before adding records

I try to extract the

jobList
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:
jobList.add(j)
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 {
begin();
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")){

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

} else {
continue;
}

}

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

return null;
}

Answer

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)){

Edit

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