我希望你能解决我的一个小问题。我的程序是一个倒数计时器程序,它是秒。所以我的问题是由于某种原因我无法使用从数据库中检索到的int值。所以在我的程序中,它将使用数据库中的int值填充一个JLabel,该值将是我的计时器的起始点,但倒数计时器将在我启动它时不会启动并且会给我一个错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at myproject2016.NLogin$event.actionPerformed(NLogin.java:202)
这是我的代码(请原谅我的编码,我是新手=)谢谢!):
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();
}
}
}
}