MySQL データベースのユーザー名に関連付けられたユーザー アクセス権限を取得しようとしています。ただし、Null Pointer Exception が発生します。理由を知っている人はいますか?メソッドに関連付けられたクラスの一部を添付しました。
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;
}
誰でも私を助けることができます、私はこれに何日も立ち往生しています!!
PS SQL ステートメントが素晴らしくなく、SQL インジェクション攻撃を受けやすいことはわかっていますが (そう言われました)、それはここでの問題ではありません!