Java POI not setting previous cell hyperlink

I’m trying to read in from a text file, and when the line contains a link, I set the previous cell’s hyperlink to such link:

while (scanner.hasNextLine()) {

    String nextToken = scanner.nextLine();
    if (nextToken.startsWith("<")) {

        temp = Jsoup.parse(nextToken);
        url = temp.select("a").first();
        link.setAddress(url.attr("href"));
        System.out.println(link.getAddress());
        prevCol = currRow.getCell(col - 1);
        System.out.println(row + ", " + col -1);
        prevCol.setHyperlink(link);

    } else {
        currCol.setCellValue(nextToken);
        col++;
        currCol = currRow.createCell(col);
    }

    if (nextToken.isEmpty()) {
        row++;
        col = 0;
        currRow = sheet.createRow(row);
        currCol = currRow.createCell(col);
    }

}

I’m printing every link and cell coordinates to the console to make sure the links are being set at the cell they should be, and they are. But my issue is only the last cell in all of the data gets a hyperlink attached to it. Any ideas why?

Full code for those who find it more helpful:

public static void main(String[] args) throws EncryptedDocumentException, InvalidFormatException, IOException {
    File file = new File("C:\Users\Jester\Desktop\data scrap payday\finishedformat.txt");
    FileInputStream fis = new FileInputStream(
            new File("C:\Users\Jester\Desktop\data scrap payday\Payday 2 Rewards.xlsx"));
    XSSFWorkbook workbook = new XSSFWorkbook(fis);
    CreationHelper createHelper = workbook.getCreationHelper();
    XSSFSheet sheet = workbook.createSheet("Achievement Rewards");
    Document temp;
    Element url;
    Scanner scanner = new Scanner(file, "UTF-8");
    XSSFHyperlink link = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
    int row = 0;
    int col = 0;
    XSSFRow currRow = sheet.createRow(row);
    XSSFCell currCol = currRow.createCell(col);
    XSSFCell prevCol;

    XSSFCellStyle hlinkstyle = workbook.createCellStyle();
    XSSFFont hlinkfont = workbook.createFont();
    hlinkfont.setUnderline(XSSFFont.U_SINGLE);
    hlinkfont.setColor(HSSFColor.BLUE.index);
    hlinkstyle.setFont(hlinkfont);
    while (scanner.hasNextLine()) {

        String nextToken = scanner.nextLine();
        if (nextToken.startsWith("<")) {

            temp = Jsoup.parse(nextToken);
            url = temp.select("a").first();
            link.setAddress(url.attr("href"));
            System.out.println(link.getAddress());
            prevCol = currRow.getCell(col - 1);
            System.out.println(row + ", " + col);
            prevCol.setHyperlink(link);
            prevCol.setCellStyle(hlinkstyle);
            System.out.println(prevCol.getHyperlink().getAddress()); //This is returning the desired link to the console too, sooooo....

        } else {
            currCol.setCellValue(nextToken);
            col++;
            currCol = currRow.createCell(col);
        }

        if (nextToken.isEmpty()) {
            row++;
            col = 0;
            currRow = sheet.createRow(row);
            currCol = currRow.createCell(col);
        }

    }

    fis.close();
    FileOutputStream fos = new FileOutputStream(
            new File("C:\Users\Jester\Desktop\data scrap payday\Payday 2 Rewards.xlsx"));
    workbook.write(fos);
    fos.close();

}

Input format is like such:

Hail to the King, Baby
In the Golden Grin Casino heist, kill "The King" and complete the heist in stealth
<a href="http://payday.wikia.com/wiki/Golden_Grin_Casino" title="Golden Grin Casino">Golden Grin Casino</a>
"Sports Utility Mask" mask
<a href="http://payday.wikia.com/wiki/Masks_(Payday_2)#The_Golden_Grin_Casino_Heist_DLC" title="Masks (Payday 2)">Sports Utility Mask</a>
"Carpet" material
<a href="http://payday.wikia.com/wiki/Materials#The_Golden_Grin_Casino_Heist_DLC" title="Materials">Carpet</a>
"Dices" pattern
<a href="http://payday.wikia.com/wiki/Patterns#The_Golden_Grin_Casino_Heist_DLC" title="Patterns">Dices</a>

Answer

This is a scope issue. Move the link object creation inside the if statement.

            if (nextToken.startsWith("<")) {
            XSSFHyperlink link = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
            temp = Jsoup.parse(nextToken);
            url = temp.select("a").first();
            link.setAddress(url.attr("href"));
            System.out.println(link.getAddress());
            prevCol = currRow.getCell(col - 1);
            System.out.println(row + ", " + col);
            prevCol.setHyperlink(link);
            prevCol.setCellStyle(hlinkstyle);
            System.out.println(prevCol.getHyperlink().getAddress()); //This is returning the desired link to the console too, sooooo.

Leave a Reply

Your email address will not be published. Required fields are marked *