632 words

JPA - ExceptionInInitializerError when creating EntityManager

2014-12-09 04:14

EntityFactoryManager(emf) is successfully received but error at emf.createEntityManager
Please see my code of fragment from my project.

public class Main { 
    final private static Properties EMAIL_PROPS; 
    final private static String EMAIL_FROM; 
    static { 
EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("tmpCLMSPU"); 
//       System.out.println("EMF: "+emf); //OUTPUT: EMF: org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl@ffa7e1 
        EntityManager emTemp = emf.createEntityManager(); 
TypedQuery<EmailSettings> emailSettings = emTemp.createNamedQuery("EmailSettings.findByName", EmailSettings.class).setMaxResults(1); 
        EmailSettings smtp, port, from; 
        smtp = emailSettings.setParameter("name", "SMTP").getSingleResult(); 
        port = emailSettings.setParameter("name", "Port").getSingleResult(); 
        from = emailSettings.setParameter("name", "EMAILFROM").getSingleResult(); 
        // Get system properties 
        EMAIL_PROPS = System.getProperties(); 
        // Setup mail server 
EMAIL_PROPS.put("mail.smtp.host", smtp.getValue()); 
EMAIL_PROPS.put("mail.smtp.port", port.getValue());  
        EMAIL_FROM = from.getValue(); 
   //Empty Constructor 
  public void Main() { 
public static void main(String[] args) { 
    new Main(); 

My project was was running successfully but All of sudden, it is showing following error:-

Caused by: javax.persistence.PersistenceException: java.lang.NullPointerException
                at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:766)

I am using Netbeans JAVA IDE for my project. I am very frustrated after lot of tries at my end to solve this problem. I tried following but FAILED:-
I ran my program again after each of following step but still Error is showing.
I check my database services. It is successfully connected. Connection is OK.
Restart Database server (SQL Server) services on Server.
Delete NetBeans configuration's Users directories.
Restart Server & Also, My client Computer.
Create a separate project & copy my original sources into it. I did the all configuration part of my project again i.e. adding Libraries, Setup Database Connections & Persistence Unit again.
However same database setup is working fine for other project. I found same problem on Internet:-
No proper solution is given on above link Because it is not NullPointerException. EntityFactoryMnager is retrieved successfully. Please see the attachments:-
EmailSettings.java : an Entity
Main.java : Main program to run
Persistence.xml.txt : File containing persistence information
Stacktrace.txt : complete stack trace after error is showing.

Have anyone idea to solve this problem?

Thanks In Advance,


gajesh's picture
Joined on 2014-12-09
User Post #1
2014-12-09 04:54

> Restart Database server (SQL Server) services on Server.

This is the ObjectDB website and the forum focuses on using ObjectDB and not on other DBMS.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support's picture
Joined on 2010-05-03
User Post #2,095
2014-12-09 08:57

Thanks for your Reply.

I'll take care your point & post only problem to this Forum which is related to objectdb.

This problem is SOLVED. For the sake of completion of this forum, I am giving solution here:-

I have an Entity named ShiftDetail in my project that has following error codes of line:-

     * any error occurred for getting this value from CLMS S/w then set it's
     * default value as 40 minutes. Minutes
    public static int SHIFT_SLOT; //Field SHIFTATTENDAFTERSLOT from AttendanceSettings table
    static {
        javax.persistence.EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("myDBPU");
        javax.persistence.EntityManager em = emf.createEntityManager();
        try {
            Object minutes = em.createNativeQuery("SELECT AttendanceSettings.Value FROM AttendanceSettings WHERE rtrim(LTRIM(Name))='SHIFTATTENDAFTERSLOT'")
            if (minutes != null && minutes instanceof String)
                SHIFT_SLOT = Integer.parseInt(minutes.toString());
                SHIFT_SLOT = 40; //Take default Time Slot as 40-minutes
        } catch (Exception ex) {
            SHIFT_SLOT = 40; //Take default Time Slot as 40-minutes


I should not use static field within an Entity & should not use EntityFactoryManager to initialize this field. I removed above lines from my source & I got solution... Thanks.

gajesh's picture
Joined on 2014-12-09
User Post #2

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Maximum file size: 32 MB