0

I am writing a desktop application in Java and I am using a SQLite database. I am fetching data from SQLite studio, and it is fetched but when I get it in the application I'm getting a null pointer.

Below is the database manager class where the database is connected and functions related to db are called:

DB Class:

public class DBManager {
Connection con=null;//build connection
Statement stmt=null;//execute query
static PreparedStatement pst= null;
public DBManager(){
    try {
        Class.forName("org.sqlite.JDBC");
        String url="jdbc:sqlite:mobileDB.db";
        con=DriverManager.getConnection(url);
        stmt=con.createStatement();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}
public void insertUpdateDelete(String query){
    try{
        stmt.execute(query);
    }catch(Exception ex){
        System.out.print(ex.toString());
    }
}
public ResultSet select(String query){
    try{
        ResultSet rs= stmt.executeQuery(query);
        return rs;
    }catch(Exception ex){
        ex.printStackTrace();
        return null;
    }
}
public ResultSet getLogInRecord(String user,String pass, String role) throws SQLException {
    String query="select * from Users where Username=? and Password=? and Role=?";
    pst.setString(0, user);
    pst.setString(1, pass);
    pst.setString(2, role);
    pst=con.prepareStatement(query);
    ResultSet rs=pst.executeQuery();
    return rs;

}

Function Calling:

btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                String username=txtUsername.getText(),password=txtPass.getText(),role=cmbRole.getSelectedItem().toString();
                if(username.equals("")||password.equals("")||role.equals("Select")) {
                    JOptionPane.showMessageDialog(frame, "Please input all the data. No empty data allowed.","Empty Fields",JOptionPane.ERROR_MESSAGE);
                }else {
                    DBManager dbm= new DBManager();
                    //ResultSet rs= dbm.getLogInRecord(username, password, role);
                    ResultSet rs=dbm.select("select * from Users where Username='admin' and Password='admin' and Role='Admin'");
                    if(rs.next()) {
                        JOptionPane.showMessageDialog(frame, "True","Error",JOptionPane.ERROR_MESSAGE);
                    }else {
                        JOptionPane.showMessageDialog(frame, "False","Error",JOptionPane.ERROR_MESSAGE);
                    }
                }
            }catch (Exception e) {
                JOptionPane.showMessageDialog(frame, e,"Error",JOptionPane.ERROR_MESSAGE);
            }
        }
    });
Venantius
  • 2,471
  • 2
  • 28
  • 36
  • 1
    Attaching stack-trace would help here. – MIK Oct 04 '18 at 17:45
  • We'll need to see the error that you're getting to be able to offer any help. – mypetlion Oct 04 '18 at 17:45
  • 2
    Proabably here "pst=con.prepareStatement(query);", you are using pst before initializing, first call pst=con.prepareStatement(query); after that call pst.setString(0, user); – MIK Oct 04 '18 at 17:49

1 Answers1

0

pst.setString(0, user) is the line that is causing the null pointer.

pst=con.prepareStatement(query) has to come before it because it has never been initialized.

Sooooo

public ResultSet getLogInRecord(String user,String pass, String role) throws SQLException {
   String query="select * from Users where Username=? and Password=? and Role=?";
   pst=con.prepareStatement(query);
   pst.setString(0, user);
   pst.setString(1, pass);
   pst.setString(2, role);
   ResultSet rs=pst.executeQuery();
   return rs;
}
RobOhRob
  • 585
  • 7
  • 17