0

I have the code below,and it shows me an exception which is: Exception in thread "Timer-0" java.lang.NullPointerException. The erros is in line : jLabel1.setIcon(black); If I delete this line,then it works well. Any idea? Thank you !

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);
        }
    });
}
Smit
  • 4,617
  • 1
  • 22
  • 27
user2933161
  • 33
  • 1
  • 3
  • 11
  • 1
    where is `jLable1` initialized or declared? – Anirban Nag 'tintinmj' Nov 19 '13 at 19:44
  • You havent shown us the code where jLabel1 and black are decalred. – Deadron Nov 19 '13 at 19:47
  • @tintinmj I use Netbeans,so I have declared on Design View. – user2933161 Nov 19 '13 at 19:48
  • @Deadron I edit my post and you can see where I have declared black icon. – user2933161 Nov 19 '13 at 19:51
  • @user2933161 Here are two possibilities. **1.** `jLabel1` is not initialized **2.** `createImageIcon` return `null` if `imgURL` is null. Make sure you have provided correct file path and file name. – Smit Nov 19 '13 at 19:58
  • @Smit createImageIcon has a println() and it is not show me that it can't find a file,so it is ok at this part. How can I initialize the jlabel ? I did what I do anytime I use a jlabel. :/ – user2933161 Nov 19 '13 at 20:10
  • @user2933161 YOu have to initialized somewhere correct place in your code like `JLabel jLabel1 = new JLabel()`. If you dont do `new XXX` then it will be `null` by default. For more details see [`--> Creating Objects – Smit Nov 19 '13 at 20:21
  • 1
    Sorry to all,I found the error. I forget to use initComponents(); in public Frame1() . Thank you ! – user2933161 Nov 19 '13 at 20:23

1 Answers1

0

Code either doesn't know what jLabel1 is, or it doesn't know what black is. Make sure both are Objects within view of that run() method.

DoubleDouble
  • 1,465
  • 10
  • 25
  • This isn't a compiler error. `NullPointerExceptions` show up when you run the program, indicating that everything compiled fine. The problem is that `jLabel1` hasn't been initialized. – musical_coder Nov 19 '13 at 19:53
  • @musical_coder Can you help me? Where is the error exactly? I don't understand. Thank you ! – user2933161 Nov 19 '13 at 19:56
  • Sorry, edited my wrong wordage. Here is what I would do to try and see what is happening: 1. set a println() in the createImageIcon method to make sure it is being called. (and that "Couldn't Find File" is not showing up) 2. set a debug marker on the line where the error is happening. – DoubleDouble Nov 19 '13 at 20:04
  • Also, make sure `Icon black=createImageIcon("black.PNG");` happens before `jLabel1.setIcon(black);` – DoubleDouble Nov 19 '13 at 20:08
  • @DoubleDouble createImageIcon has a println() and it is not show me that it can't find a file,so it is ok at this part.. – user2933161 Nov 19 '13 at 20:09
  • Most likely jLabel1 is not initialized. If you are already calling `JLabel jLabel1` or something similar elsewhere, try doing `jLabel1 = new JLabel()` to see if the error is still thrown. – DoubleDouble Nov 19 '13 at 20:11
  • @DoubleDouble Sorry,I found the error. I forget to use initComponents(); in public Frame1() . Thank you ! – user2933161 Nov 19 '13 at 20:51