سؤال

أنا أظن أنني أحصل على هذا الخطأ لأن السلسلة تحاول البحث عن null القيمة. ولكن لن ".length() > 0" جزء القضاء على هذه المشكلة؟

هنا هو مقتطف جافا:

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

حصلت على هذا الخطأ:

 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)
هل كانت مفيدة؟

المحلول

أنا تخمين أنني أحصل على هذا الخطأ لأن السلسلة تحاول التركيز قيمة فارغة. ولكن لن يكون الجزء ". الطول ()> 0" القضاء على هذه المشكلة؟

لا، استدعاء ItemDescription.length. باطل.

كما أشار آخرون، يشير StringindexoutOfboundSexceptionSoxception إلى أن ItemDescription لا يقل طوله عن 38 حرفا على الأقل. ربما تريد التعامل مع كلا الشرطين (أفترض أنك تريد اقتطاع):

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);

قد يكون مكانا جيدا لوظيفة فائدة إذا قمت بذلك كثيرا ...

نصائح أخرى

إنه من المؤسف أن substring لا يتم تنفيذه بطريقة تتعامل مع سلاسل قصيرة - مثل بلغات أخرى مثل Python.

حسنا، لا يمكننا تغيير ذلك وعلي النظر في هذه الحالة الحافة في كل مرة نستخدمها substr, ، بدلا من أي جمل أخرى سأذهب لهذا البديل الأقصر:

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

تحتاج حقا إلى التحقق مما إذا كان طول السلسلة أكبر أو يساوي 38.

أوصي اباتشي المشاع Lang.. وبعد بطانة واحدة تعتني بالمشكلة.

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

substring(0,38) يعني أن السلسلة يجب أن تكون 38 حرفا أو أطول. إذا لم يكن الأمر كذلك، فإن "مؤشر السلسلة خارج النطاق".

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

أنا أفترض عمودك هو الطول 38 حرفا، لذلك تريد اقتطاع itemdescription لتناسب داخل قاعدة البيانات. يجب أن تفعل وظيفة الأداة المساعدة مثل ما يلي ما تريد:

/**
 * 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()));
}

ثم أنت فقط تسميها مثل ذلك:

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

pstmt2.setString(3, value);

جاوة substring فشلت الطريقة عند محاولة الحصول على فرعية بدءا من فهرس أطول من السلسلة.

بديل سهل هو استخدام المشاع أباتشي 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

ملاحظة، إذا لم تتمكن من استخدام Apache Commons Lib لسبب ما، فيمكنك فقط الاستيلاء على الأجزاء التي تحتاجها من المصدر

// 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 أقصر من 38 حرف. وهذا هو السبب في StringOutOfBoundsException يتم طرحها.

تدقيق .length() > 0 ببساطة يتأكد من String لديه بعض القيمة غير الفارغة، ما عليك القيام به هو التحقق من أن الطول طويل بما فيه الكفاية. بامكانك ان تحاول:

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

يجب عليك التحقق من طول السلسلة. تفترض أنه يمكنك القيام به substring(0,38) طالما سلسلة ليست كذلك null, ، لكنك بحاجة فعلا إلى السلسلة لتكون بطول 38 حرفا على الأقل.

عندما يكون هذا مناسبا، استخدم اعواد الكبريت بدلا من Substring..

مع Substring.:

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

مع اعواد الكبريت (يجب استخدام تدوين Regex):

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

تحصل على هذا إذا كان ItemDescription أقصر من 38 حرفا

يمكنك أن ننظر إلى الاستثناءات التي يتم إلقاؤها ومتى تعمل في Java API في حالة STRICE # SUSTRING (INT، INT): https://docs.oracle.com/javase/9/docs/api/java/lang/string.html#substring-int-int-

Substring.
السلسلة الفرعية العامة (Int Beginindex، Intindex)
   . . .

رميات:
 indexoutofboundsexception.إذا كان BeginIndex سلبي، أو Endindex أكبر من طول كائن السلسلة هذا, ، أو Beginindex أكبر من Endindex. (ينطبق نفسه على إصدارات Java السابقة أيضا)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top