Java 排序数组不起作用

Java 排序数组不起作用,java,arrays,sorting,Java,Arrays,Sorting,有人能告诉我下面的代码有什么问题吗? 我正在尝试对存储在“Options”变量中的数组进行排序。但它抛出“Null”异常 public static void CheckOptionsPresent(String s1) throws Exception { try { List optionsList=null; webDriver.findElement(By.cssSelector("article.ContactInfo.acti

有人能告诉我下面的代码有什么问题吗? 我正在尝试对存储在“Options”变量中的数组进行排序。但它抛出“Null”异常

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
String.compareTognoreCase()
不喜欢
null
作为其参数

在第一次循环之后,
options[0]=null;

当在第一次执行
if(选项[i].compareTignoreCase(选项[j])工作代码时,如果遇到此异常,则会发生
NullPointerException

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;
}
public static void checkoptions present(字符串s1)引发异常{
试一试{
列表<字符串>选项列表=空;
webDriver.findElement(通过.css选择器(“article.ContactInfo.active div.half-left.ContactInfo div.idprovince of issue按钮”)。单击();
int listcount=webDriver.findElements(By.css选择器(“article.ContactInfo.active div.half-left.ContactInfo div.idprovisionofissue div ul li”)).size();
System.out.println(列表计数);
字符串[]选项=新字符串[listcount];
对于(int i=listcount;i>=2;i--){
选项[i-1]=webDriver.findElement(By.cssSelector(“article.ContactInfo.active div.half-left.ContactInfo div.idprovisionofissue div ul:n子级(“+i+”)a span”)。getText();
System.out.println(选项[i-1]);
optionsList=Arrays.asList(选项);
}
系统输出打印项次(选项列表);
System.out.println(isSorted(选项列表));
}捕获(运行时异常e){
System.out.println(e.getMessage());
抛出新的RuntimeException(例如getMessage());
}
}
公共静态布尔值已排序(列表<字符串>列表){
布尔排序=真;
对于(int i=0;i0)sorted=false;
}
返回排序;
}

发布错误消息及其发生位置发布错误发生的行号或发布堆栈跟踪为什么不使用Collections.sort()以下是错误:org.eclipse.debug.core.DebugException:com.sun.jdi.ClassNotLoadedException:检索数组的组件类型时未加载类型。for(int i=j+1;iIt不会进入上面的for循环。是的,标记。实际上,我正在为我们的项目编写一个泛型函数,所有下拉列表都包含一个空值。这就是为什么连collections.sort(optionList)对我都不起作用的原因。请告诉我如何从数组/列表中删除空值,而不是“optionsList.removeall(collections.singleton(null));”…感谢您的帮助。我认为更好的解决方案是使用(n)(数组)列表而不是数组,并且只包含非null值,然后是collections.sort()应该可以正常工作。当你对所有内容进行排序后,在第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;
}