[英]Stored the values in list and then sorting them using Collections is not working
我正在尝试使用变量'optionsList'将字符串值存储在列表中。 但是它抛出了NullPointer异常,请有人帮我解决这个问题。
下面是代码:
公共静态无效CheckOptionsPresent(String s1)引发异常{试试{
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)
下面是您上面的简化版本,下面可以正常工作。
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);
}
我认为问题在于您正在读取options []的值。
尝试缩小问题的范围,我很确定它会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.