dimanche 17 avril 2016

I am getting with the following Code the java.lang.NullPointerException Error , what do I do ? [duplicate]

This question already has an answer here:

I am creating a log in page with java window builder and sqlite database but i am having a little bit of a trouble. I am getting java.lang.NullPointerExcpetion error after clicking Log in and I cant find the issue why it does. This is my code for my page.

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;
import javax.swing.*;


public class Admin {

    private JFrame frame;
    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void AdminScreen()
     {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Admin window = new Admin();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    Connection connection=null;


    /**
     * Create the application.
     */
    public Admin() {
        initialize();
        connection=sqliteConnection.dbConnector();

    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setBackground(new Color(240, 255, 255));
        frame.setBounds(100, 100, 957, 528);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        Image img = new ImageIcon(this.getClass().getResource("/hands.png")).getImage();

        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setIcon(new ImageIcon(img));
        lblNewLabel.setBounds(320, 217, 264, 272);
        frame.getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("Username\r\n");
        lblNewLabel_1.setFont(new Font("Times New Roman", Font.PLAIN, 20));
        lblNewLabel_1.setBounds(271, 82, 84, 24);
        frame.getContentPane().add(lblNewLabel_1);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setFont(new Font("Times New Roman", Font.PLAIN, 20));
        lblPassword.setBounds(271, 130, 84, 24);
        frame.getContentPane().add(lblPassword);

        textField = new JTextField();
        textField.setBounds(378, 87, 206, 20);
        frame.getContentPane().add(textField);
        textField.setColumns(10);


        passwordField = new JPasswordField();
        passwordField.setBounds(379, 135, 205, 20);
        frame.getContentPane().add(passwordField);

        JButton btnNewButton = new JButton("Log In\r\n");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {


                try {
                    String query="select * from Login where username=? and password=? ";
                    PreparedStatement pst=connection.prepareStatement(query);

                    pst.setString(1,textField.getText() );
                    pst.setString(2,passwordField.getText() );

                    ResultSet rs=pst.executeQuery();
                    int count =0;
                    while(rs.next()){
                        count=count+1;

                    }
                    if(count ==1)
                    {
                        JOptionPane.showMessageDialog(null, "Username and password is correct");
                    }
                    else if(count>1)
                    {
                        JOptionPane.showConfirmDialog(null,"Duplicate Usernme and password");
                    }
                    else {
                        JOptionPane.showConfirmDialog(null,"Username and password is incorrect");

                    }

                    rs.close();
                    pst.close();
                }catch(Exception e)
                {
                    JOptionPane.showMessageDialog(null, e);
                }

            }
        });
        btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 20));
        btnNewButton.setBounds(378, 191, 167, 33);
        frame.getContentPane().add(btnNewButton);

    }
}

I hope someone can help me out as i have tried to figure the problem out for hours and i am just stuck. Let me know if posting the database connection code would help out more.

Aucun commentaire:

Enregistrer un commentaire