2016-12-27 68 views
0

我创建了一个名为修订时,EJB服务器端Maven项目和Maven的客户项目修订时,EJB客户端时得到NullPointerException异常。Maven的:试图插入到数据库

数据库表已创建,但在尝试插入到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

正如你从容器外部调用EJB的,你需要来注释接口

@Remote 
public interface PlayerServiceRemote { 
错误

您可以检查信息when to use @Remote

+0

谢谢,我已经找到了,半小时前经过一整天的搜索。 –