Maven:尝试插入数据库时​​获取NullPointerException

时间:2016-12-27 18:03:39

标签: maven java-ee

我创建了一个名为 revison-ejb 的服务器端Maven项目和Maven客户端项目 revison-ejb-client

数据库表已经创建,但在尝试插入t_player表时出现NullPointerException。 任何帮助将不胜感激!

修改过程的EJB

Player.java

package edu.foot.entities;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Entity implementation class for Entity: Player
 *
 */
@Entity
@Table(name = "t_player")

public class Player implements Serializable {

    private int id;
    private int age;
    private String nom;
    private static final long serialVersionUID = 1L;

    public Player() {
        super();
    }

    public Player(int age, String nom) {
        super();
        this.age = age;
        this.nom = nom;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getNom() {
        return this.nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    @Override
    public String toString() {
        return "Player [id=" + id + ", age=" + age + ", nom=" + nom + "]";
    }

}

PlayerServiceRemote.java

    package edu.foot.interfaces;

import edu.foot.entities.Player;

public interface PlayerServiceRemote {

    void add(Player player);

    void update(Player player);

}

PlayerService

package edu.foot.interfaces.impl;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import edu.foot.entities.Player;
import edu.foot.interfaces.PlayerServiceRemote;

@Stateless
public class PlayerService implements PlayerServiceRemote {

    @PersistenceContext
    EntityManager em;

    public void add(Player player) {
        em.persist(player);

    }

    public void update(Player player) {
        em.merge(player);

    }

}

修改过程的EJB的客户端

package edu.esprit.irt.Player;

import javax.naming.InitialContext;

import edu.foot.entities.Player;
import edu.foot.interfaces.PlayerServiceRemote;

public class AddPlayer {
    public static void main(String[] args) {
        InitialContext ctx = null;
        PlayerServiceRemote proxy = null;
        String jndi = "revison-ejb/PlayerService!edu.foot.interfaces.PlayerServiceRemote";

        try {
            ctx = new InitialContext();
            proxy = (PlayerServiceRemote) ctx.lookup(jndi);
        } catch (Exception e) {

        }
        Player p1 = new Player(10, "Dirar");

        proxy.add(p1);

    }

}

错误

Exception in thread "main" java.lang.NullPointerException
at edu.esprit.irt.Player.AddPlayer.main(AddPlayer.java:25)

1 个答案:

答案 0 :(得分:1)

当您从容器外部调用EJB时​​,需要将接口注释为

use R;

您可以查看有关when to use @Remote

的信息