Java substring: 'string index out of range'

后端 未结 13 777
眼角桃花
眼角桃花 2020-12-03 10:24

I\'m guessing I\'m getting this error because the string is trying to substring a null value. But wouldn\'t the \".length() > 0\" part eliminate

13条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 10:37

    itemdescription is shorter than 38 chars. Which is why the StringOutOfBoundsException is being thrown.

    Checking .length() > 0 simply makes sure the String has some not-null value, what you need to do is check that the length is long enough. You could try:

    if(itemdescription.length() > 38)
      ...
    

提交回复
热议问题