Home » Support » Forum » JPA - ExceptionInInitializerError when creating EntityManager

JPA - ExceptionInInitializerError when creating EntityManager

#1

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(); 
emTemp.close(); 
        emf.close(); 
    } 
   //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:-

java.lang.ExceptionInInitializerError
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:-
http://www.dreamincode.net/forums/topic/285379-javaxpersistencepersistenceexception-javalangnullpointerexception/
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

application/zip iconPersistenceException.zip (2.94KB)
Containing Source Codes that Generate Runtime Exception
#2

> 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)
#3

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:-

/**
     * SHIFT SLOT TIME ALLOWED AFTER WHICH SHIFT WILL BE APPLICABLE. Time in If
     * 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'")
                    .setMaxResults(1).getSingleResult();
            if (minutes != null && minutes instanceof String)
                SHIFT_SLOT = Integer.parseInt(minutes.toString());
            else
                SHIFT_SLOT = 40; //Take default Time Slot as 40-minutes
        } catch (Exception ex) {
            SHIFT_SLOT = 40; //Take default Time Slot as 40-minutes
        }

        emf.close();
    }

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.


Post Reply

To post a reply and/or subscribe to update notifications - please login