0

I would like to hide the UserSecurity menu point from users who have employee value in the database field. My code is:

    String employee = "employee";
    String SQL_USERSECURITY = "SELECT UserSecurity FROM user WHERE (UserSecurity = ?);";
    String userSecurity = null;
    try {
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/worker","admin","");
        PreparedStatement stat = conn.prepareStatement(SQL_USERSECURITY);
        stat.setString(1, userSecurity);

        ResultSet rs = stat.executeQuery();
        while ( rs.next() ) {
            userSecurity = rs.getString("UserSecurity");
        }
        conn.close();
    } catch (Exception e) {
        System.err.println("Got an exception! ");
        System.err.println(e.getMessage());
    }
    
    JMenuItem Company = new JMenuItem(Labels.LIST_COMPANIES);
    if (userSecurity.equals(employee)) {
        Company.setVisible(false);
    } else {
        Company.setVisible(true);
    }

May I ask your kind support to find out what can be the reason for the error?

Tibson
  • 1
  • 1
  • 1
    Your `NullPointerException` has nothing to do with the code you posted. – f1sh Oct 15 '20 at 23:28
  • 1
    Your database interaction makes no sense and has no effect at all. You select all users that have `UserSecurity` = `null`. Then you iterate over all results and do `userSecurity = rs.getString("UserSecurity");`, which is, of course, `null`, just like the initial value of `userSecurity`. – f1sh Oct 15 '20 at 23:30
  • If you do not get any rows from the DB, then `userSecurity` will remain as `null` – Scary Wombat Oct 16 '20 at 00:25

0 Answers0