technophebe
technophebe

Reputation: 494

JPA connection works in Eclipse but gives NullPointerException when compiled

I'm working on this Java project, however I'm a C# developer and completely new to the environment so apologies if there are any really basic errors here, I'm totally winging it!

Basically my JPA connection works fine when running in Eclipse, but as soon as I compile to a .jar I get a java.lang.NullPointerException.

Here's my code:

public List<DecUpdate> getDecUpdates() {

    EntityManagerFactory conn = null;
    EntityManager db = null;

    try {

        // Connect:
        conn = Persistence.createEntityManagerFactory("DatabaseName");
        db = conn.createEntityManager();            

        // Get entities:
        String queryString = "select d from DecUpdate d "
                            + "where d.Requested >= :from "
                            + "and d.Completed is null "
                            + "and d.Requested = "
                                + "(select max(d1.Requested) from DecUpdate d1 "
                                    + "where d.Requested >= :from "
                                    + "and d1.Completed is null "
                                    + "and d1.GroupId = d.GroupId "
                                    + "and d1.ServiceId = d.ServiceId) "
                            + "and not exists "
                                + "(select d2 from DecUpdate d2 "
                                    + "where d.Requested >= :from "
                                    + "and d2.Requested > d.Requested "
                                    + "and d2.GroupId = d.GroupId "
                                    + "and d2.ServiceId = d.ServiceId) ";

        Query query = db.createQuery(queryString);
        query.setParameter("from", new Timestamp(new DateTime().minusHours(3).getMillis()));

        List<DecUpdate> resultList = query.getResultList();

        return resultList;
    }
    catch (Exception e) {

        throw e;
    }
    finally { // Close context and connection:

        db.close();
        conn.close();
    }
}

And this is my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
   <persistence-unit name="DatabaseName" transaction-type="RESOURCE_LOCAL">
       <exclude-unlisted-classes>false</exclude-unlisted-classes>
       <properties>
           <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
           <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://1.1.1.1:1111;DatabaseName=DatabaseName"></property>
           <property name="javax.persistence.jdbc.user" value="foo"></property>
           <property name="javax.persistence.jdbc.password" value="bar"></property>

           <!-- EclipseLink should create the database schema automatically -->
           <property name="eclipselink.ddl-generation" value="create-tables" />
           <property name="eclipselink.ddl-generation.output-mode" value="database" />
       </properties>
   </persistence-unit>
</persistence>

I've read around the subject as much as I can but I can't figure out why this is working in the development environment but not when compiled. I suspect I'm lacking some basic knowledge that is being assumed by all of the tutorials I'm reading but which I don't have since I'm self training.

Anyone care to point out my rookie error?

Edit:

Thanks to some sleuthing from @ThomasEdwin, I've uncovered a more helpful error:

org.eclipse.persistence.exceptions.PersistenceUnitLoadingException: 
Exception Description: An exception was thrown while trying to load persistence unit at url: rsrc:../
Internal Exception: Exception [EclipseLink-30004] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: rsrc:../
Internal Exception: java.net.MalformedURLException
    at...

That looks to me like I've referenced my persistence.xml incorrectly or something? It's in the src/META-INF folder, does it need to be moved, or included or referenced somewhere during compile perhaps?

Upvotes: 1

Views: 824

Answers (1)

ThomasEdwin
ThomasEdwin

Reputation: 2145

First edit:

java.lang.NullPointerException at myRepository.DatabaseNameContext.getDecUpdates(DatabaseNam‌​‌​eContext.java:63)

  at Main$1.run(Main.java:51)
  at java.util.TimerThread.mainLoop(Unknown Source)
  at java.util.TimerThread.run(Unknown Source)

The NPE masks the actual error. Modify the line 63 to if (db != null) db.close();

Second edit:

org.eclipse.persistence.exceptions.PersistenceUnitLoadingException: 
Exception Description: An exception was thrown while trying to load persistence unit at url: rsrc:../
Internal Exception: Exception [EclipseLink-30004] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5):
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: rsrc:../
Internal Exception: java.net.MalformedURLException
    at...

As discussed in eclipselink PersistenceUnitLoadingEception in executable JAR, EclipseLink has problem in handling executable jar. Try selecting the option "Copy required libraries into a sub-folder".

Upvotes: 1

Related Questions