EntityManagerFactory emf;
EntityManager em = null;
try {
emf =
Persistence.createEntityManagerFactory("oo");
em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(ri);
em.getTransaction().commit();
} finally {
em.close();
}
If you throw an exception in Persistence.createEntityManagerFactory
(), em will be null when you try to call close on it. Put an
if (em != null)
test in your finally.