简体   繁体   中英

Stored the values in list and then sorting them using Collections is not working

Am trying to store the string values in a list using the variable 'optionsList'. But it's throwing NullPointer exception.Can some one please help me in resolving this.

Below is the code:

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

           webDriver.findElement(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance button")).click();

           List<WebElement> list = webDriver.findElements(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance div ul li"));

           int listcount = list.size();           
           System.out.println(listcount);
           String options[]=new String[listcount];


           for (int i=3; i<=listcount; i++ )
           {

           options[i-3] = 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-3]);
                   webDriver.findElement(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance div ul li:nth-child("+i+") a span")));

           optionsList = Arrays.asList(options);

           }

           System.out.println(optionsList);

           optionsList.removeAll(Collections.singleton(null));


           Collections.sort(optionsList);


           System.out.println("Sorted List:" + optionsList);

}

这是String的列表。您应该使用Collections.sort(optionsList,String.CASE_INSENSITIVE_ORDER)

The below is a simplified version of yours above, the below works fine.

  public static void main(String[] args) {
        List optionsList = null;
        String options[] = new String[3];// listcount];
        options[0] = "cat";
        options[1] = "dog";
        options[2] = "alpha";
        optionsList = Arrays.asList(options);
        System.out.println(optionsList);
        optionsList.removeAll(Collections.singleton(null));
        Collections.sort(optionsList);
        System.out.println("Sorted List:" + optionsList);

    }

I think the issue is in the way you are reading values for options[].

try narrowing down the issue I am pretty sure it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM