java Workbook' is abstract; cannot be instantiated

351 Views Asked by At

I was using org.apache.poi.xssf.usermodel.XSSFWorkbook im my export implementation and after SonarQube warns "Use 'org.apache.poi.ss.usermodel.Workbook' here; it is a more general type than 'XSSFWorkbook'.", I switched to org.apache.poi.ss.usermodel.Workbook.

However, as Workbook is an abstract class, IntelliJ gives "'Workbook' is abstract; cannot be instantiated" error. So, how should I fix the problem in the following class?

public class WorkSheet {

    private Workbook workbook;
    private Sheet sheet;
    private AtomicInteger rowCount;

    public WorkSheet(String title) {
        this.workbook = new Workbook(); // <-- here is the problem
        this.sheet = workbook.createSheet(title);
        this.rowCount = new AtomicInteger(0);
    }

    public Workbook getWorkbook() {
        return workbook;
    }

    public Sheet getSheet() {
        return sheet;
    }

    public AtomicInteger getRowCount() {
        return rowCount;
    }
}
1

There are 1 best solutions below

0
On

“java Workbook' is abstract; cannot be instantiated”

Use Workbook as your variable type but create an instance of XSSFWorkbook:

instead of:
Workbook workbook = new Workbook();


use this:

this.workbook = new XSSFWorkbook();