-1

When is try to get an input from the textfield it gives me an error message:

 "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: 
  Cannot invoke "javax.swing.JTextField.getText()" because 
 "this.textField2" is null"

I have tried everything that I am aware and I'm expecting to set the value of an integer using the:

 x = textField1.getText();
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.*;
import javax.swing.*;

public class Frame extends JFrame implements ActionListener
{

    JTextField textField1;
    JTextField textField2;
    JTextField textField3;
    JButton button;
    int[] newArr = new int[2];
    int num1, num2, num3;
    //we need to declare button and text field outside constructor so they become global 
   
   
   
   Frame()
   {
      //Text box
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setLayout(new FlowLayout());
      this.setPreferredSize(new Dimension(500,100));
      //Text field 1
      JTextField textField1 = new JTextField();
      textField1.setPreferredSize(new Dimension(40,40));
      this.add(textField1);
      //Text field 2
      JTextField textField2 = new JTextField();
      textField2.setPreferredSize(new Dimension(40,40));
      this.add(textField2);
      //Text field 3
      JTextField textField3 = new JTextField();
      textField3.setPreferredSize(new Dimension(40,40));
      this.add(textField3);
      //Button 1
      button = new JButton("Submit Guess");
      button.addActionListener(this);
      this.add(button);
      
      
      this.pack();
      this.setVisible(true);
      
   }
   public void actionPerformed(ActionEvent e)
   {
      if (e .getSource() == button)//if source of event comes from button...
        {
          newArr[0] = Integer.parseInt(textField1.getText());
          newArr[1] = Integer.parseInt(textField2.getText());
          newArr[2] = Integer.parseInt(textField3.getText());
          System.out.println(newArr[0] + newArr[1] + newArr[2]);
         
      }
      
   }
  
}
Sydney_dev
  • 1,448
  • 2
  • 17
  • 24
  • 1
    You're defining your `JTextFields` as class fields and local fields. – Gilbert Le Blanc May 12 '23 at 20:11
  • 2
    *common* error on this site: `JTextField textField2 = new JTextField();` is declaring (and initializing) a local variable which is **not** the same as the field `JTextField textField2;` declared at the start of the class. The field gets the default value `null` – user16320675 May 12 '23 at 20:20

1 Answers1

1

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

Here's a revised GUI:

Guess

Creating a JPanel to place in the JFrame helps to separate your concerns and focus on one part of the GUI at a time.

Using a JFrame is better than extending a JFrame. You shouldn't extend any Java class unless you intend to override one or more of the class methods.

A Java array has to be long enough to hold all of the elements of the array.

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class JTextfieldExample implements ActionListener {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JTextfieldExample();
            }
        });
    }

    JTextField textField1;
    JTextField textField2;
    JTextField textField3;
    JButton button;
    int[] newArr = new int[3];
    int num1, num2, num3;
    // we need to declare button and text field outside constructor so they
    // become global

    JTextfieldExample() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createMainPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        // Text field 1
        textField1 = new JTextField(4);
        panel.add(textField1);
        // Text field 2
        textField2 = new JTextField(4);
        panel.add(textField2);
        // Text field 3
        textField3 = new JTextField(4);
        panel.add(textField3);
        // i Button 1
        button = new JButton("Submit Guess");
        button.addActionListener(this);
        panel.add(button);

        return panel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button)// if source of event comes from button...
        {
            newArr[0] = Integer.parseInt(textField1.getText());
            newArr[1] = Integer.parseInt(textField2.getText());
            newArr[2] = Integer.parseInt(textField3.getText());
            System.out.println(newArr[0] + newArr[1] + newArr[2]);

        }

    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111