Java charAt() String index out of range: 0

java

Solution

you must check null and length greater than 0.

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

Problem

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')` Any help is appreciated. Thanks

Original source