jonathan
jonathan

Reputation: 291

Can someone explain why I'm getting this error in Java?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at A_test.addButton(A_test.java:27)
    at A_test.<init>(A_test.java:17)
    at Main$1.run(Main.java:11)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Main class

import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {                
            public void run() {
                new A_test();
            }
        });
    }
}

A_test class

import java.awt.event.*;
import javax.swing.*;

public class A_test implements ActionListener {

    private JFrame jf;

    public A_test() {
        JFrame jf = new JFrame("A simple swing Program");
        jf.setSize(275,100);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel jl = new JLabel("Swing powers the modern JAVA GUI");
        jf.getContentPane().add(jl);
        this.addButton();
        jf.setVisible(true);
    }

    public void addButton() {
        JButton jb = new JButton("Submit");
        jf.getContentPane().add(jb);
        jb.addActionListener(this);
        jf.getContentPane().add(jb);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        ((JLabel) jf.getContentPane().getComponent(0)).setText("test");
    }    
}

I'm using the A_test class in order to create a JFrame with a label and a button in it. When I press the button I want the application to change the text of the label to "test" from what it was before.

Upvotes: 0

Views: 130

Answers (3)

Bill K
Bill K

Reputation: 62769

You should start using an editor like Eclipse or netbeans and Pay Attention To The Warnings. They would have told you exactly what was wrong (That your variable was shadowing a member variable).

Java gives awesome warnings--Java's "Pickyness" causes many people to think the language is more "Difficult" but it actually makes it MUCH easier. A language with a lax syntax is really much harder in the long run.

Upvotes: 1

DaveJohnston
DaveJohnston

Reputation: 10151

You have two instances of jf. One is an member of the class and is not initialised. The other is a local variable within the constructor which is initialised. So when addButton is called you are trying to access the member variable which is null and hence the NullPointerException.

Upvotes: 1

Mechkov
Mechkov

Reputation: 4324

Here

JFrame jf = new JFrame("A simple swing Program");
jf.setSize(275,100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel jl = new JLabel("Swing powers the modern JAVA GUI");
jf.getContentPane().add(jl);
this.addButton();
jf.setVisible(true);

You are creating a new JFrame instead of assigning the private instance variable to a new JFrame. By the time you reach this point:

  JButton jb = new JButton("Submit");
jf.getContentPane().add(jb);
jb.addActionListener(this);
jf.getContentPane().add(jb);

jf is still null;

Just do this:

jf = new JFrame("A simple swing Program");
jf.setSize(275,100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel jl = new JLabel("Swing powers the modern JAVA GUI");
jf.getContentPane().add(jl);
this.addButton();

Upvotes: 4

Related Questions