I am aware this code is rather messy. Copying an extraction of one list to another I’m pretty sure is not the most elegant of solutions. However I just want to work out why this does not work.
This is a hibernate method to query a db a return the enteries in a list. i.e the fix list. From this I only want the FixString which I wish to use to populate the fixString list.
However for reason unknown to me. whenever I call
try {
//Get all the Fix Strings stored in db
fixStrings = HibFunction.listFix();
} catch (Exception e) {
System.out.printf("Cannot get Fix strings",e);
}
My junit fails giving me a console printout of
1 string
Cannot get Fix strings
And a Failute Trace of NullPointer…
Why is this equalling null despite being able to print out a result?
public static List<String> listFix()
{
List <String> fixString = null;
List fix = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
fix = session.createQuery("from metadataPoC.hib.TestHib").list();
for (Iterator iterator = fix.iterator(); iterator.hasNext();)
{
TestHib fixtable = (TestHib) iterator.next();
System.out.println("****************************************************");
System.out.println("****************************************************");
System.out.println(fixtable.getFixString());
System.out.println("****************************************************");
System.out.println("****************************************************");
fixString.add(fixtable.getFixString());
}
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
return fixString;
}
Quickly looking at your code, it seems you are not initializing the
properly. It will be null when you invoke a method on it. So instead of
initialize the list by, for example,