【问题标题】:Works fine without ItemListener, but when I add it, it gives me a NullPointerException没有 ItemListener 也能正常工作,但是当我添加它时,它给了我一个 NullPointerException
【发布时间】:2013-02-20 03:01:44
【问题描述】:

我为我的班级制作了一个简单的摇摆程序,它可以根据 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) {}
        }
    }

但是,当我尝试重新定义它并使时间更改起作用时,我在这一行中得到一个空指针异常:

p.cb.addItemListener(new ItemListener() {

方法 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) {}
    }
}

如果有人感到困惑,p 是 JFrame 类的一个实例,而 cb 是对 JFrame 类中 ComboBox 的引用。

【问题讨论】:

  • 你在哪里初始化ComboBox
  • 发布堆栈跟踪,以防 BloodShura 不正确。顺便说一句:您每秒添加一个 ItemListener 。那不是那么好......
  • 感谢有关 ItemListener 的提示。我觉得自己像个白痴,但我想问一下,堆栈跟踪到底是什么?
  • 堆栈跟踪是在程序崩溃/停止之前进行的方法调用的列表(实际上是:堆栈)。当你得到 NullPointerException 时,它通常会写更多的行。这就是调用堆栈。
  • pastebin.com/W3tg35fq 这就是cb所在的整个班级。 pastebin.com/KXwNdTKb - 线程所在的类。

标签: java multithreading swing user-interface calendar


【解决方案1】:

NullPointerException 被抛出,因为 pcb 具有 null 值。

【讨论】:

    【解决方案2】:

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

    我假设您尝试做的是每秒钟更新一次标签或在组合框更改时更新。

    您应该在没有线程的情况下执行此操作,而是使用 Swing Timer 和 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 元素。如果你删除你的线程,除了第一个之外,所有这些条件都会消失。然后很容易确保在调用 addItemListener 方法之前初始化 p.cb。

    此外,您应该使用比 p、cb、l 更具描述性的名称。

    【讨论】:

      【解决方案3】:

      如果有人感到困惑,p 是 JFrame 类的一个实例,而 cb 是对 JFrame 类中的 ComboBox 的引用。

      JFrame 的实例?你为什么要遵循这种方法?只需调用ComboBox 变量并添加监听器! p 好像是NULL

      【讨论】:

        猜你喜欢
        • 2014-12-03
        • 2014-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-29
        相关资源
        最近更新 更多