I have a General class which has static methods and static variables with Hibernate configuration settings and methods which return List after hitting the database. I am working on JavaFx and i recently learned its better to use Task for time consuming operations like hitting the db for a long list of data etc. So, here in my case, i created a Task object, wrote the code in anonymous inner class which contains the code for hitting the db for the LOGIN credential. The Task must return an instance of List. I intialized a Thread object inside the static method, passed Task's object in its constructor, set the daemon to true and started the Thread. However i am getting NullPointerException after running the application..
private static SessionFactory sessionFactory = null;
private static Session session = null;
private static final Transaction transaction = null;
private static final Configuration configuration = null;
private static List list;
public static List getSelectList(final String query)
{
//list = null;
System.err.println("Inside getSelectList");
try{
final Task <List> task= new Task <List> ()
{
@Override
public List call()
{
try
{
System.err.println("Inside call");
session.beginTransaction();
list = session.createQuery(query).list();
System.err.println(list);
session.getTransaction().commit();
return list;
}
catch (Exception e)
{
session.getTransaction().rollback();
e.printStackTrace();
}
System.err.println("Outta try block");
return null;
}
};
task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent t) {
System.err.println("Inside handle");
list = task.getValue();
/* for (Iterator it = list.iterator();it.hasNext(); )
{
System.err.println("Inside Set on succeeded");
//System.err.println( ((Login)it.next()).getUsername());
} */
}
});
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
//list=task.getValue();
}
catch (Exception e) {e.printStackTrace();}
return list;
}
However, after many combinations and random permutations, things worked for me with commenting the Thread code and instead substituting it with task.run();
/*Thread th = new Thread(task);
th.setDaemon(true);
th.start();*/
task.run();
Also i had to change the local variable List list = session.createQuery(query).list(); by declaring static List list; and initializing this static list = session.createQuery(query).list(); since it was throwing me the exception too even if i removed Thread code and added task.run()
try
{
session.beginTransaction();
//List locallist = session.createSQLQuery(query).list();
list = session.createSQLQuery(query).list();
session.getTransaction().commit();
return list ;
}
catch (Exception e){
session.getTransaction().rollback();
I wish to know why this happened. Why task.run() along with using static List worked for me?
Converting List to static and removing the Thread instance and using task.run(); was merely a number to different tries from my side to make my code work. i dunno the reasons. I will be greatly humbled for any explanations. Thanks in advance.