-1

I'm trying to make a Java Gui that print the text on a JTextField into a label. So, on runtime I'll write something in a JTextField then I'll press a button that print the content of JTextField in a label.

But the program give me a java.lang.NullPointerException. Is there something wrong in ActionListener method?

Could you help me?

Below the code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.util.*;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import java.awt.Rectangle;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.util.EventObject;
import java.awt.event.ActionEvent;

public class MastermindGui1 extends JFrame {

public static JTextField insNome;
public static JLabel welcomeLbl;

private JPanel contentPane;
Container contenuto;

public MastermindGui1() {
    JFrame finestra = new JFrame("MASTERMIND");
    finestra.setResizable(false);

    finestra.setBounds(200,200,300,300);
    contenuto = finestra.getContentPane();
    contenuto.setBackground(Color.YELLOW);
    finestra.getContentPane().setLayout(null);

    JTextField insNome = new JTextField();
    insNome.setBounds(21, 70, 151, 35);
    contenuto.add(insNome);

    JLabel welcomeLbl = new JLabel("");
    welcomeLbl.setVisible(false);
    welcomeLbl.setBounds(58, 138, 180, 74);
    welcomeLbl.setBackground(Color.ORANGE);
    contenuto.add(welcomeLbl);

    JLabel lblNewLabel = new JLabel("INSERISCI IL TUO NOME");
    lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
    lblNewLabel.setBounds(75, 11, 142, 23);
    finestra.getContentPane().add(lblNewLabel);

    JButton stampa = new JButton("STAMPA");
    stampa.addActionListener(new stampa());
    stampa.setBounds(195, 76, 89, 23);
    contenuto.add(stampa);
    stampa.addActionListener(new stampa());

    finestra.setVisible(true);
    finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private class stampa implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent e) {
        insNome = (JTextField) e.getSource();

        String testo = insNome.getText();

        welcomeLbl.setText(testo);
    }
}

public static void main(String[] args) {
            MastermindGui1 finestra = new MastermindGui1();
        }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Novice
  • 7
  • 2
  • You have to `stampa.addActionListener(new stampa());` lines... Only one needed. Also do not use lower case class names... – Usagi Miyamoto Aug 03 '17 at 17:52
  • Also this line `insNome = (JTextField) e.getSource();` overwrites your `insNome` variable with an incorrect value, as `getSource()` will be the button, not the textfield... – Usagi Miyamoto Aug 03 '17 at 17:53
  • Why the SHOUTING in the title? – EJoshuaS - Stand with Ukraine Aug 03 '17 at 18:09
  • 1) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 2) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831). 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and .. – Andrew Thompson Aug 03 '17 at 22:06
  • .. borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 03 '17 at 22:07
  • 4) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Aug 03 '17 at 22:14

2 Answers2

4

replace JLabel welcomeLbl = new JLabel(""); with:

welcomeLbl = new JLabel("");

Your public static JLabel welcomeLbl; still null because you didn't initialize it but did create new local variable

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
0

Thanks to Maxim. I replace the code where you said and now it works. I delete also this:

"private class stampa implements ActionListener"

and i change the creation of the class like this :

"public class MastermindGui1 extends JFrame implements ActionListener".

Novice
  • 7
  • 2
  • 1
    If Maxim;s suggestion solved your problem then don't forget to "accept" the answer by clicking on the checkmark so people know the problem has been solved. Also, this is NOT an answer so it should not be posted as one. You and edit your question if you wish. Delete this answer. – camickr Aug 03 '17 at 20:35