空指针异常何时不应该?

时间:2013-07-06 12:08:28

标签: java mysql database null nullpointerexception

我正在尝试检索与MySQL数据库中的用户名关联的用户访问权限。但是我得到了一个空指针异常。谁知道为什么?我附上了与方法相关的类的部分。

JIOS_Login_Controller.java:

public class JIOS_Login_Controller extends javax.swing.JFrame {

static private JIOSModel model;
private JIOS_Login_Controller home;
static private String username;
static private String password;
static private String a;


public JIOS_Login_Controller() {
    initComponents();
    username = null;
    password = null;

}

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String sql = "Select * From user_Tbl";
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/abnd251?user=####&password=########");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(sql);

        String user = jTextField1.getText();
        String pwd = new String(jPasswordField1.getPassword());
        while (rs.next()) {
            String uname = rs.getString("Username");
            String pword = rs.getString("Password");
            if ((user.equals(uname)) && (pwd.equals(pword))) {
                java.awt.EventQueue.invokeLater(new Runnable() {

                    @Override
                    public void run() {

                        setUsername();
                        String user = new String(getUsername());

                        ArrayList<UPosition> list = null;
                        try {
                          list = model.getPositions(user);
                        } catch (SQLException ex) {
                            Logger.getLogger(JIOS_Login_Controller.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        for (UPosition p : list) {
                            a = p.getPosition();
                        }

                        new JIOS_Home_Controller().setVisible(true);
                        dispose();
                    }
                });
            } else {
                if ((user == null ? (uname) != null : !user.equals(uname)) && (pwd.equals(pword))) {
                    JOptionPane.showMessageDialog(this, "Incorrect Username");
                } else {
                    if ((user.equals(uname)) && (pwd == null ? (pword) != null : !pwd.equals(pword))) {
                        JOptionPane.showMessageDialog(this, "Incorrect Password");
                    } else {
                        if ((user == null ? (uname) != null : !user.equals(uname)) && (pwd == null ? (pword) != null : !pwd.equals(pword))) {
                            JOptionPane.showMessageDialog(this, "Incorrect Username and Password");
                        }
                    }
                }
            }
        }
    } catch (ClassNotFoundException | SQLException | HeadlessException e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
}  

static public String getPosition(){
    return a;
} 

JIOS_Home_Controller.java:

public class JIOS_Home_Controller extends javax.swing.JFrame {

private JIOSModel model;
private JIOS_Home_Controller home;
static private String username;
static private String password;
static private String access;
private String position;


public JIOS_Home_Controller()  {
    initComponents();
    user.setText(""+ JIOS_Login_Controller.getUsername() +"");
    userPosition.setText(""+ JIOS_Login_Controller.getPosition() +"");

}

JIOSModel.java:

public ArrayList<UPosition> getPositions(String user) throws SQLException {
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    ArrayList<UPosition> list = new ArrayList<>();

    try {
        con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/abnd251?user=####&password=########");
        stmt = con.prepareStatement("SELECT Position FROM user_tbl WHERE Username=  '"+ user +"' ");
        rs = stmt.executeQuery();
        while (rs.next()) {
            UPosition p = new UPosition();
            p.setPosition(rs.getString("Position"));
            list.add(p);
        }
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException se) {
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException se) {
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException se) {
            }
        }
    }

    return list;
}

任何人都可以帮助我,我已经坚持了几天!!

P.S。我知道我的SQL语句并不精彩,并且受到SQL注入攻击(所以我被告知)但这不是问题!

1 个答案:

答案 0 :(得分:2)

您需要初始化

JIOSModel model;

这样的事情:

model = new JIOSModel();