尝试连接到 MySQL 数据库时 java 中的 NullPointerException
Posted
技术标签:
【中文标题】尝试连接到 MySQL 数据库时 java 中的 NullPointerException【英文标题】:NullPointerException in java while trying to connect to MySQL database 【发布时间】:2015-05-20 12:25:08 【问题描述】:我的 mysql 数据库连接出现问题。我正在尝试在服务器和客户端之间建立连接。我收到了NullPointerException
与Statement stat = this.conn.createStatement();
在线交流
我可以从我的客户端 ping 到服务器。服务器上的防火墙也已关闭。
如下图所示,我已将 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());
有人知道吗?
【问题讨论】:
请发布您的堆栈跟踪 请在此处发布您的堆栈跟踪 Stacktrace 包括在内,见下图:i.stack.imgur.com/4suqR.png 是的,既然有些地方屏蔽了分享网站(你能想象...),还是贴在帖子里比较好... 在 jdbc:mysql://[IPADDRESS]:3306/lms 你有没有把 [IPADDRESS] 改成你的 IP 地址? 【参考方案1】:你必须检查 conn 不为空:
private void closeConnection()
try
if(conn != null)
conn.close();
conn = null;
catch (SQLException ex)
System.err.println(ex.getMessage());
【讨论】:
以上是关于尝试连接到 MySQL 数据库时 java 中的 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
尝试连接到 Mysql 数据库时找不到类 com.mysql.jdbc.Driver jdbc