java.lang.NullPointerException将返回值从一个类文件传递到另一个类文件中的变量时

时间:2018-02-13 08:51:07

标签: java eclipse nullpointerexception return return-value

btnLogin.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent arg0) 
        {
            // validation of login panel
            if(txtUser.getText().equalsIgnoreCase("") && txtPassword.getText().equalsIgnoreCase(""))
            {
                //JOptionPane.showMessageDialog(null, "in");
                lblErrUser.setVisible(true);
                lblErrPassword.setVisible(true);
            }
            else if (txtUser.getText().equalsIgnoreCase("") || txtPassword.getText().equalsIgnoreCase(""))
            {
                if(txtUser.getText().equalsIgnoreCase(""))
                    lblErrUser.setVisible(true);
                else
                    lblErrPassword.setVisible(true);
            }
            else
            {
                // checking with database the credentials
                try {

                    System.out.println(txtUser.getText().toString() + " : " + txtPassword.getText().toString());
                    resultuser = qp.checkUser(txtUser.getText().toString(),txtPassword.getText().toString()); 
                    if(resultuser == 0)
                    {
                        JFrame newFrame = new homeScreen();
                        newFrame.setVisible(true);
                        newFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    }
                    else
                        JOptionPane.showMessageDialog(null, "Wrong Credentials !!!");

                } 
                catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("ERROR n login panel");
                }

            }
        }
    });

qp是类查询处理的对象,请参见代码:

    public class queryProcessing {

Statement stmt = null;
ResultSet rs = null;
Connection con = null;

public queryProcessing() {
    // TODO Auto-generated constructor stub
}

public int checkUser(String user, String pswd)
{
    System.out.print("In checking");
    String userdb = null;
    String pswddb = null;
    // TODO Auto-generated method stub
    String sql = ("SELECT * FROM login");
    try {
        stmt = (Statement) con.createStatement();
        rs = stmt.executeQuery(sql);

        while(rs.next()) 
        { 
            userdb = rs.getString("user"); 
            pswddb = rs.getString("password");
            System.out.print(userdb + " : " + pswddb);
            if(userdb.equalsIgnoreCase(user) && pswddb.equalsIgnoreCase(pswd))
                return 0;
            else
                return 1;
        }
    } 
    catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 1;
    }
    return 1;
}}     

错误:

a : a
java.lang.NullPointerException
ERROR n login panel
at loginPanel$2.actionPerformed(loginPanel.java:115)
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$300(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)     

花两天时间来解决此错误。 我只想获得下面一行的返回值,其中qp是其他类文件的对象,引用该方法将一些值返回给另一个类文件中的变量。 但无论我改变什么数据类型,我都会遇到错误。

请帮助???

resultuser = qp.checkUser(txtUser.getText()。toString(),txtPassword.getText()。toString());

1 个答案:

答案 0 :(得分:0)

我的第一个建议是打印qp以确保不为空。

System.out.println(txtUser.getText().toString() + " : " + txtPassword.getText().toString());
resultuser = qp.checkUser(txtUser.getText().toString(),txtPassword.getText().toString());

您的System.out.println();可确保用户和密码不为空。尝试将qp添加到system.out并检查结果