This method is working fine. Here I'm fetching max bookid from book table but I'm getting null pointer exception when database is null. So, how to handle this exception.
public void insertData() {
Transaction tx = null;
Session session = getSession();
try {
tx = session.beginTransaction();
Book book = new Book();
SQLQuery query = session.createSQLQuery("select MAX(bookid) from books");
List<Book> rows = query.list();
for (Object row : rows) {
book.setBookid(Integer.parseInt(row.toString()));
Integer id1 = book.getBookid() + 1;
selbookid = id1;
}
book.setBookname("Java");
book.setDateofcreation(new Date());
book.setLastmodifieddate(new Date());
session.save(book);
tx.commit();
session.flush();
session.close();
} catch (RuntimeException e) {
e.printStackTrace();
}
}
NullPointerException? In the methodgetSession? Then you should check and handle it there? For me it is unclear, what your problem is...