简体   繁体   English

Java charAt()字符串索引超出范围:0

[英]Java charAt() String index out of range: 0

I have this code : 我有这个代码:

private void submitPstart() {

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


    }else {
        errorBox ("Uppercase A-Z");
    }

    tStock.setText("");
    tStock.setFocus();
}

THis is working but when I try not to put anything on the textbox and press the OK button it crashes. 这是有效但当我尝试不在文本框上放任何东西并按下确定按钮时它会崩溃。 It says: 它说:

java.lang.StringIndexOutOfBoundsException: String index out of range: 0

and it pointed out to this part: if (tStock.getText().charAt(0)>='A' && tStock.getText().charAt(0)<='Z') 它指出了这一部分: if (tStock.getText().charAt(0)>='A' && tStock.getText().charAt(0)<='Z')

Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

You need to check if getText() returns a 0-length (ie empty) string. 您需要检查getText()返回0长度(即空)字符串。

If it does, then don't try to pull the first character out! 如果确实如此,那就不要试图拉出第一个角色! (via charAt() ) (通过charAt()

Note that your commented-out check for length() should occur prior to the existing character check. 请注意,对于length()注释掉检查应该在现有字符检查之前进行。

You may want to check for a null string being returned as well, depending on your framework/solution etc. Note the Apache Commons StringUtils.isEmpty() method, which performs this check concisely. 您可能还希望检查是否返回了返回的空字符串,具体取决于您的框架/解决方案等。请注意Apache Commons StringUtils.isEmpty()方法,它简明地执行此检查。

你必须检查null和长度大于0。

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

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. 在您的情况下,如果文本为空,则返回的长度将为0.因此charAt(..)方法将引发异常。 As such, you should first check that the text that you're trying to compare is empty or not. 因此,您应首先检查您要比较的文本是否为空。

Add

if (tStockPIStart!=null && tStockPIStart.length>0) {
    [...]
}

In Java 7 there is the isEmpty method you can use to make things a bit more expressive in your code 在Java 7中,您可以使用isEmpty方法在代码中使表达更具表现力

if (tStockPIStart.getText()!=null && !tStockPIStart.getText().isEmpty()) {
    //do stuff
}

This is the same as doing length != 0 but I personally think is a bit more clear. 这与做length != 0相同length != 0但我个人认为更清楚一点。

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

相关问题 java String-超出范围的字符串索引,charAt - java String - String index out of range , charAt Java charAt() 字符串索引超出范围 - Java charAt() String index out of range Java charAt()字符串索引超出范围:5 - Java charAt() String index out of range: 5 charAt(0) 字符串索引超出范围:0 - charAt(0) String index out of range: 0 Java日志文件读取器,charAt()字符串索引超出范围 - Java Log File Reader, charAt() String index out of range Java中使用charAt的“字符串索引超出范围”错误消​​息 - 'String Index out of Range' Error Message in Java using charAt Java字符串索引越界,CharAt问题 - Java string index out of bound , CharAt problem java中的字符串索引越界错误(charAt) - string index out of bounds error in java (charAt) 线程“main”中的异常 java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658) - Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658) 线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:java.lang.String.charAt(String.java:646)处为5 - Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 5 at java.lang.String.charAt(String.java:646)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM