Lars, One obvious difference between your code and the out of box sample is the way the driver class and its properties are specified. build.xml vs persistence.xml. It looks like the org.apache.derby.jdbc.EmbeddedDriver class from your persistence.xml is not being found or the connection url is not getting through.
Regards V -----Original Message----- From: Lars Vogel [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 12, 2008 9:22 AM To: [email protected] Subject: JPA - Null pointer exception Hi, I trying to make JPA work for me. The delivered example works without problems, I can also successfully modify this example. The example was based on http://openjpa.apache.org/quick-start.html. But if I create a new project the Entity Manager returns only a NULL value. Error: Exception in thread "main" java.lang.NullPointerException at hellojpa.Main.main(Main.java:44) I use the same files as in the working example. All jars are included in the build path. I assume that the persistence.xml is not found but to my knowledge the classpath is correctly maintained. The path .../bin contains the directory META-INF and here the file persistence.xml. Any advice? I must be missing something very simple but cannot figure it out. For example coding please see below. Best regards, Lars File structure: src - hellojpa - Main.java - Message.java - META-INF - persistence.xml Same structure in the bin directory. -----------persistence.xml.............. <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"> <!-- A persistence unit is a set of listed persistent entities as well the configuration of an EntityManagerFactory. We configure each example in a separate persistence-unit. --> <persistence-unit name="hellojpa" transaction-type="RESOURCE_LOCAL"> <!-- The default provider can be OpenJPA, or some other product. This element is optional if OpenJPA is the only JPA provider in the current classloading environment, but can be specified in cases where there are multiple JPA implementations available. --> <provider> org.apache.openjpa.persistence.PersistenceProviderImpl </provider> <!-- We must enumerate each entity in the persistence unit --> <class>hellojpa.Message</class> <properties> <property name="openjpa.ConnectionURL" value="jdbc:derby:C:/DerbyDatabases/hellojpa-database5;create=true" /> <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver" /> <property name="openjpa.ConnectionUserName" value="" /> <property name="openjpa.ConnectionPassword" value="" /> <!-- Tell OpenJPA to automatically create tables in the database for entities. Note that this should be disabled when running against a production database, since you probably don't want to be altering the schema at runtime. --> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" /> </properties> </persistence-unit> </persistence> -----------Main.java /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package hellojpa; import java.util.*; import javax.persistence.*; /** * A very simple, stand-alone program that stores a new entity in the * database and then performs a query to retrieve it. */ public class Main { @SuppressWarnings("unchecked") public static void main(String[] args) { System.out.println(System.getProperty("java.class.path")); // Create a new EntityManagerFactory using the System properties. // The "hellojpa" name will be used to configure based on the // corresponding name in the META-INF/persistence.xml file EntityManagerFactory factory = Persistence. createEntityManagerFactory("hellojpa", System.getProperties()); // Create a new EntityManager from the EntityManagerFactory. The // EntityManager is the main object in the persistence API, and is // used to create, delete, and query objects, as well as access // the current transaction EntityManager em = factory.createEntityManager(); // Begin a new local transaction so that we can persist a new entity em.getTransaction().begin(); // Create and persist a new Message entity em.persist(new Message("Hello Persistence!")); // Commit the transaction, which will cause the entity to // be stored in the database em.getTransaction().commit(); // It is always good practice to close the EntityManager so that // resources are conserved. em.close(); // Create a fresh, new EntityManager EntityManager em2 = factory.createEntityManager(); // Perform a simple query for all the Message entities Query q = em2.createQuery("select m from Message m"); // Go through each of the entities and print out each of their // messages, as well as the date on which it was created for (Message m : (List<Message>) q.getResultList()) { System.out.println(m.getMessage() + " (created on: " + m.getCreated() + ")"); } // Again, it is always good to clean up after ourselves em2.close(); factory.close(); } } -----------
