1

So I wrote this code before without methods and parameter passing and it worked splendid!

But once I started making it more organized using methods and passing parameters, I'm running into Null Pointer Exception. Why am I getting such an error?

import javax.swing.JFrame;
import javax.swing.JLabel;


public class test {

    private static JLabel label;

    public static void main(String[] args){
        initializeLabel();
        initializeFrame(label);
    }

    private static void initializeLabel(){
        JLabel label = new JLabel();

        label.setText("hi");

    }

    private static void initializeFrame(JLabel label){
        JFrame frame = new JFrame();
        frame.add(label);
        frame.setVisible(true);
    }

}
2
  • It's helpful to include the actual error message (since it will tell you exactly what's wrong). Commented Nov 8, 2011 at 5:08
  • don't create a new label in initializeLabel() Commented Nov 8, 2011 at 5:10

7 Answers 7

2

The problem is this function:

private static void initializeLabel(){
    JLabel label = new JLabel(); // This creates a new variable named label
    label.setText("hi");
}

You're trying to intialize this.label, but you're just creating a new variable and assigning it. Change it to this:

private static void initializeLabel(){
    label = new JLabel(); // or this.label = new JLabel();
    label.setText("hi");
}
Sign up to request clarification or add additional context in comments.

Comments

1

It's because you initialize label inside a method so it's scope is only to your method initializeLabel(). You should make label a global variable and then do the initialization in you method, that way your whole program will have access to it.

Comments

1

Because in initializeLabel you create a new label, but you don't assign the reference to it to your static variable, but instead to a local variable that is lost after the method executes.

Change to:

private static void initializeLabel(){
    label = new JLabel();

    label.setText("hi");

} 

Comments

1

When you do:

JLabel label = new JLabel();

within initializeLabel(), that's a new, local variable, not the object-level one you think you're setting. The object-level one is therefore still set to its default value of null.

If you want to affect the object-level one, just set it thus:

label = new JLabel();

Comments

1

Change line 15 to:

    label = new JLabel();

Otherwise you redefined label.

Comments

1

Since your are parsing an uninitialized JLabel to your initializeFrame(JLabel label) method you are getting this exception.

Initialize label before parsing to the initializeFrame(JLabel label) method.

initializeLabel();
label =  new JLabel("Your label Name ...");
initializeFrame(label);

Comments

1
 private static void initializeFrame(JLabel label){
        JFrame frame = new JFrame();
        frame.add(label);  <-- Here it might be throwing NPE because label is not initialized and is null.
        frame.setVisible(true);
    }

You have not initialized global label variable that's why you are receiving NPE. Don't create new label in initializeLabel() method else initialize global variable in it. So it look something like this...

private static void initializeLabel(){
    label = new JLabel();
    label.setText("hi");
}

Also change initializeFrame(JLabel label) to initializeFrame(). No need to pass label as parameter in it as you have declared label in class' global scope.

private static void initializeFrame(){
    JFrame frame = new JFrame();
    frame.add(label);
    frame.setVisible(true);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.