Java charAt() String index out of range: 0

前端 未结 5 1199
刺人心
刺人心 2021-01-13 06:51

I have this code :

private void submitPstart() {

    if (tStock.getText().charAt(0)>=\'A\' && tStock.getText().charAt(0)<=\'Z\'){


    }else          


        
5条回答
  •  醉酒成梦
    2021-01-13 07:37

    Try

    if (tStockPIStart.getText().length() > 0 && tStockPIStart.getText().charAt(0)>='A' && tStockPIStart.getText().charAt(0)<='Z')
    

    In your case, if the text is empty, then the length returned will be 0. Hence the charAt(..) method will throw you an exception. As such, you should first check that the text that you're trying to compare is empty or not.

提交回复
热议问题