2015-11-03 72 views
0

有人能让我知道下面的代码有什么问题吗? 我正试图对存储在“Options”变量中的数组进行排序。但它会抛出'空'异常。排序阵列不起作用

public static void CheckOptionsPresent(String s1) throws Exception 
{ 
    try 
    { 
     List optionsList=null;  
     webDriver.findElement(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance button")).click(); 
     int listcount = webDriver.findElements(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance div ul li")).size(); 
     System.out.println(listcount); 
     String[] options=new String[listcount]; 

     for (int i=2; i<=listcount; i++) 
     { 
      options[i-1] = webDriver.findElement(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance div ul li:nth-child("+i+") a span")).getText(); 
      System.out.println(options[i-1]); 
     } 

     System.out.println(options.length); 

     for(int j=0; j<options.length;j++) 
     { 
      for (int i=j+1 ; i<options.length; i++) 
      { 
       if(options[i].compareToIgnoreCase(options[j])<0) 
       { 
       String temp= options[j]; 
       options[j]= options[i]; 
       options[i]=temp; 


       } 
      } 

      System.out.println(options[j]); 
     } 
    } 
    catch (RuntimeException e) 
    { 
     System.out.println(e.getMessage()); 
     throw new RuntimeException(e.getMessage()); 
    } 
} 

输出:

14 
AB - Alberta 
BC - British Columbia 
MB - Manitoba 
NB - New Brunswick 
NL - Newfoundland and Labrador 
NS - Nova Scotia 
NT - Northwest Territories 
NU - Nunavut 
ON - Ontario 
PE - Prince Edward Island 
QC - Québec 
SK - Saskatchewan 
YT - Yukon Territory 
14 

下面是错误:

org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array. 

for (int i=j+1 ; i<options.length; i++) { 
    if(options[i].compareToIgnoreCase(options[j])<0) { 
     String temp= options[j]; options[j]= options[i]; 
     options[i]=temp; 
    } 
} 
+1

后的错误消息,并在那里发生的事情 –

+1

其中后发生错误的行号或张贴堆栈跟踪 – gvmani

+4

为什么以书面形式代替不使用Collections.sort()你自己排序? – Tim

回答

0

String.compareToIgnoreCase()做不像null作为它的论据。

你的第一个循环后,options[0] = null;

一个NullPointerException当这个被打的if(options[i].compareToIgnoreCase(options[j])<0)第一遍那么索引(i = 1, j = 0)发生。

+0

是的。实际上,我正在为我们的项目写一个通用函数,并且所有下拉列表都包含一个空值。这就是为什么collections.sort(optionsList)不适合我的原因。你能不能让我知道如何从数组/列表中除去“optionsList.removeall(collections.singleton(null));”....“....感谢您的帮助。 – Rahul

+0

我认为一个更好的解决方案是使用(n)(Array)List而不是数组,并且只包含非空值,然后Collections.sort()应该可以正常工作。当你排序完成后,在第0个索引处插入一个空值。 – Thevenin

+0

感谢解决方案标记。我从朋友那里得到了另一个逻辑,它的工作正常。发布相同。 – Rahul

0

工作代码:

public static void CheckOptionsPresent(String s1) throws Exception { 
    try { 
     List <String> optionsList = null; 
     webDriver.findElement(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance button")).click(); 
     int listcount = webDriver.findElements(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance div ul li")).size(); 
     System.out.println(listcount); 
     String[] options = new String[listcount]; 

     for (int i = listcount; i >= 2; i--) { 
      options[i - 1] = webDriver.findElement(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance div ul li:nth-child(" + i + ") a span")).getText(); 
      System.out.println(options[i - 1]); 
      optionsList = Arrays.asList(options); 

     } 
     System.out.println(optionsList); 
     System.out.println(isSorted(optionsList)); 

    } catch (RuntimeException e) { 
     System.out.println(e.getMessage()); 
     throw new RuntimeException(e.getMessage()); 
    } 
} 
public static boolean isSorted(List <String> list) { 
    boolean sorted = true; 
    for (int i = 0; i < list.size() - 1; i++) { 
     if (list.get(i) == null) continue; 
     if (list.get(i).compareTo(list.get(i + 1)) > 0) sorted = false; 
    } 

    return sorted; 
}