mercredi 14 octobre 2015

java.lang.NullPointerException. Can't connect to SQLite

I've create a GUI Program where the user can log in to their user account and password. But I have a problem in my codes here:

package fileMaintenanceProject;

import java.awt.EventQueue;
import java.sql.*;
import javax.swing.*;

import javax.swing.JFrame;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class logInClass {

private JFrame frmLogIn;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                logInClass window = new logInClass();
                window.frmLogIn.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Connection connect = null;
private JTextField usernameTF;
private JPasswordField passwordPF;

/**
 * Create the application.
 */
public logInClass() {
    initialize();
    connect = SQLiteFinal.dbConnector();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frmLogIn = new JFrame();
    frmLogIn.setResizable(false);
    frmLogIn.setAlwaysOnTop(true);
    frmLogIn.setTitle("Log In");
    frmLogIn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmLogIn.getContentPane().setLayout(null);
    frmLogIn.setSize(448,152);

    JLabel lblNewLabel = new JLabel("Username");
    lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblNewLabel.setBounds(50, 31, 66, 14);
    frmLogIn.getContentPane().add(lblNewLabel);

    JLabel lblPassword = new JLabel("Password");
    lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblPassword.setBounds(50, 56, 58, 30);
    frmLogIn.getContentPane().add(lblPassword);

    usernameTF = new JTextField();
    usernameTF.setBounds(126, 30, 129, 20);
    frmLogIn.getContentPane().add(usernameTF);
    usernameTF.setColumns(10);

    passwordPF = new JPasswordField();
    passwordPF.setBounds(126, 63, 129, 20);
    frmLogIn.getContentPane().add(passwordPF);

    JButton btnLogIn = new JButton("Log In");
    btnLogIn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try{
                String query = "select * from EmployeeInfo where username = ? and password = ?";
                PreparedStatement pst = connect.prepareStatement(query);
                pst.setString(1, usernameTF.getText());
                pst.setString(2, passwordPF.getText());

                ResultSet rs = pst.executeQuery();

                int count = 0;

                while(rs.next()){
                    count++;
                }

                if(count == 1){
                    JOptionPane.showMessageDialog(null, "Username and Password is correct");

                }
                else if(count > 1){
                    JOptionPane.showMessageDialog(null, "Username and Password is duplicated");
                }
                else{
                    JOptionPane.showMessageDialog(null, "Incorrect Username or Password");
                }
                rs.close();
                pst.close();
            }catch(Exception ex){
                JOptionPane.showMessageDialog(null, ex);
            }

        }
    });
    btnLogIn.setBounds(265, 29, 89, 23);
    frmLogIn.getContentPane().add(btnLogIn);

    JButton btnCancel = new JButton("Cancel");
    btnCancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
        }
    });
    btnCancel.setBounds(265, 62, 89, 23);
    frmLogIn.getContentPane().add(btnCancel);
}

How can I execute this program in order that the user can now access the program?

Aucun commentaire:

Enregistrer un commentaire