简体   繁体   English

尝试连接到MySQL数据库时Java中的NullPointerException

[英]NullPointerException in java while trying to connect to MySQL database

I'm having some trouble with my MySQL database connection. 我的MySQL数据库连接遇到问题。 I'm trying to set up a connection between server and client. 我正在尝试在服务器和客户端之间建立连接。 I'm geting a NullPointerException on the line with Statement stat = this.conn.createStatement(); 我在Statement stat = this.conn.createStatement();的行上得到一个NullPointerException Statement stat = this.conn.createStatement();

I am able to ping to the server from my client. 我可以从客户端ping到服务器。 Firewall is also turned off on the server. 服务器上的防火墙也已关闭。

As you can see in the following image, I have added the MySQL jdbc driver to the project. 如下图所示,我已将MySQL jdbc驱动程序添加到项目中。

在此处输入图片说明

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at testdatabaseconn.DatabaseConn.closeConnection(DatabaseConn.java:58)
at testdatabaseconn.DatabaseConn.loadAccount(DatabaseConn.java:44)
at testdatabaseconn.TestDatabaseConn$1.handle(TestDatabaseConn.java:37)
at testdatabaseconn.TestDatabaseConn$1.handle(TestDatabaseConn.java:30)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8216)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)

DatabaseConn.java

public class DatabaseConn {

    private Connection conn;

    public String loadAccount() throws SQLException, IOException,  ClassNotFoundException {
        String username = null;

        try {
            this.initConnection();

            Statement stat = this.conn.createStatement();
            String accountQuery = "SELECT username FROM account WHERE username = 'Ruubje'";

            ResultSet persoonResults = stat.executeQuery(accountQuery);


            while (persoonResults.next()) {
                username = persoonResults.getString("username");
            }

        } catch (SQLException | IllegalArgumentException ex) {
            throw new IOException(ex.getMessage());
        } finally {
            this.closeConnection();
        }

        return username;
    }

    private void initConnection() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");

        conn = DriverManager.getConnection("jdbc:mysql://[IPADDRESS]:3306/lms", "root", "usbw");
    }

    private void closeConnection() {
        try {
            conn.close();
            conn = null;
        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
        }
    }
}

Does anyone have any idea? 有人有什么主意吗?

您必须检查conn不为空:

private void closeConnection() { try { if(conn != null){ conn.close(); conn = null; } } catch (SQLException ex) { System.err.println(ex.getMessage()); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM