Cannot get a STRING value from a NUMERIC cell

3.2k views Asked by At

I am trying to enter Postal Code (V9G1Y3) in my selenium script from excel but it is throwing error. Code is as follows-

cell = sheet.getRow(i).getCell(3);
driver.findElement(postalCode).sendKeys(cell.getStringCellValue());

Below error message is received -

    java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC 
    cell
    at org.apache.poi.hssf.usermodel.HSSFCell.typeMismatch(HSSFCell.java:654)
    at 
 org.apache.poi.hssf.usermodel.HSSFCell.getRichStringCellValue(HSSFCell.java:731)
    at org.apache.poi.hssf.usermodel.HSSFCell.getStringCellValue(HSSFCell.java:714)
    at sweta.NewTest.ReadData(NewTest.java:97)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

The application is not receiving the Postal Code.

Any suggestions from anyone?

2

There are 2 answers

0
Suresh On

From the error, I understand that you are trying to fetch the pincode value from excel sheet using cell.getStringCellValue() where the cell type is not text or General but number.

I recommend you to format the cell holding the pincode value to General and that will resolve the error.

enter image description here

1
Austin7L On

You might use setCellType to String then you can get the String values.