6

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?

0

4 Answers 4

9

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 };
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.