I have everything working but I am having an issue with the JTextField. When the user submits nothing, it should be returning null rather than 0. So far it's returning 0, but I need it to return NULL. It's not reaching the Catch block at all even if I put System.out.println("test"); it's not reaching there.
import javax.swing.*;
import javax.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class stringlength implements ActionListener {
public static JLabel outputLabel;
public static JTextField inputField = new JTextField(20);
public static void main(String[] args) {
stringlength myWindow = new stringlength ();
}
public stringlength (){
JFrame frame = new JFrame("stringlength");
frame.setVisible(true);
frame.setSize(500,100);
//frame.setLayout(new GridLayout(1,3));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
outputLabel = new JLabel("String length = ");
JButton stringLengthButton = new JButton("Get String Length");
stringLengthButton.addActionListener(this);
panel.add(stringLengthButton);
panel.add(outputLabel);
panel.add(inputField);
}
public void actionPerformed(ActionEvent e) {
try{
String text = inputField.getText();
System.out.println(text);
outputLabel.setText("String length = " + text.length());
}
catch(NullPointerException e1)
{
e1.printStackTrace();
}
}
}
NullPointerException?