简体   繁体   English

尝试捕获块获取字符串长度错误

[英]Try-Catch block getting length of string error

I have everything working but I am having an issue with the JTextField. 我一切正常,但JTextField出现问题。 When the user submits nothing, it should be returning null rather than 0 . 当用户什么都不提交时,它应该返回null而不是0 So far it's returning 0, but I need it to return NULL. 到目前为止,它返回0,但是我需要它返回NULL。 It's not reaching the Catch block at all even if I put System.out.println("test"); 即使我将System.out.println("test");放到了Catch块,它也完全没有到达 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();

    }
  }

}

getText() will never return null . getText()永远不会返回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. 如果要使用null值或异常,则必须检测长度为零的字符串,然后返回null或自行引发异常。

JavaDoc for getText() 用于getText()的JavaDoc

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

Let me ask you... What if you can return null. 让我问你...如果可以返回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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM