title: NullPointerException in java while trying to connect to MySQL database
body: <p>I'm having some trouble with my MySQL database connection. I'm trying to set up a connection between server and client. I'm geting a <code>NullPointerException</code> on the line with <code>Statement stat = this.conn.createStatement();</code> </p>
  
  <p>I am able to ping to the server from my client. Firewall is also turned off on the server.</p>
  
  <p>As you can see in the following image, I have added the MySQL jdbc driver to the project.</p>
  
  <p><img src="https://i.stack.imgur.com/3kVsD.jpg" alt="enter image description here"></p>
  
  <pre><code>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)
  </code></pre>
  
  <p><code>DatabaseConn.java</code></p>
  
  <pre><code>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());
          }
      }
  }
  </code></pre>
  
  <p>Does anyone have any idea?</p>