gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:53:27 25 4
gpt4 key购买 nike

我在尝试使用 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();
}
}
}

谢谢你。

最佳答案

UserDAO u = null;
User ut = null;

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

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

关于java - 尝试使用 DAO 查找用户时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15112178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com