Problème avec actionPerformed() unknown source
Bonjour,
J'ai une interface d'authentification, et je dois effectuer la vérification du login et mot de passe tapé en passant par la base de données, j'ai donc fais ce code mais lorsque j'exécute la classe, j'obtient une erreur lorsque je clique sur le bouton "login", de meme en cliquant sur "cancel".
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| package workflow.interfaces;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class UserLogin extends JFrame {
private Statement statement;
private ResultSet rs;
private JTextArea errorText;
private String passVerif;
private int id_entreprise;
JButton login, cancel;
JTextField uname;
JPasswordField pass;
JLabel u,p;
public UserLogin(){
setTitle("Login");
setLayout(new GridLayout(3,2));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
u = new JLabel("Username");
p = new JLabel("Password");
uname = new JTextField(20);
pass = new JPasswordField(20);
login = new JButton("Login");
cancel = new JButton("Cancel");
add(u);
add(uname);
add(p);
add(pass);
add(login);
add(cancel);
uname.requestFocus();
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String un=uname.getText();
String pa= new String(pass.getPassword());
connectToDB();
try {
rs = statement.executeQuery("SELECT * FROM entreprise WHERE login = " + un);
/* si le login existe dans la base de données
alors récuperer le password et vérifier sa correspondance */
while (rs.next()){
id_entreprise = rs.getInt("id_entreprise");
// String nom_entreprise = rs.getString("nom_entr");
// String login = rs.getString("login");
passVerif = rs.getString("passe");
int row = rs.getRow();
System.out.println("Données contenues dans la ligne "+row);
System.out.println("id entreprise : "+id_entreprise);
if ((pa.equals(passVerif))){
dispose();
new WelcomeFrame();
}
else errorText.append("Veuillez resaisir votre mot de passe");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
KeyAdapter k = new KeyAdapter(){
public void KeyPressed(KeyEvent ke){
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
login.doClick();
}
};
pass.addKeyListener(k);
uname.addKeyListener(k);
pack();
setLocationRelativeTo(null);
}
public void connectToDB() {
try {
Connection connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/destockage", "xxx", "xxxx");
Statement statement = connection.createStatement();
} catch (SQLException connectException) {
System.out.println(connectException.getMessage());
System.out.println(connectException.getSQLState());
System.out.println(connectException.getErrorCode());
System.exit(1);
}
}
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
System.err.println("Unable to find and load driver");
System.exit(1);
}
new UserLogin();
}
} |
Erreur:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| connexion bd établie
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at workflow.interfaces.UserLogin$1.actionPerformed(UserLogin.java:71)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source) |
Que pourra être le problème?
Merci pour votre aide