Pergunta

Eu estou supondo que eu estou recebendo este erro porque a cadeia está tentando substring um valor null. Mas não seria a parte ".length() > 0" eliminar esse problema?

Aqui está o Java trecho:

if (itemdescription.length() > 0) {
    pstmt2.setString(3, itemdescription.substring(0,38));
} 
else { 
    pstmt2.setString(3, "_");
} 

Eu tenho esse erro:

 java.lang.StringIndexOutOfBoundsException: String index out of range: 38
    at java.lang.String.substring(Unknown Source)
    at MASInsert2.itemimport(MASInsert2.java:192)
    at MASInsert2.processRequest(MASInsert2.java:125)
    at MASInsert2.doGet(MASInsert2.java:219)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:835)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1286)
    at java.lang.Thread.run(Unknown Source)
Foi útil?

Solução

Eu "estou supondo que eu estou recebendo este erro porque a cadeia está tentando substring um valor nulo. Mas não o faria o" .length ()> 0" parte eliminar essa questão?

Não, chamando itemdescription.length () quando itemdescription é nulo não geraria um StringIndexOutOfBoundsException, mas sim um NullPointerException desde que você seria, essencialmente, estar tentando chamar um método em nulo .

Como já foi indicado, StringIndexOutOfBoundsException indica que itemdescription não é pelo menos 38 caracteres. Você provavelmente vai querer lidar com ambas as condições (I supondo que você deseja truncar):

final String value;
if (itemdescription == null || itemdescription.length() <= 0) {
    value = "_";
} else if (itemdescription.length() <= 38) {
    value = itemdescription;
} else { 
    value = itemdescription.substring(0, 38);
}
pstmt2.setString(3, value);

Pode ser um bom lugar para uma função de utilidade se você fizer isso muito ...

Outras dicas

É uma pena que substring não é implementado de uma forma que lida com seqüências curtas - como em outros idiomas, por exemplo Python.

Ok, não podemos mudar isso e tem que considerar neste caso borda cada vez que usar substr, em vez de if-else cláusulas gostaria de ir para esta variante mais curta:

myText.substring(0, Math.min(6, myText.length()))

Você realmente precisa verificar se o comprimento da corda é maior ou igual a 38.

Eu recomendaria apache commons lang . Um one-liner cuida do problema.

pstmt2.setString(3, StringUtils.defaultIfEmpty(
    StringUtils.subString(itemdescription,0, 38), "_")); 

substring(0,38) significa a seqüência tem que ser de 38 caracteres ou mais. Se não, o "índice de Cordas está fora de alcance".

if (itemdescription != null && itemdescription.length() > 0) {
    pstmt2.setString(3, itemdescription.substring(0, Math.min(itemdescription.length(), 38))); 
} else { 
    pstmt2.setString(3, "_"); 
}

Estou assumindo sua coluna é de 38 caracteres de comprimento, assim que você quer truncar itemdescription para caber dentro do banco de dados. Uma função de utilitário como o seguinte deve fazer o que quiser:

/**
 * Truncates s to fit within len. If s is null, null is returned.
 **/
public String truncate(String s, int len) { 
  if (s == null) return null;
  return s.substring(0, Math.min(len, s.length()));
}

então você apenas chamá-lo assim:

String value = "_";
if (itemdescription != null && itemdescription.length() > 0) {
  value = truncate(itemdescription, 38);
}

pstmt2.setString(3, value);

método substring de Java falha ao tentar obter uma substring a partir de um índice que é maior que o string.

Uma alternativa é fácil de usar Apache Commons StringUtils.substring :

public static String substring(String str, int start)

Gets a substring from the specified String avoiding exceptions.

A negative start position can be used to start n characters from the end of the String.

A null String will return null. An empty ("") String will return "".

 StringUtils.substring(null, *)   = null
 StringUtils.substring("", *)     = ""
 StringUtils.substring("abc", 0)  = "abc"
 StringUtils.substring("abc", 2)  = "c"
 StringUtils.substring("abc", 4)  = ""
 StringUtils.substring("abc", -2) = "bc"
 StringUtils.substring("abc", -4) = "abc"

Parameters:
str - the String to get the substring from, may be null
start - the position to start from, negative means count back from the end of the String by this many characters

Returns:
substring from start position, null if null String input

Note, se você não pode usar o Apache Commons lib por algum motivo, você poderia simplesmente pegar as peças que você precisa da fonte

// Substring
//-----------------------------------------------------------------------
/**
 * <p>Gets a substring from the specified String avoiding exceptions.</p>
 *
 * <p>A negative start position can be used to start {@code n}
 * characters from the end of the String.</p>
 *
 * <p>A {@code null} String will return {@code null}.
 * An empty ("") String will return "".</p>
 *
 * <pre>
 * StringUtils.substring(null, *)   = null
 * StringUtils.substring("", *)     = ""
 * StringUtils.substring("abc", 0)  = "abc"
 * StringUtils.substring("abc", 2)  = "c"
 * StringUtils.substring("abc", 4)  = ""
 * StringUtils.substring("abc", -2) = "bc"
 * StringUtils.substring("abc", -4) = "abc"
 * </pre>
 *
 * @param str  the String to get the substring from, may be null
 * @param start  the position to start from, negative means
 *  count back from the end of the String by this many characters
 * @return substring from start position, {@code null} if null String input
 */
public static String substring(final String str, int start) {
    if (str == null) {
        return null;
    }

    // handle negatives, which means last n characters
    if (start < 0) {
        start = str.length() + start; // remember start is negative
    }

    if (start < 0) {
        start = 0;
    }
    if (start > str.length()) {
        return EMPTY;
    }

    return str.substring(start);
}

itemdescription é menor do que 38 caracteres. É por isso que o StringOutOfBoundsException está sendo lançada.

Verificar .length() > 0 simplesmente garante que o String tem algum valor não-nulo, o que você precisa fazer é verificar que o comprimento é longo o suficiente. Você poderia tentar:

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

Você deve verificar o comprimento da cadeia. Você supor que você pode fazer substring(0,38) enquanto cadeia não é null, mas você realmente precisa do String para ser de comprimento pelo menos 38 caracteres.

Quando este for o caso, eu uso jogos em vez de substring .

Com substring :

if( myString.substring(1,17).equals("Someting I expect") ) {
    // Do stuff
}
// Does NOT work if myString is too short

com jogos (deve usar a notação de Regex):

if( myString.matches("Someting I expect.*") ) {
    // Do stuff
}
// This works with all strings

Você começa este se itemdescription é menor do que 38 caracteres

Você pode olhar que exceções são lançadas e quando na API JAVA no seu caso para String # substring (int, int): https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#substring-int-int-

substring
public String substring(int beginIndex, int endIndex)
   . . .

Throws:
 IndexOutOfBoundsException
 if the beginIndex is negative,
 or endIndex is larger than the length of this String object, 
 or beginIndex is larger than endIndex.



(same applies to previous java versions as well)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top