2015-11-03 70 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; 
}