Java 带有sql语句的Actionlistener中的NullPointerException
我在从sql语句中获取密码时遇到了问题,当我尝试在一个简单的类中使用密码时,它对我来说非常有效,但当我尝试在GUI actionlistener中使用密码时,我获取了NullPointerException。这是我的一部分代码,我希望你能找到解决这个问题的办法 代码: 我的sql语句的代码:Java 带有sql语句的Actionlistener中的NullPointerException,java,sql,user-interface,actionlistener,Java,Sql,User Interface,Actionlistener,我在从sql语句中获取密码时遇到了问题,当我尝试在一个简单的类中使用密码时,它对我来说非常有效,但当我尝试在GUI actionlistener中使用密码时,我获取了NullPointerException。这是我的一部分代码,我希望你能找到解决这个问题的办法 代码: 我的sql语句的代码: public String login(String username) throws ClassNotFoundException, SQLException, IndexOutOfBoundsE
public String login(String username) throws ClassNotFoundException, SQLException, IndexOutOfBoundsException, NullPointerException
{
DBConnection db = new DBConnection("sdj");
ArrayList<Object[]> result;
result = db.query("SELECT password FROM `users` WHERE username = ?", username);
Object[] temp;
String incorrect = "incorrect";
try
{
temp = result.get(0);
}
catch (IndexOutOfBoundsException exception)
{
return incorrect;
}
String usr = (String) temp[0];
return usr;
}
您不必在代码中的任何地方初始化变量
private DBController db
。然后在actionPerformed()
方法中,尝试调用此空变量的login方法:
temp = db.login(loginText.getText());
您可能会将其与您在登录方法中声明的db混淆在一起,这是不同的。1)为了更快地获得更好的帮助,请发布一个。2) 源代码中只有一行空白就足够了。{
之后或}
之前的空行通常也是多余的。
java.lang.NullPointerException
at GUI.Main$LoginListener.actionPerformed(Main.java:90)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
temp = db.login(loginText.getText());