Note that there are some explanatory texts on larger screens.

plurals
  1. POsimple data comparison with MySQL, nullpointerexception
    primarykey
    data
    text
    <p>I'm currently being thrown into the depths by my school and they are expecting me to program a simple login form using sql. They have given us brief examples on how they use JDBC and what it all is, but haven't really explained step by step how to use it on our own. Therefore i have snatched a bit of code from an example but i'm unable to get it working. I keep receiving an nullpointerexception and i can't figure out why :(</p> <p>Here's the connection class:</p> <pre><code>package Database; import java.sql.*; public class MySQLConnection { public static final String DRIVER = "com.mysql.jdbc.Driver"; public static final String DBURL = "jdbc:mysql://localhost/corendon"; public static final String DBUSER = "root"; public static final String DBPASS = "simplepass"; private ResultSet result = null; private int affectedRows = -1; Connection conn = null; public void startConnection() { try { Class.forName(DRIVER); DriverManager.setLoginTimeout(5); conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); } catch (Exception e) { } } public void closeConnection() { try { if (conn != null &amp;&amp; !conn.isClosed()) { conn.close(); } } catch (Exception e) { } conn = null; } public ResultSet performSelect(PreparedStatement prdstmt) throws SQLException { result = prdstmt.executeQuery(); return result; } public int performUpdate(PreparedStatement prdstmt) throws SQLException { affectedRows = prdstmt.executeUpdate(); return affectedRows; } public Connection getConnection() { return conn; } } </code></pre> <p>And here is the method i'm getting the exception in (in a different class):</p> <pre><code>MySQLConnection conn = new MySQLConnection(); public void compareData(int id, String pass) throws SQLException{ ResultSet rs = null; PreparedStatement prdstmt = null; String query = "SELECT id, password FROM users WHERE id=?, password=?"; conn.startConnection(); prdstmt = conn.getConnection().prepareStatement(query); prdstmt.setInt(1, id); prdstmt.setString(2, pass); rs = conn.performSelect(prdstmt); while (rs.next()){ String tempPass = rs.getString("password"); int tempId = rs.getInt("id"); } if(conn != null){ conn.closeConnection(); } } </code></pre> <p>I'm getting the nullpointerexception on line:</p> <pre><code>prdstmt = conn.getConnection().prepareStatement(query); </code></pre> <p>Why does it throw an exception there, but not when i start the connection and also how do i solve this? Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload