0

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();

    }
  }

}
1
  • 1
    No really, why are you catching a NullPointerException? Commented Mar 12, 2015 at 15:19

3 Answers 3

1

getText() will never return null. If you want a null value, or an exception, you will have to detect a zero-length string and return null or throw the exception yourself.

JavaDoc for getText()

Sign up to request clarification or add additional context in comments.

Comments

0
public void actionPerformed(ActionEvent e) {

  try{
    String text = inputField.getText();
    System.out.println(text);
    outputLabel.setText("String length = " + text.length());
    if (text.length()==0)
      throw new NullPointerException();
    }
    catch(NullPointerException e1)
    {
      e1.printStackTrace();
    }
 }

Comments

0

Let me ask you... What if you can return null. Is your code supposed to give the user a second try at entering a string ?? if so then try

    try
        {
        firstTry = inputField.getText();
            if(firstTry.length() == 0)
                {
                    outputLabel.setText("String Length = "+secondTry.length());
                }
            else
                {
                outputLabel.setText("String length = "+firstTry.length());
                }
        }//end try
    catch(NullPointerException e1)
        {
            secondTry=JOptionPane.showInputDialog ( "empty try again" );
            outputLabel.setText("String Length = "+secondTry.length());
        }//end catch

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.