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

29,129

Solution 1

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 };

Solution 2

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

String[] test = (String[]) dataList.toArray(new String[dataList.size]);
Share:
29,129
Shari
Author by

Shari

Updated on March 07, 2020

Comments

  • Shari
    Shari about 4 years

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

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