java使用collat​​or处理奇怪的行为

时间:2016-08-17 09:59:55

标签: java string

使用compareIgnoreCase时,我得到NegativeArraySizeException /'NullPointerException'(在Java 7 JRE中运行)。

Caused by: java.lang.NegativeArraySizeException
    at java.lang.String.toCharArray(String.java:2748)
    at sun.text.normalizer.NormalizerImpl.canonicalDecomposeWithSingleQuotation(NormalizerImpl.java:2506)
    at java.text.RBTableBuilder.build(RBTableBuilder.java:107)
    at java.text.RBCollationTables.<init>(RBCollationTables.java:83)
    at java.text.RuleBasedCollator.<init>(RuleBasedCollator.java:300)
    at java.text.RuleBasedCollator.<init>(RuleBasedCollator.java:281)
    at java.text.Collator.getInstance(Collator.java:276)

Caused by: java.lang.NullPointerException
    at java.lang.String.length(String.java:623)
    at java.text.RBTableBuilder.build(RBTableBuilder.java:84)
    at java.text.RBCollationTables.<init>(RBCollationTables.java:83)
    at java.text.RuleBasedCollator.<init>(RuleBasedCollator.java:300)
    at java.text.RuleBasedCollator.<init>(RuleBasedCollator.java:281)
    at java.text.Collator.getInstance(Collator.java:276)

这些似乎发生在String.compareIgnoreCase和String.equalsIgnoreCase

示例代码:

  public static int compareIgnoreCase(Locale locale, String a, String b) {
    if (a != null && a.length() == 0) {
      a = null;
    }
    if (b != null && b.length() == 0) {
      b = null;
    }
    //
    if (a == b) {
      return 0;
    }
    if (a == null) {
      return -1;
    }
    if (b == null) {
      return 1;
    }
    Collator collator = Collator.getInstance(locale);
    collator.setStrength(Collator.SECONDARY);
    return collator.compare(a, b);
  }

被称为:

  public static int compareIgnoreCase(String a, String b) {
    return compareIgnoreCase(NlsUtility.getDefaultLocale(), a, b);
  }

NlsUtility.getDefaultLocale:

  public static Locale getDefaultLocale() {
    Locale locale = LocaleThreadLocal.get();
    if (locale == null) {
      locale = Locale.getDefault();
    }
    return locale;
  }

似乎问题已经恢复,然后是JVM崩溃。我不知道如何解决这个问题:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f1dabb9d4f0, pid=4290, tid=139765228910336
#
# JRE version: Java(TM) SE Runtime Environment (7.0_80-b15) (build 1.7.0_80-b15)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.80-b11 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.so+0x7ee4f0]  nmethod::can_unload(BoolObjectClosure*, OopClosure*, oopDesc**, bool)+0x70
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /tmp/hs_err_pid4290.log
Compiled method (c2) 191914528 4305  s!         java.text.Collator::getInstance (157 bytes)
 total in heap  [0x00007f1da19f7f90,0x00007f1da19fa8f8] = 10600
 relocation     [0x00007f1da19f80b0,0x00007f1da19f8290] = 480
 constants      [0x00007f1da19f82a0,0x00007f1da19f82c0] = 32
 main code      [0x00007f1da19f82c0,0x00007f1da19f9980] = 5824
 stub code      [0x00007f1da19f9980,0x00007f1da19f9a40] = 192
 oops           [0x00007f1da19f9a40,0x00007f1da19f9b30] = 240
 scopes data    [0x00007f1da19f9b30,0x00007f1da19fa3d0] = 2208
 scopes pcs     [0x00007f1da19fa3d0,0x00007f1da19fa690] = 704
 dependencies   [0x00007f1da19fa690,0x00007f1da19fa6a8] = 24
 handler table  [0x00007f1da19fa6a8,0x00007f1da19fa8d0] = 552
 nul chk table  [0x00007f1da19fa8d0,0x00007f1da19fa8f8] = 40
Compiled method (c2) 191914528 4305  s!         java.text.Collator::getInstance (157 bytes)
 total in heap  [0x00007f1da19f7f90,0x00007f1da19fa8f8] = 10600
 relocation     [0x00007f1da19f80b0,0x00007f1da19f8290] = 480
 constants      [0x00007f1da19f82a0,0x00007f1da19f82c0] = 32
 main code      [0x00007f1da19f82c0,0x00007f1da19f9980] = 5824
 stub code      [0x00007f1da19f9980,0x00007f1da19f9a40] = 192
 oops           [0x00007f1da19f9a40,0x00007f1da19f9b30] = 240
 scopes data    [0x00007f1da19f9b30,0x00007f1da19fa3d0] = 2208
 scopes pcs     [0x00007f1da19fa3d0,0x00007f1da19fa690] = 704
 dependencies   [0x00007f1da19fa690,0x00007f1da19fa6a8] = 24
 handler table  [0x00007f1da19fa6a8,0x00007f1da19fa8d0] = 552
 nul chk table  [0x00007f1da19fa8d0,0x00007f1da19fa8f8] = 40
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#

2 个答案:

答案 0 :(得分:0)

在调用Collat​​or.getInstance(locale)之前,您必须先检查区域设置;

检查它是否为null,具有无效的length()或有效的语言环境。尝试调试应用程序。这会给你一些关于那里发生的事情的建议。

显然,您对compareIgnoreCase引发了异常(Nullpointer),但我建议调试您的应用程序,因为这是由于您的检查中的代码失败或者可能是在Collat​​or.getInstance()期间......

答案 1 :(得分:0)

看来这个bug正在使JVM发生段错误。如果您遇到类似问题,可以查看How do I debug Segfaults occurring in the JVM when it runs my code?