estou fazendo uma tela de login, quando o usuário max loga, ele apresenta a tela principal. Mas quando qualquer outro usuário tenta logar aparece “java.lang.NULLPointerException”. Lembrando estou começando a programar.
segue codigo:
package br.com.sisclube.telas;
import java.sql.*;
import br.com.sisclube.dal.ModuloConexao;
import javax.swing.JOptionPane;
public class TelaLogin extends javax.swing.JFrame {
Connection conexao = null;
PreparedStatement pst = null;
ResultSet rs = null;
//metodo logar
public void logar() {
String sql = "select * from Usuarios where Nome=? and Senha=?";
try {
pst = conexao.prepareStatement(sql);
pst.setString(1, txtUsuario.getText());
pst.setString(2, txtSenha.getText());
rs = pst.executeQuery();
if (rs.next()) {
String Ativo = rs.getString(6);
if (Ativo.equals("V")) {
TelaPrincipal principal = new TelaPrincipal();
principal.setVisible(true);
this.dispose();
conexao.close();
} else {
JOptionPane.showMessageDialog(null, "Usuário inativo");
this.dispose();
conexao.close();
}
} else {
JOptionPane.showMessageDialog(null, "Usuário e/ou senha inválido");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
public TelaLogin() {
initComponents();
conexao = ModuloConexao.conector();
if (conexao != null) {
lblStatus.setText("Conectado");
} else {
lblStatus.setText("Desconectado");
}
}
@SuppressWarnings("unchecked")
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtUsuario = new javax.swing.JTextField();
txtSenha = new javax.swing.JPasswordField();
lblStatus = new javax.swing.JLabel();
btnLogar = new javax.swing.JButton();
lblfundo = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Login");
getContentPane().setLayout(null);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel1.setText("Login");
getContentPane().add(jLabel1);
jLabel1.setBounds(60, 70, 50, 17);
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel2.setText("Senha");
getContentPane().add(jLabel2);
jLabel2.setBounds(60, 140, 43, 17);
getContentPane().add(txtUsuario);
txtUsuario.setBounds(130, 60, 160, 30);
getContentPane().add(txtSenha);
txtSenha.setBounds(130, 130, 160, 30);
lblStatus.setText("...");
getContentPane().add(lblStatus);
lblStatus.setBounds(20, 280, 60, 14);
btnLogar.setText("ENTRAR");
btnLogar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLogarActionPerformed(evt);
}
});
getContentPane().add(btnLogar);
btnLogar.setBounds(200, 180, 90, 30);
lblfundo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/sisclube/icones/fund_login.png"))); // NOI18N
getContentPane().add(lblfundo);
lblfundo.setBounds(0, 0, 400, 300);
setSize(new java.awt.Dimension(416, 339));
setLocationRelativeTo(null);
}// </editor-fold>
private void btnLogarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
logar();
}
public static void main(String args[]) {
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(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TelaLogin.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() {
new TelaLogin().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnLogar;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel lblStatus;
private javax.swing.JLabel lblfundo;
private javax.swing.JPasswordField txtSenha;
private javax.swing.JTextField txtUsuario;
}
está fora da sequência.