title: Implementing Singleton DB Connection in Java
body: <p>I'm trying to implement a singleton DB connection in Java. I think I have it mostly correct, but when I try to use it in other classes, I keep getting java.lang.NullPointerExceptions. </p>
  
  <p>The error I'm getting is in my FileManipulation class, on the PreparedStatement line where I do conn.PreparedStatement(query); 
  I think conn is being passed as null, but I don't know why or how to fix it. </p>
  
  <p>This is the DB code: </p>
  
  <pre><code>public class dbConnection {
  
      static String newLine = System.getProperty("line.separator");
  
      volatile static dbConnection s;
  
      public static Connection conn; 
      public static Statement stmt = null;
  
      private dbConnection() throws SQLException {
          // create single instance of DB connection 
          Properties connectionProps = new Properties();
          connectionProps.put("user", "root");
          connectionProps.put("password", "password");
          // port &amp; db name 
          conn = DriverManager.getConnection("jdbc:mysql://"
                  + "localhost" + ":" + "3306" + "/" + "DBNAME",
                  connectionProps);
      }
  
      public Connection getConnection() throws SQLException {
          return conn; 
      }
  
      public static dbConnection getInstance() throws SQLException {
  
          if (s == null) {
              synchronized(dbConnection.class) {
                  if (s == null) {
                      s = new dbConnection(); 
                      System.out.print("First DB connection created." + newLine); 
              }
          }
      }
          conn = s.getConnection();
          return s; 
      }
  }
  </code></pre>
  
  <p>And here's how I'm trying to use it in my class: </p>
  
  <pre><code>public class FileManipulation {
  
      final static String authoritiesTable = "Authorities";
  
      static Connection conn = null; 
      static Statement stmt = null;
      static BufferedReader reader = null;
  
      static String position;
      static String username;
      static String password; 
  
      public static void authorities_check() {
          try {
              Scanner scan = new Scanner( System.in );
              boolean authentication = false;
  
              while (authentication == false) {
                  //get user input 
                  System.out.print("Enter your position: ");
                  position = scan.nextLine();
                  System.out.print("Enter your username: ");
                  username = scan.nextLine();
                  System.out.print("Enter your password: ");
                  password = scan.nextLine();
                  if (position != null &amp;&amp; username != null &amp;&amp; password != null) {
                      authentication = true; 
                  }
              }
              String query = "SELECT * FROM " + authoritiesTable + " WHERE position = ?" + " AND " + "username = ?" + " AND " + "password = ?";
              PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(query); 
              pstmt.setString(1, position);
              pstmt.setString(2, username);
              pstmt.setString(3, password);
              ResultSet rs = pstmt.executeQuery(); 
  
              //if user with position, username, and password exists
              if (rs.next()) { 
                  //let them edit the file 
                  System.out.println("User authenticated...");
              }
              else {
                  System.out.println("Error: Could not authenticate!");
              }
              scan.close();
          }
          catch(SQLException se) {
              se.printStackTrace();
          }
          finally {
              try {
                  if(stmt != null || conn != null) {
                      conn.close();
                  }
              }
              catch(SQLException se) {
                  se.printStackTrace();
              }
          }
      }
  
      public static void main (String[] args) throws SQLException {
          dbConnection connect = dbConnection.getInstance();
          authorities_check();
      }
  }
  </code></pre>