0

I want to read values like 11610.1073 from excel using java and selenium. I tried below code but it throws me null pointer exception when I am trying to parse that value and store it in array variable. I am able to read int values but unable to do the same with the float values. Below is my code:

StagePTDFTE.add(Float.parseFloat(row.getCell(2).getStringCellValue().toString()));
StagePTDFTEArray = new float[StagePTDFTE.size()];
    for (int i = 0; i < StagePTDFTEArray.length; i++) {
        StagePTDFTEArray[i] = Float.parseFloat(df.format(StagePTDFTE.get(i)));
            }   

Please let me know where I am going wrong and what should be the modifications required here. Thanks in advance :)

  • 1
    [Row.getCell](https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Row.html#getCell-int-) may return `null`. So, as for each [NullPointerException](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it), you have to check if it returns `null` and do only using it if not. See https://poi.apache.org/spreadsheet/quick-guide.html#Iterate+over+cells%2C+with+control+of+missing+%2F+blank+cells. – Axel Richter Jan 05 '18 at 06:14

1 Answers1

1

Try using Data DataFormatter

All you need to do this:

// Only need one of these
DataFormatter fmt = new DataFormatter();

// Once per cell
String valueAsSeenInExcel = fmt.formatCellValue(cell);
Siddhesh
  • 1,095
  • 1
  • 11
  • 23
  • Yeah it works but the problem is if i want to read multiple values of that column then this doesnt work. It only reads last value of that particular column. So how can it be achieved ? –  Jan 05 '18 at 09:22
  • i sorted it out with the below code. Thanks so much @Siddhesh . –  Jan 05 '18 at 11:05
  • DataFormatter fmt = new DataFormatter(); overdueList.add(fmt.formatCellValue(row.getCell(4))); //Where //overdueList is String arraylist for (int i = 0; i < overdueList.size(); i++) { System.out.println("Values are " + overdueList.get(i).replace("%", "")); } –  Jan 05 '18 at 11:06