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)
謝謝,我已經找到了,半小時前經過一整天的搜索。 –