JPA中的NullPointerException[英] NullPointerException in JPA

本文是小编为大家收集整理的关于JPA中的NullPointerException的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用JPA创建Java EE应用程序.

我在服务器上运行代码时遇到以下错误

    [2/25/12 22:56:31:371 IST] 00000047 SystemOut     O hello
    [2/25/12 22:56:31:375 IST] 00000047 SystemOut     O Error...:(
    [2/25/12 22:56:31:371 IST] 00000047 SystemErr     R java.lang.NullPointerException
    [2/25/12 22:56:31:371 IST] 00000047 SystemErr     R     at plh.service.ejb.UserBean.getUserDetails(UserBean.java:41)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:45)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at java.lang.reflect.Method.invoke(Method.java:599)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at com.ibm.etools.utc.model.ReflectionMethodModel.invoke(ReflectionMethodModel.java:65)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at com.ibm.etools.utc.servlet.InvokeServlet.invoke(InvokeServlet.java:113)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at com.ibm.etools.utc.servlet.InvokeServlet.doPost(InvokeServlet.java:374)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1443)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:790)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:443)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:175)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:91)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:859)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1557)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:173)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:455)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:384)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    [2/25/12 22:56:31:375 IST] 00000047 SystemErr     R     at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    [2/25/12 22:56:31:375 IST] 00000047 SystemErr     R     at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:202)
    [2/25/12 22:56:31:375 IST] 00000047 SystemErr     R     at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:766)
    [2/25/12 22:56:31:375 IST] 00000047 SystemErr     R     at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:896)
    [2/25/12 22:56:31:375 IST] 00000047 SystemErr     R     at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1527)

userbean.java中的第41号线是:

//在UserBean.java中,包含UserBean无状态EJB

    public class UserBean implements UserBeanInterface {

      /**
      * Default constructor. 
      */
@PersistenceContext
    private EntityManager manager;

public UserBean() {
}
public String authenticate(String name){
    return null;
}
public String changePassword(){
    return null;
}
public String setUserDetails(){
    return null;
}
public String getUserDetails(){
    System.out.println("hello");
    try{
41=>  Query query=manager.createQuery("from User");

        List<User> results = query.getResultList();

        if(results.size()!=0){
            Iterator<User> stIterator=results.iterator();
            while(stIterator.hasNext()){
                User st=stIterator.next();
                System.out.print("User Id:"+st.getUserId());
                System.out.print("Type:"+st.getUserType());
                System.out.print("Detail:"+st.getUserProfile());
            }
        }
        else
        {
            System.out.println("Record not found.");
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
        System.out.println("Error...:(");
    }
    return "hi";

    }
    }

用户类,用户已从数据库表自动生成.数据库中有一个用户表.请帮助有关此错误的可能原因.

推荐答案

您的EntityManager manager是在客户端UserBean中注入的企业Java bean. EJB应注入由应用程序服务器管理的生命周期的类.

客户端必须是Web组件或其他企业Bean.就您而言,客户端UserBean是POJO(普通的旧Java对象).请参阅在这里有关如何使用EJBS的更多详细信息.

一个可能的简单解决方案是通过在类定义之前添加注释Javax.ejb.Stateless,使UserBean成为无状态会话bean.您最终应该通过EJB注释将UserBean注入托管bean.

其他推荐答案

在这种情况下,请确保在创建包含Entity Manager的类的对象之前,请确保编写@EJB注释. 零指针异常的原因通常是因为EntityManager未经初始化. @EJB注释做到了.

在您的情况下,您声明用户类的对象写作的类 @ejb上方.这应该解决问题.

本文地址:https://www.itbaoku.cn/post/597366.html

问题描述

I am creating a Java EE application using jpa.

I am getting the following error when the code is run on server

    [2/25/12 22:56:31:371 IST] 00000047 SystemOut     O hello
    [2/25/12 22:56:31:375 IST] 00000047 SystemOut     O Error...:(
    [2/25/12 22:56:31:371 IST] 00000047 SystemErr     R java.lang.NullPointerException
    [2/25/12 22:56:31:371 IST] 00000047 SystemErr     R     at plh.service.ejb.UserBean.getUserDetails(UserBean.java:41)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:45)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at java.lang.reflect.Method.invoke(Method.java:599)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at com.ibm.etools.utc.model.ReflectionMethodModel.invoke(ReflectionMethodModel.java:65)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at com.ibm.etools.utc.servlet.InvokeServlet.invoke(InvokeServlet.java:113)
    [2/25/12 22:56:31:372 IST] 00000047 SystemErr     R     at com.ibm.etools.utc.servlet.InvokeServlet.doPost(InvokeServlet.java:374)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1443)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:790)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:443)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:175)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:91)
    [2/25/12 22:56:31:373 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:859)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1557)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:173)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:455)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:384)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    [2/25/12 22:56:31:374 IST] 00000047 SystemErr     R     at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    [2/25/12 22:56:31:375 IST] 00000047 SystemErr     R     at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    [2/25/12 22:56:31:375 IST] 00000047 SystemErr     R     at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:202)
    [2/25/12 22:56:31:375 IST] 00000047 SystemErr     R     at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:766)
    [2/25/12 22:56:31:375 IST] 00000047 SystemErr     R     at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:896)
    [2/25/12 22:56:31:375 IST] 00000047 SystemErr     R     at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1527)

Line no 41 in UserBean.java is :

//within UserBean.java, containing UserBean a stateless EJB

    public class UserBean implements UserBeanInterface {

      /**
      * Default constructor. 
      */
@PersistenceContext
    private EntityManager manager;

public UserBean() {
}
public String authenticate(String name){
    return null;
}
public String changePassword(){
    return null;
}
public String setUserDetails(){
    return null;
}
public String getUserDetails(){
    System.out.println("hello");
    try{
41=>  Query query=manager.createQuery("from User");

        List<User> results = query.getResultList();

        if(results.size()!=0){
            Iterator<User> stIterator=results.iterator();
            while(stIterator.hasNext()){
                User st=stIterator.next();
                System.out.print("User Id:"+st.getUserId());
                System.out.print("Type:"+st.getUserType());
                System.out.print("Detail:"+st.getUserProfile());
            }
        }
        else
        {
            System.out.println("Record not found.");
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
        System.out.println("Error...:(");
    }
    return "hi";

    }
    }

The User class, User has been generated automatically from the database tables. There is a USER table in the database. Please help on what the possible reason for this error could be.

推荐答案

Your EntityManager manager is no-interface an Enterprise Java Bean injected in your client UserBean. EJBs should be injected in classes whose lifecycle is managed by the application server.

The client must be either a web component or another enterprise bean. In your case, the client UserBean is a POJO (Plain Old Java Object). See here for further details about how to use EJBs.

A possible simple solution is to make UserBean a Stateless Session Bean by adding the annotation javax.ejb.Stateless before the class definition. And you should eventually inject UserBean into your ManagedBean with the EJB annotation.

其他推荐答案

in such cases make sure you write the@EJB annotation before creating the object of the class containing the entity manager. the reason for the null pointer exception is generally because the entitymanager is not initialised. @EJB annotation does that.

In your case the class where you declare an object of user class write @EJB above it.this should solve the problem.