Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql connect perfectly first time, second time return a nullpointer
    primarykey
    data
    text
    <p>This is my connection Class/Method:</p> <pre><code>static Connection connection = null; Statement stm = null; public static Connection getConexao() { try { // Carregando o JDBC Driver padrão String driverName = "com.mysql.jdbc.Driver"; Class.forName(driverName); // Configurando a nossa conexão com um banco de dados// String serverName = "192.168.0.222"; //caminho do servidor do BD String mydatabase ="bd"; //nome do seu banco de dados String url = "jdbc:mysql://" + serverName + "/" + mydatabase; String username = "user"; //nome de um usuário de seu BD String password = "pass"; //sua senha de acesso connection = DriverManager.getConnection(url, username, password); return connection; } catch (ClassNotFoundException e) { //Driver não encontrado System.out.println("O driver expecificado nao foi encontrado."); return null; } catch (SQLException e) { //Não conseguindo se conectar ao banco System.out.println("Nao foi possivel conectar ao Banco de Dados."); return null; } } </code></pre> <p>method that are called when the TextField lost focus :</p> <pre><code>private void puxaemailsiscart(String servico) throws SQLException { Connection conn = ConectaBD.getConexao(); Statement stm = null; ResultSet results = null; stm = conn.createStatement (); try { System.out.println(); results = stm.executeQuery ("SELECT * FROM " + servico + " WHERE protocolo =" + tfProtocolo.getText()); if(results.next()) { tfEmailParte.setText(results.getString("Email").toString()); } else { System.out.println("Protocolo nao encontrado"); } results.close(); stm.close(); conn.close(); .... </code></pre> <p>The problem is, the first time the TextField lost the focus, it works PERFECTLY and return the Database result, but when i put the focus in the textfield and it lost focus again, the connection return null, why? since the steps are the same as the first time??</p> <p>STACKTRACE</p> <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at telaprincipal$3.puxaemailsiscart(telaprincipal.java:499) at telaprincipal$3.focusLost(telaprincipal.java:482) </code></pre> <p>Line 499: = <code>Connection conn = ConectaBD.getConexao();</code> says that here is the NullPoint, but Why? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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