1

I have the following code implemented in Java.

public class FadeLabel extends javax.swing.JLabel {

    private Timer onTimer;

    public FadeLabel() {
        init();
    }

    private void init(){
        setHorizontalAlignment(CENTER);
        setVerticalAlignment(CENTER);
        onTimer = new Timer(100, (ActionEvent e) -> {
            // animation code
        });
    }

    @Override
    public void setText(String text) {
        onTimer.start();
        super.setText(text);
    }
}

When I create an object of this class, it gives a NullPointerException in the setText() method, the line onTimer.start().

I want to know why is this exception happening..?

ps: I know what is a NullPointerException is. I want to know how is it generated here..

This is how I instantiate.

notificationLabel = new FadeLabel();
Ramesh-X
  • 4,853
  • 6
  • 46
  • 67
  • 1
    You are creating the Label with `new FadeLabel()` ? If you use any other constructor, you would not create a new Timer. That is the only one I could think of right now. – geisterfurz007 Oct 12 '17 at 07:53
  • @geisterfurz007 since there is no other constructor, let's assume he's not using another one – Stultuske Oct 12 '17 at 07:55
  • Show us the code piece where you are actually creating new instance `FabeLabel` – Antoniossss Oct 12 '17 at 07:55
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – TT. Oct 12 '17 at 07:59
  • @TT. nope. it's not a duplicate of that. he's asking why the NPE, when the variable is in fact instantiated – Stultuske Oct 12 '17 at 08:00
  • What are the odds that NPE comes actually not from `onTimer.start()` but from null reference to `FadeLabel` like this: `FadeLabel label; label.setText()` ?? :) – Antoniossss Oct 12 '17 at 08:00
  • Please add whole stacktrace as I suspect that NPE is not comming from the line you have pointed out. – Antoniossss Oct 12 '17 at 08:00
  • @Antoniossss not, since then the NPE would not be thrown in the setText method – Stultuske Oct 12 '17 at 08:01
  • @Stultuske Im saying that OP is mistaken and NPE is not from the line he pointed out. Stacktrace needed. – Antoniossss Oct 12 '17 at 08:02
  • @Antoniossss he COULD be mistaken. bit of a difference there. – Stultuske Oct 12 '17 at 08:05

2 Answers2

8

Looking at the source code for JLabel

public JLabel() {
    this("", null, LEADING);
}

public JLabel(String text, Icon icon, int horizontalAlignment) {
    setText(text);
    setIcon(icon);
    setHorizontalAlignment(horizontalAlignment);
    updateUI();
    setAlignmentX(LEFT_ALIGNMENT);
}

Thus we can see that the no-argument constructor calls the other one, which then calls setText. However at that point your init method hasn't run, so onTimer is still NULL.

Buddy
  • 10,874
  • 5
  • 41
  • 58
-3

You define the property onTimer. You have a method init. You must call it to instantiate the timer property onTimer.

Buddy
  • 10,874
  • 5
  • 41
  • 58
michaeldbjava
  • 114
  • 10