我在 JLabel 中收到空异常错误

I get null exception error in JLabel

我创建了一个登录表单和一个时钟,我在 jlabel 上将时钟设置为 运行,当我尝试 运行 程序时,它给我一个错误提示, 线程异常 "Thread-1" java.lang.NullPointerException 在 project.jf_login_form$2.run(jf_login_form.java:125)

你先调用call()然后JL_clock = new JLabel("hello");所以你在初始化对象之前先调用JL_clock.setText("Time "+hour+":"+minuite+":"+second+" Date "+day+"/"+month+"/"+year);

可能的解决方案:

clock();

JL_username = new JLabel("Username:");
JL_pass = new JLabel("Password:");
JL_clock = new JLabel("hello");

应替换为

JL_username = new JLabel("Username:");
JL_pass = new JLabel("Password:");
JL_clock = new JLabel("hello");
clock();