2014-07-01 91 views
0

我嘗試執行下面粘貼的webdriver代碼,其中username字段是String,付款字段類型是double。它的執行錯誤消息粘貼在下面。Webdriver例外:Double不能轉換爲java.lang.String

可我知道如何從這個

錯誤克服:

Exception in thread "main" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String

webdriver的Java代碼:

package exceltest; 

import java.io.File; 
import java.io.FileInputStream; 

import org.apache.poi.xssf.usermodel.XSSFCell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 



public class sample { 

    public static void main(String[] args) throws Exception { 
     String [][] data; 
     data = excelread(); 

     String expectedtitle; 
     for (int i = 1; i < data.length; i++) 
     { 

     expectedtitle = Paymentdetails(data[i][0],data[i][1]); 

     System.out.println("page title after login is" + expectedtitle); 

     if(expectedtitle.equalsIgnoreCase("homepage Title")) 
     { 

      System.out.println("PASSED"); 
     } 

     else{ 
      System.out.println("FAILED"); 

      } 

     } 
    } 







    public static String Paymentdetails(String username,String payment) throws InterruptedException { 
     WebDriver driver = new FirefoxDriver(); 

     driver.get("http://localhost/xxx/Default.aspx"); 

     Thread.sleep(1000); 
      //driver.findElement(By.id("LoginUserName")).clear(); 
      driver.findElement(By.id("LoginUserName")).sendKeys(username); 
      //driver.findElement(By.id("LoginPayment")).clear(); 


      driver.findElement(By.id("LoginPayment")).sendKeys(payment); 
      driver.findElement(By.id("LoginLoginButton")).click(); 
      Thread.sleep(1000); 
      String expectedtitle = driver.getTitle(); 
      return expectedtitle; 

      } 








    public static String [][] excelread()throws Exception 
    { 
     File excel = new File("D:\\Book1.xlsx"); 
     FileInputStream fis = new FileInputStream(excel); 
     XSSFWorkbook wb = new XSSFWorkbook(fis); 
     XSSFSheet ws = wb.getSheet("Sheet1"); 

     int totrow = ws.getLastRowNum()+ 1; 
     int totcol = ws.getRow(0).getLastCellNum(); 
     String [][] data = new String[totrow][totcol]; 

     for (int i = 0 ; i < totrow ; i++) { 
      XSSFRow row = ws.getRow(i); 
      for (int j=0 ; j < totcol ; j++){ 
      XSSFCell cell = row.getCell(j); 
      String value = cellToString(cell); 
      data[i][j] = value; 
       System.out.println("The value is " + value); 



      } 
    } 
     return data; 
     } 


    public static String cellToString(XSSFCell cell) { 
     int type; 
     Object result ; 
     type = cell.getCellType(); 
     switch (type) { 
     case 0 : 
     result = cell.getNumericCellValue(); 
     break; 
     case 1 : 
     result = cell.getStringCellValue(); 
     break; 
     default : 
     throw new RuntimeException("There are no support for this type of cell"); 
     } 
     return (String) result(); 
     } 
    } 
+0

什麼語句你得到這個例外? – Shail016

+0

@ Shail016在excelread(),celltostring()方法 – Arun

回答

2

爲您現有的解決方案試試這個,

在cellToString

()代替return (String) result();return result.toString();,它會工作。

目前您正試圖將java.lang.Double投射到java.lang.String, when you assign a primitive to Object reference the compiler implicitly box it into its wrapper class。爲了測試這個執行下面的測試案例:

public static void main(String[] args) { 
    Object o = null; 
    double d = 10d; 
    o = d; 
    if (o instanceof Double) { 
     System.out.println("instance of java.lang.Double"); 
    } else { 
     System.out.println("not an instance of Double"); 
    } 
    String dStr = o.toString(); 
    System.out.println(dStr); 
} 

這就是爲什麼結果變成java.lang.Double,您可以直接不投串,而你應該返回其字符串值。

逸岸你cellToString()應該是這樣的:

public static String cellToString(XSSFCell cell) { 
    String result = null; 
    int type = cell.getCellType(); 
    switch (type) { 
    case 0: 
     result = String.valueOf(cell.getNumericCellValue()); 
     break; 
    case 1: 
     result = cell.getStringCellValue(); 
     break; 
    default: 
     throw new RuntimeException(
       "There are no support for this type of cell"); 
    } 
    return result; 
} 
+0

謝謝你這麼多.... replace return(String)result();與返回result.toString()完美的工作 – Arun

1

我認爲這個問題是,方法getNumericCellValue返回類型爲雙(不是班級雙人)。沒有方法toString()。

你可以試試:

result = cell.getNumericCellValue(); 

變化:

result = String.valueOf(cell.getNumericCellValue()); 
+0

您好,爲答覆,用戶名字段s的嚴格和付款領域s雙重 – Arun

+0

類型不匹配:不能轉換從對象到字符串 – Arun

+0

可以給我一些其他的想法 – Arun

相關問題