0

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.

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.

This is the DB 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 & 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; } } 

And here's how I'm trying to use it in my class:

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 && username != null && 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(); } } 
5
  • So, where did you initialize conn variable in FileManipulation class? Commented Mar 9, 2015 at 20:07
  • At the top, I have: static Connection conn = null; but in dbConnection I'm setting conn to s.getConnection(); which should return the actual connection Commented Mar 9, 2015 at 20:08
  • Exactly.. That is initialized to null, hence NPE Commented Mar 9, 2015 at 20:09
  • you're talking about two different instances of ''conn'' Commented Mar 9, 2015 at 20:11
  • I thought that by doing dbConnection connect = dbConnection.getInstance(); in FileManipulation's main, conn would also be updated [?] I see that's not correct, but I don't know how to pass conn between my classes Commented Mar 9, 2015 at 20:11

2 Answers 2

1

the connection which you used for the query, is not the connection which you defined in dbConnection class, do this:

public static void main (String[] args) throws SQLException { dbConnection connect = dbConnection.getInstance(); conn=connect.getConnection(); authorities_check(); } 
Sign up to request clarification or add additional context in comments.

Comments

0

You have to use a getConnection() from your dbConnection. You are doing dbConnection connect = dbConnection.getInstance(); but not using it. Rather you are using an instance variable which is never initialized.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.