I have this code :
private void submitPstart() {
if (tStock.getText().charAt(0)>=\'A\' && tStock.getText().charAt(0)<=\'Z\'){
}else
In Java 7 there is the isEmpty method you can use to make things a bit more expressive in your code
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.
You need to check if getText()
returns a 0-length (i.e. empty) string.
If it does, then don't try to pull the first character out! (via charAt()
)
Note that your commented-out check for length()
should occur prior to the existing character check.
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.
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'){
Add
if (tStockPIStart!=null && tStockPIStart.length>0) {
[...]
}
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.