0

I just started to learn Swing, and I encountered a NullPointerException when I try to use the constraints in adding widgets to my content.

Error Trace:

NullPointerException
at chapter.pkg6.ImprovedForceCalculator.<init>(ImprovedForceCalculator.java:56)
at chapter.pkg6.ImprovedForceCalculator.main(ImprovedForceCalculator.java:36)

Here is my code:

import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Font;

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

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class ImprovedForceCalculator {
    private JFrame frame;
    private Container content;
    private GridBagConstraints constraints;

    private JLabel massLabel;
    private JLabel accelerationLabel;

    private JTextField massInput;
    private JTextField accelerationInput;

    private JButton calculateForce;

    private double mass;
    private double acceleration;
    private double force;

    public static void main(String[] args)
    {
        new ImprovedForceCalculator();
    }

    public ImprovedForceCalculator()
    {
        frame = new JFrame();
        frame.setTitle("Improved Force Calulcator");
        frame.setResizable(false);
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        constraints = new GridBagConstraints();

        content = frame.getContentPane();
        content.setLayout(new GridBagLayout());

        massLabel = new JLabel("Enter the mass of the object (m): ");
        massLabel.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        content.add(massLabel);
        constraints.gridx = 0;
        constraints.gridy = 0;
        content.add(massInput, constraints);

        massInput = new JTextField();
        massInput.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        constraints.gridx = 1;
        constraints.gridy = 0;
        content.add(massInput, constraints);

        accelerationLabel = new JLabel("Enter the acceleration of the object (a): ");
        accelerationLabel.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridx = 0;
        constraints.gridy = 1;
        content.add(accelerationLabel, constraints);

        accelerationInput = new JTextField();
        accelerationInput.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        constraints.gridx = 1;
        constraints.gridy = 1;
        content.add(accelerationInput, constraints);

        calculateForce = new JButton("Calculate Force");
        calculateForce.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        calculateForce.addActionListener (new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    mass = Double.parseDouble(massInput.getText());
                    acceleration = Double.parseDouble(accelerationInput.getText());
                    force = mass * acceleration;

                    System.out.println("Force: " + force + " newtons");
                }
                catch (NumberFormatException ex)
                {
                    JOptionPane.showMessageDialog(frame, "Please enter a valid mass and/or acceleration");
                }
            }
        });
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 2;
        content.add(calculateForce, constraints);

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

1 Answers1

1

You have a null pointer exception because you try to use massInput before you initialize it. See here:

  content.add(massInput, constraints);

    massInput = new JTextField();

I think you meant for the first line to be content.add(massLabel, constraints) instead. Initialize massInput before adding it to content.

Caleb An
  • 366
  • 1
  • 10