我正在制作Maven项目并使用UDP侦听器部署到Wildfly 10服务器,我将Facade连接到我的数据库(MariaDB)。 我有一个测试网页,我发送一个字符串来进行处理,当我尝试使用UDP监听器进行相同的处理时出现问题。
我的UDP服务器侦听端口并获取消息但是使用我在网页中使用的相同过程在我的外观上带来了nullpointerexception,并且我发现只要我使用实体管理器就会发生这种情况。
我的UDP服务器主类(这是我用来测试的类,但它与部署的类相同)。
public class UDPServerRun {
public static void main(String[] args) {
System.out.println("UDP Server");
try {
DatagramSocket serverSocket = new DatagramSocket(8282);
byte[] receiveData = new byte[9];
byte[] sendData = new byte[9];
while (true) {
RegistrarEntrada entrada = new RegistrarEntrada();
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
System.out.println("RECEIVED: " + sentence + "!");
entrada.setCadenaEntrada(sentence);
String cadena = entrada.registrar();
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
sendData = cadena.getBytes();
DatagramPacket sendPacket
= new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
} catch (Exception ex) {
Logger.getLogger(RegistrarEntrada.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这是与Facade连接的类(也是测试类)。
public class RegistrarEntrada implements Serializable {
private AsistenciasFacadeLocal facade = new AsistenciasFacade();
private String cadenaEntrada;
public RegistrarEntrada() {
}
public String registrar() {
Calendar cal = Calendar.getInstance();
int idPersonal = -1;
String entradas[] = cadenaEntrada.split(" ");
String idChecador = entradas[0];
String idTarjeta = entradas[1];
//String idChecador = "A1";
//String idTarjeta = "F37C96";
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
//try {
idPersonal = facade.consultarIdPersonal(idTarjeta);
// } catch (Exception e) {
// System.out.println(e);
// }
if (idPersonal != -1) {
Personal persona = facade.consultarPersona(idPersonal);
if (persona != null) {
facade.registrarEntrada(idPersonal, idChecador);
return persona.getNombre() + " se registro a las: " + timeFormat.format(cal.getTime());
} else {
return "Error 02";
}
} else {
return "Error 01";
}
}
//Getters and Setters
public String getCadenaEntrada() {
return cadenaEntrada;
}
public void setCadenaEntrada(String cadenaEntrada) {
this.cadenaEntrada = cadenaEntrada;
}
public AsistenciasFacadeLocal getFacade() {
return facade;
}
public void setFacade(AsistenciasFacadeLocal facade) {
this.facade = facade;
}
}
Facade(我将标记出现错误的行)。
@Stateless
public class AsistenciasFacade implements AsistenciasFacadeLocal, AsistenciasFacadeRemote {
@PersistenceContext(unitName = "uvaqAsistenciasPU")
private EntityManager em;
protected EntityManager getEntityManager() {
return em;
}
public AsistenciasFacade() {
getEntityManager();
}
@Override
public void registrarEntrada(int idPersonal, String idChecador) {
Registros registro = new Registros();
Calendar fecha = Calendar.getInstance();
registro.setIdpersonal(idPersonal);
registro.setFechahoraregistro(fecha.getTime());
registro.setIdchecador(idChecador);
em.persist(registro);
}
@Override
public Personal consultarPersona(int idPersonal) {
String query = "SELECT * FROM personal WHERE IDPERSONAL=" + idPersonal;
List<Personal> objList = null;
objList = em.createNativeQuery(query, Personal.class).getResultList();
if (objList.size() != 0) {
return objList.get(0);
} else {
return null;
}
}
@Override
public int consultarIdPersonal(String idTarjeta) {
int idPersonal;
String query = "SELECT * FROM credenciales WHERE IDTARJETA = '" + idTarjeta + "'";
List<Credenciales> objList = null;
objList = em.createNativeQuery(query, Credenciales.class).getResultList(); //<====== The error happens here and also in every place where I use the entity manager
if (objList.size() != 0) {
idPersonal = Integer.parseInt(objList.get(0).getIdpersonal());
return idPersonal;
} else {
return -1;
}
}
@Override
public void sincronizarPersonal(List<String[]> personal) {
System.out.println("Funcion Sincronizar Personal en Asistencias");
List<Personal> nuevoPersonaltodos = new ArrayList<>();
for (int i = 0; i < personal.size(); i++) {
Personal nuevoPersonal = new Personal();
nuevoPersonal.setIdpersonal(Integer.parseInt(personal.get(i)[0]));
nuevoPersonal.setNombre(personal.get(i)[1]);
nuevoPersonal.setApellidopat(personal.get(i)[2]);
nuevoPersonal.setApellidomat(personal.get(i)[3]);
nuevoPersonal.setIdstatusper(Integer.parseInt(personal.get(i)[4]));
nuevoPersonaltodos.add(nuevoPersonal);
}
try {
em.persist(nuevoPersonaltodos);
} catch (Exception e) {
System.out.println(e);
}
}
}
我试图解释这个问题,现在我没有服务器日志,但是只要我能得到它,我就会在这里写。
编辑: 我的日志说明了这一点。
GRAVE: null
java.lang.NullPointerException
答案 0 :(得分:0)
在您RegistrarEntrada
中实例化EJB AsistenciasFacade
。为了注入EntityManager
,EJB需要由容器管理。这意味着RegistrarEntrada
的生命周期也需要由容器管理,并且您必须注入AsistenciasFacade
EJB。
换句话说,您无法实例化对象并将资源注入其中。容器需要处理这些类型对象的完整生命周期。