-2
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JTextPane;

@SuppressWarnings({ "serial", "unused" })
public class jframe extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    jframe frame = new jframe();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public jframe() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JLabel lblNewLabel = new JLabel("        PRESS THE BUTTON TO GET CLICKS");
        lblNewLabel.setFont(new Font("Sitka Small", Font.PLAIN, 15));
        lblNewLabel.setBounds(29, 71, 382, 34);
        contentPane.add(lblNewLabel);
        
        JButton btnNewButton = new JButton("CLICK ME");
        btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnNewButton.setForeground(Color.DARK_GRAY);
        btnNewButton.setBackground(Color.CYAN);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { 
                myMethod();
            }
        });
        btnNewButton.setBounds(153, 139, 116, 42);
        contentPane.add(btnNewButton);
        
        JTextPane txtpnClicks = new JTextPane();
        txtpnClicks.setForeground(Color.GRAY);
        txtpnClicks.setText("                        CLICKS:"+x);
        txtpnClicks.setFont(new Font("Sylfaen", Font.PLAIN, 15));
        txtpnClicks.setBounds(86, 189, 275, 28);
        contentPane.add(txtpnClicks);
    }
    static int x = 1;
    @SuppressWarnings("null")
    static void add1() {
        
        x++;
        textpnClicks.setText("CLICKS"+x);
}   
    
    
}

I have tried looking for the answer online. When I run the code this error comes up in the console :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.swing.JLabel.setText(String)" because "txtpnClicks" is null
    at jframe.myMethod(jframe.java:77)
    at jframe$2.actionPerformed(jframe.java:59)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6614)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6379)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2769)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.swing.JLabel.setText(String)" because "txtpnClicks" is null
    at jframe.myMethod(jframe.java:79)
    at jframe$2.actionPerformed(jframe.java:60)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6614)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6379)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2769)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Read the exception: `because "txtpnClicks" is null`. In your constructor you're creating a **local** variable called `txtpnClicks` which has nothing to do with the field of the same name, which still remains uninitialized. Also, this shouldn't compile, because there isn't actually any field called `txtpnClicks` but you're trying to access it in `add1` – QBrute Jul 03 '21 at 16:14

1 Answers1

2

The function add1() doesn't know anything about the existence of an instance of the JTextPane class - txtpnClicks, so cannot invoke method setText(). It's written clearly in error log

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.swing.JLabel.setText(String)" because "txtpnClicks" is null at jframe.myMethod(jframe.java:77)

You need to pass it to the function

static void add1(JTextPane txtpnClicks) {
    x++;
    txtpnClicks.setText("CLICKS:" + x);
}

Moreover txtpnClicks must be created before btnNewButton, otherwise btnNewButton.addActionListener(e -> add1(txtpnClicks)); won't understand what is txtpnClicks

Working code:

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
@SuppressWarnings({"serial", "unused"})
public class jframe extends JFrame {
private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
        try {
            jframe frame = new jframe();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}

public jframe() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("PRESS THE BUTTON TO GET CLICKS");
    lblNewLabel.setFont(new Font("Sitka Small", Font.PLAIN, 15));
    lblNewLabel.setBounds(29, 71, 382, 34);
    contentPane.add(lblNewLabel);

    JTextPane txtpnClicks = new JTextPane();
    txtpnClicks.setForeground(Color.GRAY);
    txtpnClicks.setText("CLICKS:" + x);
    txtpnClicks.setFont(new Font("Sylfaen", Font.PLAIN, 15));
    txtpnClicks.setBounds(86, 189, 275, 28);
    contentPane.add(txtpnClicks);

    JButton btnNewButton = new JButton("CLICK ME");
    btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
    btnNewButton.setForeground(Color.DARK_GRAY);
    btnNewButton.setBackground(Color.CYAN);
    btnNewButton.addActionListener(e -> add1(txtpnClicks));
    btnNewButton.setBounds(153, 139, 116, 42);
    contentPane.add(btnNewButton);
}

static int x = 1;

@SuppressWarnings("null")
static void add1(JTextPane txtpnClicks) {
    x++;
    txtpnClicks.setText("CLICKS:" + x);
}
}
qwezxc789
  • 124
  • 1
  • 8
  • OP: Please accept the answer(1) by clicking the 'tick' mark to the left of the question. 1) Assuming it helped solve the problem. If not, add a comment as to why it did not work for your app. or situation. – Andrew Thompson Dec 12 '21 at 05:45