尝试从另一个方法调用方法时,NUll指针异常,MYSQL DB连接

时间:2015-02-19 10:27:42

标签: java mysql methods nullpointerexception

我有一种从MYSQL db中检索信息的方法。

 public void doGetAdminUsers() {

    String queryName = "SELECT name FROM users WHERE user_id = 1";

    String queryPassword = "SELECT pass_word FROM users WHERE user_id = 1";

    try {
         Statement st = conn.createStatement();
         ResultSet rs1 = st.executeQuery(queryName);
        while (rs1.next()) {
            adminNameFromDB = rs1.getString(1);

        }

        ResultSet rs2 = st.executeQuery(queryPassword);

        while (rs2.next()) {
            adminPasswordFromDb = rs2.getString(1);
        }

    } catch (SQLException ex) {
        System.err.println(ex.getMessage());
    }


}

当我点击gui上的按钮时,我有一个方法在执行其他操作之前调用其他方法

private void LoginBtnActionPerformed(java.awt.event.ActionEvent evt){

     doGetAdminUsers();

    String name = userNameTf.getText();
    String password = passwordTf.getText();

    if (name.equals(adminNameFromDB) && (password.equals(adminPasswordFromDb))) {
        myHomeGUI.setVisible(true);
    } else {
        JOptionPane.showMessageDialog(null, "Incorrect login name or password");
    }
}         

但是我在输出中得到这个错误,下面的堆栈跟踪,我已经放置了在这里显示错误的重用行。

第316行语句st = conn.createStatement();

第209行doGetAdminUsers();

第20行公共类LoginGUI扩展了javax.swing.JFrame

第71行(自动生成)LoginBtnActionPerformed(evt)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Edu_Games.user_Interface.GUI.LoginGUI.doGetAdminUsers(LoginGUI.java:316)
at  Edu_Games.user_Interface.GUI.LoginGUI.LoginBtnActionPerformed(LoginGUI.java:209)
at Edu_Games.user_Interface.GUI.LoginGUI.access$000(LoginGUI.java:20)
at Edu_Games.user_Interface.GUI.LoginGUI$1.actionPerformed(LoginGUI.java:71)
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:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
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:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
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:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
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)[/code]



 [code]

私人连接conn;

public void setConn(Connection conn) {
    this.conn = conn;
}

public void closeConn() {
    try {
        this.conn.close();
    } catch (SQLException ex) {
        System.err.println(ex.getMessage());
    }
}[/code] that is in same class

然后在主程序中

 Class.forName("com.mysql.jdbc.Driver");
        String connectionURL = "jdbc:mysql://localhost:3306/EduTest?"
                + "user=root&password=password";
        Connection conn = DriverManager.getConnection(connectionURL); 
Connection conn = DriverManager.getConnection(connectionURL);
        LoginGUI myjdbcTest = new LoginGUI();

        myjdbcTest.setConn(conn);
        myjdbcTest.doTests();
        myjdbcTest.closeConn();

1 个答案:

答案 0 :(得分:0)

你变量conn为null。但是我们不知道你在哪里创建和初始化它,所以我们无法帮助。