Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 没有ItemListener可以正常工作,但当我添加它时,它会给我一个NullPointerException_Java_Multithreading_Swing_User Interface_Calendar - Fatal编程技术网

Java 没有ItemListener可以正常工作,但当我添加它时,它会给我一个NullPointerException

Java 没有ItemListener可以正常工作,但当我添加它时,它会给我一个NullPointerException,java,multithreading,swing,user-interface,calendar,Java,Multithreading,Swing,User Interface,Calendar,我为我的班级制作了一个简单的swing程序,根据ComboBox中选定的索引更改时区和其他一些内容。方法run()如下所示时工作正常: public void run() { while(true){ Calendar c = Calendar.getInstance(); int h = c.get(Calendar.HOUR); int m = c.get(Calendar.MINUTE);

我为我的班级制作了一个简单的swing程序,根据ComboBox中选定的索引更改时区和其他一些内容。方法run()如下所示时工作正常:

public void run() {
        while(true){
            Calendar c = Calendar.getInstance();
            int h = c.get(Calendar.HOUR);
            int m = c.get(Calendar.MINUTE);
            int s = c.get(Calendar.SECOND);
            l.setText(""+h+":"+m+":"+(s<10?"0"+s:s));
            try {
                t.sleep(1000);
            } catch (InterruptedException ex) {}
        }
    }
方法run()看起来像这样,但它不起作用。有什么想法吗

public void run() {
    while(true){

        p.cb.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                Calendar c = Calendar.getInstance();
                int h = c.get(Calendar.HOUR);
                int m = c.get(Calendar.MINUTE);
                int s = c.get(Calendar.SECOND);
                int index = p.cb.getSelectedIndex();

                if(index == 0){
                    l.setText(""+h+":"+m+":"+(s<10?"0"+s:s));
                }
                else if(index == 1){
                    l.setText(""+(h-6)+":"+m+":"+(s<10?"0"+s:s));
                }
                else if(index == 2){
                    l.setText(""+(h-1)+":"+m+":"+(s<10?"0"+s:s));
                }
                else if(index == 3){
                    l.setText(""+(h-6)+":"+m+":"+(s<10?"0"+s:s));
                }
                else if(index == 4){
                    l.setText(""+(h+8)+":"+m+":"+(s<10?"0"+s:s));
                }
            }

        });

        try {
            t.sleep(1000);
        } catch (InterruptedException ex) {}
    }
}
public void run(){
while(true){
p、 cb.addItemListener(新的ItemListener(){
@凌驾
公共无效itemStateChanged(ItemEvent e){
Calendar c=Calendar.getInstance();
int h=c.get(日历小时);
int m=c.get(日历分钟);
int s=c.get(日历秒);
int index=p.cb.getSelectedIndex();
如果(索引==0){

l、 setText(“+h+”:“+m+”:”+(sA
NullPointerException
被抛出,因为
p
cb
有一个
null
值。

你的线程现在没有意义了。它每秒钟向组合框添加一个ItemListener,但每个ItemListener都在做同样的事情。此外,你的线程正在修改EDT之外的gui元素。你可以ed将在EDT上添加ItemListener

我假设您正在尝试每秒钟更新一个标签,或者在组合框更改时更新一个标签

您应该在没有线程的情况下执行此操作,但应结合使用Swing计时器和ItemListener

// called on the EDT
void setup() {
     // create gui elements
     p.cb.addItemListener(new ItemListener() {
        updateTimeLabel();
     }
     new Timer(1000, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
           updateTimeLable()
        }
     });
 }

 private void updateTimeLabel() {
     Calendar c = Calendar.getInstance();
     int h = c.get(Calendar.HOUR);
     int m = c.get(Calendar.MINUTE);
     int s = c.get(Calendar.SECOND);
     int index = p.cb.getSelectedIndex();

     if(index == 0){
        l.setText(""+h+":"+m+":"+(s<10?"0"+s:s));
     }
     else if(index == 1){
         l.setText(""+(h-6)+":"+m+":"+(s<10?"0"+s:s));
     }
     else if(index == 2){
         l.setText(""+(h-1)+":"+m+":"+(s<10?"0"+s:s));
     }
     else if(index == 3){
          l.setText(""+(h-6)+":"+m+":"+(s<10?"0"+s:s));
     }
     else if(index == 4){
          l.setText(""+(h+8)+":"+m+":"+(s<10?"0"+s:s));
     }
 }
//在EDT上调用
无效设置(){
//创建gui元素
p、 cb.addItemListener(新的ItemListener(){
updateTimeLabel();
}
新计时器(1000,新ActionListener(){
已执行的公共无效操作(操作事件evt){
可更新的()
}
});
}
私有void updateTimeLabel(){
Calendar c=Calendar.getInstance();
int h=c.get(日历小时);
int m=c.get(日历分钟);
int s=c.get(日历秒);
int index=p.cb.getSelectedIndex();
如果(索引==0){
l、 setText(“+h+”:“+m+”:“+”(s
如果有人感到困惑,p是JFrame类的一个实例,而
cb是对JFrame类中ComboBox的引用


JFrame的实例?为什么要使用此方法?只需调用
ComboBox
变量并添加侦听器!
p
似乎是
NULL

在哪里初始化
ComboBox
?如果BloodShura不正确,请发布堆栈跟踪。顺便说一句:您每秒添加一个ItemListener。这不是很好d、 …谢谢你给我关于ItemListener的提示。我觉得自己像个白痴,但我要问,堆栈跟踪到底是什么?堆栈跟踪就是列表(实际上是:堆栈)在程序崩溃/停止之前所进行的方法调用的集合。当您得到NullPointerException时,它通常会写入更多的行。这是调用堆栈。这是cb所在的整个类。-线程所在的类。
// called on the EDT
void setup() {
     // create gui elements
     p.cb.addItemListener(new ItemListener() {
        updateTimeLabel();
     }
     new Timer(1000, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
           updateTimeLable()
        }
     });
 }

 private void updateTimeLabel() {
     Calendar c = Calendar.getInstance();
     int h = c.get(Calendar.HOUR);
     int m = c.get(Calendar.MINUTE);
     int s = c.get(Calendar.SECOND);
     int index = p.cb.getSelectedIndex();

     if(index == 0){
        l.setText(""+h+":"+m+":"+(s<10?"0"+s:s));
     }
     else if(index == 1){
         l.setText(""+(h-6)+":"+m+":"+(s<10?"0"+s:s));
     }
     else if(index == 2){
         l.setText(""+(h-1)+":"+m+":"+(s<10?"0"+s:s));
     }
     else if(index == 3){
          l.setText(""+(h-6)+":"+m+":"+(s<10?"0"+s:s));
     }
     else if(index == 4){
          l.setText(""+(h+8)+":"+m+":"+(s<10?"0"+s:s));
     }
 }