ClassCastException: java.lang.String 不能转换为 Ljava.lang.String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29231288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:36:57  来源:igfitidea点击:

ClassCastException: java.lang.String cannot be cast to Ljava.lang.String

javaarraysstringcasting

提问by Shari

I get this error->

我收到此错误->

java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String; 

From the code pasted below.

从下面粘贴的代码。

public class LoginAttemps extends Setup {
    public void testSearchCountry() throws Exception {
        driver.get("http://www.wikipedia.org/wiki/Main_Page");
        ReadExcelDemo readXls = new ReadExcelDemo();
         List dataList = readXls.getData();
         for (int i = 1; i < dataList.size(); i++) {
             String[] testCase = new String[5];
             String[] test = (String[]) dataList.get(i);
             String countryName = test[0];
             String countryDesc = test[1];
             driver.findElement(By.id("searchInput")).clear();
             driver.findElement(By.id("searchInput")).sendKeys(countryName);
             driver.findElement(By.id("searchButton")).click();
             String str = driver.findElement(
             By.xpath("//h1[@id='firstHeading']/span")).getText();
             System.out.println(countryDesc);
             Assert.assertTrue(str.contains(countryName));
         }
   }
}

I think the issue is with String[] test = (String[]) dataList.get(i);

我认为问题在于 String[] test = (String[]) dataList.get(i);

But I am not sure about resolving this exception.. Any clues?

但我不确定解决这个异常..有什么线索吗?

回答by GhostCat

You can not cast "String" into "Array of Strings".

您不能将“字符串”转换为“字符串数组”。

You can only put a string into a slot within an array.

您只能将字符串放入数组中的插槽中。

What you can do is:

你可以做的是:

    String theString = "whatever";
    String[] myStrings = { theString };

回答by Sasha

Looking at the code, I believe you trying to convert List to an Array, therefore your "issue line" should be the following:

查看代码,我相信您尝试将List转换为 Array,因此您的“问题行”应如下所示:

String[] test = (String[]) dataList.toArray(new String[dataList.size]);

回答by gautam

Cause: element at "i" position is of type string. However you are trying to cast it into array of string.

原因:“i”位置的元素是字符串类型。但是,您正在尝试将其转换为字符串数组。

Direct Solution: remove [] from both sides i.e. change from:

直接解决方案:从两侧删除 [] 即从:

String[] test = (String[]) dataList.get(i);

To:

到:

String test = (String) dataList.get(i);

This will work if your List will not contain array of any type.

如果您的 List 不包含任何类型的数组,这将起作用。

回答by Shari

thankyou Guys. Yeah I am trying to cast a List Objects to String Arrays. I found the issues. Corrected code.

谢谢你们。是的,我正在尝试将列表对象转换为字符串数组。我发现了问题。更正的代码。

    public void testSearchCountry() throws Exception {
        driver.get("http://www.wikipedia.org/wiki/Main_Page");
        ReadExcelDemo readXls = new ReadExcelDemo();
         List dataList = readXls.getData();
        String[] test = new String[dataList.size()];
         for (int i = 1; i < dataList.size(); i++) {
             String[] testCase = new String[5];
             test[i] = dataList.get(i).toString();
             String countryName = test[0];
             String countryDesc = test[1];
             driver.findElement(By.id("searchInput")).clear();
             driver.findElement(By.id("searchInput")).sendKeys(countryName);
             driver.findElement(By.id("searchButton")).click();
             String str = driver.findElement(
             By.xpath("//h1[@id='firstHeading']/span")).getText();
             System.out.println(countryDesc);
             Assert.assertTrue(str.contains(countryName));
         }
   }

and it worked.

它奏效了。