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

时间:2011-08-18 07:44:44

标签: java compiler-errors nullpointerexception initialization

我正在使用java swing创建这个登录表单,我基本上做的是从相关的JtextFields获取值并将它们发送到另一个类。之后我用数据库记录检查它。但是,每次运行程序时,程序始终显示NullPointerException。我测试了每个变量,看到它们都没有包含任何Null值。

这是从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)

4 个答案:

答案 0 :(得分:5)

测量,不要猜测。

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

答案 1 :(得分:2)

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

答案 2 :(得分:1)

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

的原因

此行是model.verifyUser(user,pass);

答案 3 :(得分:1)

我打赌model变量为null