0

I'm trying to retrieve the user access privileges associated with a username in a MySQL database. However I'm getting a Null Pointer Exception. Anyone know why? I've attached the parts of the classes associated with the method.

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;
}

Can anyone help me, I've been stuck on this for days!!

P.S. I know my SQL statements aren't brilliant and are subject to SQL injection attacks (So I've been told) but thats not the issue here!

4
  • 2
    You've provided huge amounts of code - and haven't told us where the exception is occurring. That makes it really hard to help you. Commented Jul 6, 2013 at 12:10
  • 2
    Seriously? You throw us all your code, but not the exception that gives line numbers and a description of the problem... Commented Jul 6, 2013 at 12:10
  • Sorry new at this and getting very stressed!! list = model.getPositions(user); Is where the Null Pointer Points too Commented Jul 6, 2013 at 12:16
  • Since user is initialized before with String user = new String(getUsername()); I believe that your model has not been initialized. Commented Jul 6, 2013 at 12:21

1 Answer 1

2

You need to initialize your

JIOSModel model;

Something like this:

model = new JIOSModel();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.