当没有未初始化的变量时,Java NullPointerException

发布于 2024-11-30 13:36:39 字数 4677 浏览 0 评论 0原文

我正在使用 java swing 创建此登录表单,我基本上所做的就是从相关的 JtextFields 获取值并将它们发送到另一个类。然后我用数据库记录检查它。但是,每次运行该程序时,它总是显示 NullPointerException。我测试了每个变量,发现它们都不包含任何空值。

这是从 JTextField 获取值的第一个方法(在 LoginPage 类中)

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);
        }

}

这是堆栈跟踪

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)

I am creating this login form using java swing and what I basically do is I get the values from the relevant JtextFields and send them onto another class. Thereafter I check it with the database records. However the program is always displaying a NullPointerException everytime I run it. I tested every variable and saw that none of them contain any Null Values at all.

This is the first method (in the LoginPage class) that gets the values from the 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());
//}

}

Thereafter it calls this method (in class 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();
}
}

Finally the verifyUser method (in class LoginModel) checks it with the database

 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 is the Stack Trace

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尹雨沫 2024-12-07 13:36:39

测量,不要猜测。

为什么要猜测哪个变量为空?堆栈跟踪为您提供异常发生的行号。启动调试器并在 LoginController 类的第 64 行设置断点。

Measure, don't guess.

Why guess which variable is null? The stack trace gives you the line number where the exception happens. Fire up the debugger and set a breakpoint on line 64 of the LoginController class.

哎呦我呸! 2024-12-07 13:36:39

我猜测 model 变量为 null 并导致 NullPointerException

I'm guessing that the model variable is null and causing the NullPointerException.

蒲公英的约定 2024-12-07 13:36:39

我相信您的 model 变量为空。
这就是导致 NullPointerException 的原因。

第 64 行是 model.verifyUser(user,pass);

I believe your model variable is null.
That is what is causing your NullPointerException.

Is this line 64 model.verifyUser(user,pass);

一个人的旅程 2024-12-07 13:36:39

我敢打赌 model 变量是 null

I bet the model variable is null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文