Reputation: 3
so I need to print out the product id and image url from a excel file as I need to modify another file with the contents I retrieve, however for some reason the mappings is empty, but if I print the values out directly it shows them correctly.
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
public class Main {
static final Map<String, String> PRODUCT_CODES_BY_IMAGES = new HashMap<>();
public static void main(String[] args) throws IOException, InvalidFormatException {
File file = new File("./data/stock_met_imageurls.xlsx");
FileInputStream fis = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(fis);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(cell != null && cell.getCellTypeEnum().equals(CellType.STRING)) {
String image_url = null;
String product_id = null;
switch(cell.getColumnIndex()) {
case 0:
image_url = cell.getStringCellValue();
break;
case 2:
product_id = cell.getStringCellValue();
break;
}
if(image_url != null && product_id != null) {
PRODUCT_CODES_BY_IMAGES.put(product_id, image_url);
}
}
}
}
PRODUCT_CODES_BY_IMAGES.forEach((k, v) -> {
System.out.println("key = " + k);
System.out.println("val = " + v);
});
System.out.println("Size = " + PRODUCT_CODES_BY_IMAGES.size());
}
}
if I did under the cases
System.out.println("val = " + cell.getStringCellValue());
it prints it out correctly but for some reason the mappings is empty?
Any help would be greatly appreciated.
Upvotes: 0
Views: 61
Reputation: 3
Fixed, I added the elements in the mappings above the wrong bracket.
Upvotes: 0
Reputation: 735
Because your map is declared as static final. Check out https://www.baeldung.com/java-final
Upvotes: 1