Jump to content

Recommended Posts

Posted

Boa tarde, estou a elaborar um login e surgiu-me este erro "java.lang.nullpointerexception", como o posso resolver?

O código é:

Classe login:

public class Login extends javax.swing.JFrame {

Connection conn = null;
ResultSet rset = null;
PreparedStatement pstmt = null;
private DaoHelper daoHelper;
/**
 * Creates new form Logi
 */
public Logi() throws SQLException {
	initComponents();
	DaoHelper daoHelper = new DaoHelper();

}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {										
	// TODO add your handling code here:
	String sql = "Select * from log where nome = ? and senha = ?";
	try{
	   conn = daoHelper.getConnectionFromContext();
	   pstmt = conn.prepareStatement(sql);
	   pstmt.setString(1, txtNome.getText());
	   pstmt.setString(2, txtPass.getText());

	   rset = pstmt.getGeneratedKeys();

	   if(rset.next()){
		   JOptionPane.showMessageDialog(null, "Entrou");
		   Main m = new Main();
		   m.setVisible(true);
	   }

	   else{
		   JOptionPane.showMessageDialog(null, "Dados ");
	   }

	}catch(Exception e){
		 JOptionPane.showMessageDialog(null, e);
	}

}										

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
	/* Set the Nimbus look and feel */
	//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
	/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
	 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
	 */
	try {
		for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
			if ("Nimbus".equals(info.getName())) {
				javax.swing.UIManager.setLookAndFeel(info.getClassName());
				break;
			}
		}
	} catch (ClassNotFoundException ex) {
		java.util.logging.Logger.getLogger(Logi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (InstantiationException ex) {
		java.util.logging.Logger.getLogger(Logi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (IllegalAccessException ex) {
		java.util.logging.Logger.getLogger(Logi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (javax.swing.UnsupportedLookAndFeelException ex) {
		java.util.logging.Logger.getLogger(Logi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	//</editor-fold>

	/* Create and display the form */
	java.awt.EventQueue.invokeLater(new Runnable() {
		public void run() {
			try {
				new Logi().setVisible(true);
			} catch (SQLException ex) {
				Logger.getLogger(Logi.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
	});
}

Classe de conexão a base de dados:

public class DaoHelper {

private static final ThreadLocal<Connection> context = new ThreadLocal<Connection>();


/**
* Fornece conex�o a BD
* @return
* @throws SQLException
*/

public Connection getConnection() throws SQLException{

Connection conn = null;

try {
Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ipss", "root", "");

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return conn;

}

public void beginTransaction() throws SQLException{

Connection conn = getConnection();
conn.setAutoCommit(false);
context.set(conn);
}

public void endTransaction() throws SQLException {
commit ( getConnectionFromContext() );
releaseTransaction();
}

public void releaseTransaction () throws SQLException {
Connection conn = getConnectionFromContext();
release(conn);
context.remove();
}

public void rollbackTransaction () {
Connection conn;
try {
conn = getConnectionFromContext();
rollback(conn);
release(conn);
} catch (SQLException e) {
e.printStackTrace();
}

context.remove();
}

public void commit (Connection conn) throws SQLException {
conn.commit();
}

public void rollback (Connection conn) throws SQLException{
if (conn != null) conn.rollback();
}


public Connection getConnectionFromContext() throws SQLException{
Connection conn = context.get();
if (conn == null) throw new SQLException("Transac��o invalida");
if (conn.isClosed()) throw new SQLException("Transac��o invalida, Conex�o fechada :S");

return conn;
}

public <T> void executeQuery (String query
 , QueryMapping<T> queryMapping) throws SQLException {

executeQuery(getConnection(), query, queryMapping);

}

public <T> void executeQuery (Connection conn, String query
  , QueryMapping<T> queryMapping) throws SQLException {
Statement stmt = null;
ResultSet rset = null;
try{
stmt = conn.createStatement();
rset = stmt.executeQuery(query);
queryMapping.mapping(rset);

} finally{
release(rset);
release(stmt);
}
}

public <T> void executePreparedQuery (String query
, QueryMapping<T> queryMapping
, Object... params) throws SQLException {

executePreparedQuery(getConnection(), query, queryMapping, params);
}

public <T> void executePreparedQuery (Connection conn
, String query
, QueryMapping<T> queryMapping
, Object... params) throws SQLException {

PreparedStatement pstmt = conn.prepareStatement (query);
ResultSet rset = null;

try{
pstmt = conn.prepareStatement (query);
populatePreparedStatement(pstmt, params);
rset = pstmt.executeQuery();
queryMapping.mapping(rset);

} finally{
release(rset);
release(pstmt);
}

}

public long executePreparedUpdateAndReturnGeneratedKeys (Connection conn, String query, Object... params) throws SQLException {

PreparedStatement pstmt = null;
ResultSet rset = null;
long result = 01;
try{
pstmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);

populatePreparedStatement(pstmt, params);

pstmt.executeUpdate();
rset = pstmt.getGeneratedKeys();

if(rset.next()) result = rset.getLong(1);
} finally{
release (pstmt);
release (rset);
}
return result;
}

public void populatePreparedStatement(PreparedStatement pstmt,
Object... params) throws SQLException {
int i = 0;
for (Object param : params){
pstmt.setObject( ++i,  param);
}
}

public long executePreparedUpdateAndReturnGeneratedKeys ( String query
, Object... params) throws SQLException {

return executePreparedUpdateAndReturnGeneratedKeys (getConnectionFromContext()
, query
, params);
}

public void executePreparedUpdate (String query, Object... params) throws SQLException {
executePreparedUpdate(getConnectionFromContext(), query, params);
}


public void executePreparedUpdate (Connection conn, String query, Object... params) throws SQLException {

PreparedStatement pstmt = null;
try{
pstmt = conn.prepareStatement(query);
populatePreparedStatement(pstmt, params);
pstmt.executeUpdate();
} finally{
release (pstmt);

}
}



public void release (Statement stmt){
if(stmt != null) return;
try {
stmt.close();
} catch (SQLException e) { }
}

public void release (Connection conn){
if(conn != null) return;
try {
conn.close();
} catch (SQLException e) { }
}

public void release (ResultSet rset){
if(rset != null) return;
try {
rset.close();
} catch (SQLException e) { }
}

public void releaseAll (Connection conn, Statement stmt){
release(stmt);
release(conn);
}

public void releaseAll (Connection conn, Statement stmt, ResultSet rset){
release(rset);
releaseAll(conn,stmt);
}


}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.