0

I hope you can help with a little problem of mine. My program is a countdown timer program, it's by seconds. So my problem is for some reason I can't use the int value that I retrieved from my database. So in my program, it will populate one JLabel with the int value from the database and that value will be the starting point of my timer, but the countdown timer won't start when I start it and will give me an error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at myproject2016.NLogin$event.actionPerformed(NLogin.java:202)

Here is my code(Pardon me with my coding, I'm a newbie =) thanks!):

  package myproject2016;

import java.awt.EventQueue;

public class NLogin {

    JLabel promptLabel, timerLabel;
    int counter;
    JLabel tf;
    JButton button;
    Timer timer;
    private long startedAt;
    private JFrame frame;
    private JPanel contentPane;
    private JTextField txtUser;
    private JPasswordField txtPass;
    DataBase db = new DataBase();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    NLogin window = new NLogin();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public NLogin() {
        initialize();
    }


    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setBackground(new Color(0, 102, 102));
        frame.setBounds(100, 100, 450, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("Computer Laboratory Usage Login");
        lblNewLabel.setFont(new Font("Trajan Pro", Font.BOLD, 15));
        lblNewLabel.setBounds(66, 11, 320, 65);
        frame.getContentPane().add(lblNewLabel);

        JLabel lblUsername = new JLabel("Username:");
        lblUsername.setFont(new Font("Arial", Font.BOLD, 12));
        lblUsername.setBounds(107, 87, 62, 27);
        frame.getContentPane().add(lblUsername);

        JLabel lblPassword = new JLabel("Password:");
        lblPassword.setFont(new Font("Arial", Font.BOLD, 12));
        lblPassword.setBounds(107, 131, 62, 27);
        frame.getContentPane().add(lblPassword);

        txtUser = new JTextField();
        txtUser.setBounds(192, 91, 129, 20);
        frame.getContentPane().add(txtUser);
        txtUser.setColumns(10);

        final JPasswordField txtPass = new JPasswordField();
        txtPass.setColumns(10);
        txtPass.setEchoChar('*');
        txtPass.setBounds(192, 135, 129, 20);
        frame.getContentPane().add(txtPass);

        final JLabel timerLabel = new JLabel("",SwingConstants.CENTER);
        timerLabel.setForeground(Color.WHITE);
        timerLabel.setBackground(Color.BLACK);
        timerLabel.setFont(new Font("Tahoma", Font.BOLD, 25));
        timerLabel.setBounds(66, 402, 302, 111);
        frame.getContentPane().add(timerLabel);

            final JLabel tf = new JLabel("");
            tf.setFont(new Font("Tahoma", Font.BOLD, 12));
            tf.setBounds(231, 361, 78, 27);
            frame.getContentPane().add(tf);

        JButton btnNewButton = new JButton("LOGIN");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {


                try {

                    String uName = txtUser.getText();
                    String pWord = txtPass.getText();

                    db.connect();
                    db.rs = db.st.executeQuery("select * from user where Username='"+uName+"' and Password='"+pWord+"'");
                    int count =0;
                    while(db.rs.next()){
                        count = count +1;
                        Login l = new Login();
                        JOptionPane.showMessageDialog(null,"Login successful");


                    }
                    if(count==1){


                         db.connect();
                         db.rs = db.st.executeQuery("select * from user where T_rem");
                        while(db.rs.next()){
                            tf.setText(String.valueOf(db.rs.getInt("T_rem")));

                        }


                    }
                    else{
                        JOptionPane.showMessageDialog(null,"User does not exist.");
                    }
                } catch (SQLException c) {

                    c.printStackTrace();
                }

            }
        });
        btnNewButton.setIcon(new ImageIcon("C:\\Users\\Carl\\Documents\\Icons\\check-icon.png"));
        btnNewButton.setFont(new Font("Stencil", Font.PLAIN, 18));
        btnNewButton.setBounds(66, 192, 123, 38);
        frame.getContentPane().add(btnNewButton);

        JLabel promptLabel = new JLabel("Time Remaining:");
        promptLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
        promptLabel.setBounds(88, 361, 144, 27);
        frame.getContentPane().add(promptLabel);


        JButton btnLogout = new JButton("LOGOUT");
        btnLogout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                 String sbutt = e.getActionCommand();

                 if(sbutt.equals("LOGOUT")) {

                        timer.stop();

                        Toolkit.getDefaultToolkit().beep();

                 }
            }
        });

        btnLogout.setFont(new Font("Stencil", Font.BOLD, 16));
        btnLogout.setBounds(257, 191, 123, 38);
        frame.getContentPane().add(btnLogout);


        JButton btnNewButton_1 = new JButton("Start");
        btnNewButton_1.setFont(new Font("Stencil", Font.PLAIN, 11));
        btnNewButton_1.setBounds(174, 300, 89, 23);
        frame.getContentPane().add(btnNewButton_1);

          event e =  new event();
          btnNewButton_1.addActionListener(e);

    }

         public void actionPerformed(ActionEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
            public void run() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
            public class event implements ActionListener {
                public void actionPerformed(ActionEvent e) {
                    int count = (int)(Double.parseDouble(tf.getText()));
                    timerLabel.setText("Time left: " + count);
                    TimeClass tc = new TimeClass(count);
                    timer = new Timer(1000, tc);
                    timer.start();
                }
            }

            public class TimeClass implements ActionListener {
                int counter;
                public TimeClass(int counter) {
                    this.counter= counter;
           }
                public void actionPerformed(ActionEvent tc) {
                    counter--;
                    if(counter >= 1) {
                        timerLabel.setText("Time left: " + counter);
                    }
                    else {
                        timer.stop();
                        timerLabel.setText("Done!");
                        Toolkit.getDefaultToolkit().beep();
                    }
                }
            }
        }
Carsh CaNe
  • 39
  • 7
  • Hey Jim, This is different. The countdown timer won't start, I believe it's not just the error. – Carsh CaNe Mar 02 '16 at 09:08
  • You get a `NullPointerException` in `event.actionPerformed`, so it never reaches the statement starts the timer. Debug your code and check what is null (likely `tf` or `timerLabel`) – Mark Rotteveel Mar 02 '16 at 10:29
  • Hi Mark, You are right, it's actually this line "timerLabel.setText("Time left: " + counter);" , i tried to remove it , but the time does not start. – Carsh CaNe Mar 02 '16 at 11:07

0 Answers0