线程"Timer-0" java.lang.NullPointerException 中的异常



我有下面的代码,它向我显示了一个异常,它是:线程"Timer-0"java.lang.NullPointerException中的异常。错误在第行:jLabel1.setIcon(黑色);如果我删除这一行,那么它运行良好。知道吗?非常感谢。

Icon black=createImageIcon("black.PNG");
protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = Frame1.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}
public Frame1(int seconds) {
    timer = new Timer();
    timer.schedule(new RunMeTask(), 1000,1000);
    }

 public class RunMeTask extends TimerTask {
    public int k=0;
public void run() {
    System.out.println("Run Me ~");
            jLabel1.setIcon(black);    //error
            k++;
            if (k==10) {
              timer.cancel();
            }
}
}
public static void main(String args[]) {
    new Frame1(1);
    System.out.format("Task scheduled.%n");
java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Frame1().setVisible(true);
        }
    });
}

Code要么不知道什么是jLabel1,要么不知道black。请确保两者都是该run()方法视图中的对象。

最新更新