有人可以告诉我以下代码有什么问题吗? 我试图对存储在“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<options.length; i++) {
if(options[i].compareToIgnoreCase(options[j])<0) {
String temp= options[j]; options[j]= options[i];
options[i]=temp;
}
}
答案 0 :(得分:0)
String.compareToIgnoreCase()
不喜欢null
作为其论点。
第一次循环后,options[0] = null;
NullPointerException
在第一次传递if(options[i].compareToIgnoreCase(options[j])<0)
时被点击,然后索引为(i = 1, j = 0)
,就会发生var array = new List<object>();
var offers = new List<object>();
offers.Add(new
{
ItemName = itnme,
Price = price,
Quantity = quant,
});
array.Add(new
{
Dealname = dealname,
Ticketcount = tictnum,
OriginalPrice = origpri,
Dealsticketcount = dealsticktnu,
dealprice = dp,
totalprice = totamnt,
Offers = offers
});
。
答案 1 :(得分: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;
}