尝试持久化实体时获取NullPointerException

时间:2013-12-08 19:02:49

标签: jpa nullpointerexception eclipselink

我有以下代码。当我运行junit测试时,它会创建一个表和字段,但它们是null。我也得到一个NullPointerException .. 为什么我的用户没有添加?

java.lang.NullPointerException
    at com.testproject.repo.JPAUserRepo.addUser(JPAUserRepo.java:15)
    at com.testproject.manager.UserManager.saveUser(UserManager.java:18)
    at com.testproject.manager.test.UserManagerTest.testSaveUser(UserManagerTest.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

User.java

@Entity
@Table(name="USER")
@NamedQuery(name="getAllUser", query="SELECT a FROM User a ORDER BY a.userid")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="USER_ID")
    private long userid;

    @Column(name="USERNAME")
    private String username;

    @Column(name="PASSWORD")
    private String password;

    @Column(name="USER_EMAIL")
    private String userEmail;

    @Column(name="USER_PHONE")
    private int userPhone;

    public User() {
        // TODO Auto-generated constructor stub
    }

    public User(String username, String password, String email, int userPhone) {
        super();
        this.username = username;
        this.password = password;
        this.userEmail = email;
        this.userPhone = userPhone;
    }
//Getters and Setters

JPAUserRepo.java

public class JPAUserRepo implements UserRepo {

    EntityManager em;

    @Override
    public void addUser(User user) {
    em.persist(user);
        }

UserManager.java

public class UserManager {

    UserRepo userRepo;

    public void saveUser(User user){
        userRepo.addUser(user);
    }

UserManagerTest.java

public class UserManagerTest {

    EntityManager em;
    UserManager userManager;
    User user;


    @Before
    public void setUp() throws Exception {

        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnitMySQL");
        em = factory.createEntityManager();

        userManager = new UserManager();
        JPAUserRepo jpaUserRepo = new JPAUserRepo();
        userManager.setUserRepo(jpaUserRepo);


        em.getTransaction().begin();
        user = new User("name", "password", "mail@mail.com", 1561234567);
    }

    @After
    public void tearDown() throws Exception {
        em.getTransaction().commit();
    }

    @Test
    public void testSaveUser() {
        userManager.saveUser(user);

    }

}

0 个答案:

没有答案