尝试使用DAO查找用户时出现NullPointerException

时间:2013-02-27 12:29:25

标签: java nullpointerexception actionlistener dao

尝试使用方法findUser查找用户时遇到问题。 我创建了一个窗口“登录”以在我的应用程序中连接,当我点击“连接”按钮时,会显示以下错误:

   Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.alstom.ems2.application.login.connectionActionPerformed(login.java:127)
    at com.alstom.ems2.application.login.access$100(login.java:16)
    at com.alstom.ems2.application.login$2.actionPerformed(login.java:61)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我正在使用NetBeans创建应用程序。

应用程序的代码是自动创建的,他在这里直到按钮部分:

private void initComponents() {

        login = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        connection = new javax.swing.JButton();
        password = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setIconImage(getIconImage());

        login.setPreferredSize(new java.awt.Dimension(150, 30));
        login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginActionPerformed(evt);
            }
        });

        jLabel2.setText("Password");

        jLabel1.setText("User");

        connection.setText("Connect");
        connection.setPreferredSize(new java.awt.Dimension(80, 25));
        connection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectionActionPerformed(evt);
            }
        });

按钮操作的connectionActionPerformed。

private void connectionActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:

    UserDAO u = null;
    User ut = null;

    ut = u.findUser(login.getText()); //error (login.java:127)

}

UserDAO上的方法findUser

public User findUser(String login) {
    try{
        return  (User) em.createQuery("FROM User u WHERE u.login = '" +login+ "'").getSingleResult();
    }catch(NoResultException n){
        return null;
    }
    }

最后一个,在Data Base创建用户。

public class App {
public static void main(String[] args) {
   EntityManager em = Data.createEntityManager();

    try {
       UserDAO u = new UserDAO (em);
       User us = new User();

       us.setLogin("lol");
       us.setPassword("test");

       u.create(us);  
    }
    catch (Exception e) {
        em.getTransaction().rollback();
        e.printStackTrace();
    }
    finally{
        em.close();
    }        
 }
}

谢谢你。

2 个答案:

答案 0 :(得分:5)

UserDAO u = null;
User ut = null;

ut = u.findUser(login.getText()); //error

您的UserDAO对象u设置为null。你将得到一个空指针错误,因为还没有u的实例。

答案 1 :(得分:1)

无法正常工作

UserDAO u = null;
User ut = null;

ut = u.findUser(login.getText()); //error

你将u声明为null然后调用findUser。您必须启动UserDAO:

UserDAO u = new UserDAO();
User ut = null;
ut = u.findUser(login.getText()); //error

顺便说一句......你绝对相信这会有效吗?

(User) em.createQuery("FROM User u WHERE u.login = '" +login+ "'").getSingleResult();

是否不需要“SELECT u FROM”?

(User) em.createQuery("Select u FROM User u WHERE u.login = '" +login+ "'").getSingleResult();