Я создаю эту форму входа в систему, используя java swing, и в основном я получаю значения из соответствующих JtextFields и отправляю их в другой класс.После этого я проверяю это с записями базы данных.Однако программа всегда отображает исключение NullPointerException каждый раз, когда я его запускаю.Я протестировал каждую переменную и увидел, что ни одна из них не содержит нулевых значений вообще.
Это первый метод (в классе LoginPage), который получает значения из JTextField
public void actionPerformed(ActionEvent e) {
String user=textField.getText();
String pass=String.valueOf(passwordField.getPassword());
//try{
//if((user!=null)&&(pass!=null)){
// System.out.println("user is not null");
ui.sendLogin(user,pass);
//}else{}
//}catch(NullPointerException ex){
//System.out.println("Entered catch block og view"+ex.getMessage());
//}
}
После этого он вызывает этот метод (в классе LoginController)
public void sendLogin(String user, String pass) {
try{
System.out.println("values from sendLogin"+user+"pass"+pass);
model.verifyUser(user,pass);
}catch(NullPointerException ex){
System.out.println("Entered catch block os sendLogin"+ex);
ex.printStackTrace();
}
}
Наконец, метод verifyUser (в классе LoginModel) проверяет его с базой данных
public void verifyUser(String user,String pass){
String username="";
String password="";
String acc="";
try{
String sql="SELECT * FROM users";
con1.getInstance();
Statement st = con1.getConn().createStatement();
ResultSet rs=st.executeQuery(sql);
rs.next();
username=rs.getString("username");
password=rs.getString("password");
acc=rs.getString("accountType");
if(username.equals(user)&&password.equals(pass)){
System.out.println("Logged into the system");
global_username=username;
accountType=acc;
}else{
System.out.println("Unsuccessful login"+username+user+password+pass);
}
st.execute(sql);
} catch (SQLException s){
System.out.println("SQL statement is not executed!"+s);
}
}
Thisтакое трассировка стека
java.lang.NullPointerException
at org.application.view.LoginController.sendLogin(LoginController.java:64)
at org.application.view.LoginPage$2.actionPerformed(LoginPage.java:94)
user is not null
values from sendLoginapassabc
Entered catch block os sendLoginjava.lang.NullPointerException
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$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$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)