2

尝试使用 method 查找用户时遇到问题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;
    }
    }

最后一个,在数据库中创建用户。

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();
    }        
 }
}

谢谢。

4

2 回答 2

5
UserDAO u = null;
User ut = null;

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

您的 UserDAO 对象 u 设置为 null。您将收到空指针错误,因为尚不存在 u 的实例。

于 2013-02-27T12:31:12.800 回答
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();
于 2013-02-27T12:33:31.230 回答