Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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?

share|improve this question
    
Reordered for clarity, formatted code simplified title – Eru Penkman Mar 24 at 12:12

4 Answers 4

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 };
share|improve this answer

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

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.

share|improve this answer

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.

share|improve this answer

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|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.