我正在使用PDF Box API创建一个表格。当我尝试运行我的代码时,我得到了空指针异常。我正在做的是在不同级别(即单元格级别,行级别,表级别)设置表格的颜色边框宽度等属性。
这里的行,单元格,表是不同的类。
这意味着如果在单元格级别设置了边框宽度,则仅在该单元格上使用,如果在行级别设置了此属性,则它将设置整个行的边框宽度,其中未使用单元格边框宽度属性,并且在表级别也是如此。
优先级:单元格>行>表格
但是我的代码确实是当未使用setter在单元格级别设置该值时,因此该值显然将为0(在基本类型的情况下)。如果它是0,那么它将从行级别获取值。行的情况相同。
但是当我尝试获取从行继承的值时,我得到了Null Pointer Exception。并间接地来自表格。而且当我尝试获取get Row时也会得到Null Pointer异常,因为它返回的是null。
首先,我尝试使用if循环解决Null指针异常。但是结果还是一样。
错误消息:
Exception in thread "main" java.lang.NullPointerException
at main.Cell.getBorderWidth(Cell.java:66)
at main.CreateTable.createSampleDocument(CreateTable.java:54)
at main.CreateTable.main(CreateTable.java:16)
我提供的代码很少。
表类
public class Table {
private List<Row> rows = new LinkedList<>();
private float borderWidth = 0.2f;
private Table(final List<Row> rows) {
this.rows = rows;
}
public List<Row> getRows() {
return rows;
}
public float getTableWidth() {
return tableWidth;
}
public void setTableWidth(final float tableWidth) {
this.tableWidth = tableWidth;
}
public static class TableBuilder {
private final List<Row> rows = new LinkedList<>();
private Table table = new Table(rows, columns);
public TableBuilder addRow(Row row) {
row.setTable(table);
rows.add(row);
return this;
}
public TableBuilder setTableWidth(final float tableWidth) {
this.tableWidth = tableWidth;
this.table.setTableWidth(tableWidth);
return this;
}
public Table build() {
return table;
}
}
}
排级
public class Row {
private Table table;
private final List<Cell> cells;
private float borderWidth;
private Row(final List<Cell> cells) {
super();
this.cells = cells;
}
public List<Cell> getCells() {
return this.cells;
}
public Table getTable() {
return table;
}
void setTable(final Table table) {
this.table = table;
}
public float getBorderWidth() {
if(borderWidth == 0) {
System.out.println(getTable().getBorderWidth());
borderWidth = getTable().getBorderWidth();
}
return borderWidth;
}
public void setBorderWidth(float borderWidth) {
this.borderWidth = borderWidth;
}
public static class RowBuilder {
private final List<Cell> cells = new LinkedList<>();
Row row = new Row(cells);
public RowBuilder add(final Cell cell) {
cell.setRow(row);
cells.add(cell);
return this;
}
public RowBuilder setAllPadding(float allPadding) {
//this.allPadding = Optional.of(allPadding);
this.row.setAllPadding(allPadding);
return this;
}
public RowBuilder setBorderWidth(float borderWidth) {
/*this.borderWidth = Optional.of(borderWidth);
this.borderWidth.ifPresent(row::setBorderWidth);*/
this.row.setBorderWidth(borderWidth);
return this;
}
public Row build() {
return row;
}
}
}
单元格类
public class Cell {
private Row row;
private final String text;
private float borderWidth; //To be seen afterwards
private Cell(Object object) {
this.text = String.valueOf(object);
}
public static Cell addText(Object object) {
System.out.println(String.valueOf(object));
return new Cell(object);
}
public String getText() {
return text;
}
public Row getRow() {
return row;
}
public void setRow(final Row row) {
this.row = row;
}
public float getBorderWidth() {
//Optional<Float> optBorderWidth = Optional.ofNullable(borderWidth);
//return optBorderWidth.orElse(getRow().getBorderWidth());
if(borderWidth == 0) {
borderWidth = getRow().getTable().getBorderWidth();
}
return borderWidth;
}
public Cell setBorderWidth(float borderWidth) {
this.borderWidth = borderWidth;
return this;
}
}
编辑
我只是忘了告诉这个值是如何使用和设置在内存中的。
这是代码
public class CreateTable {
public static void main (String[] args) throws Exception {
new CreateTable().createSampleDocument();
}
public void createSampleDocument() throws Exception {
final Object[][] tableData = {
{"ent",0.8, 0.5},
{"AV",144, 51.7},
{"Scc",0.1, 0.5}
};
TableBuilder tableBuilder = new TableBuilder()
.setBorderWidth(0.2f)
tableBuilder.addRow(new RowBuilder()
.add(Cell.addText("P").withAllBorders().setBorderColor(Color.BLUE).setLineSpacing(2))
.add(Cell.addText("S").withAllBorders())
.add(Cell.addText("D").withAllBorders())
.setBorderColor(Color.RED).setLineSpacing(1)
.build());
final PDDocument document = new PDDocument();
final PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
final PDPageContentStream contentStream = new PDPageContentStream(document, page);
(new TableDrawer(contentStream, tableBuilder.build(), 300, 300)).draw();
contentStream.close();
document.save("C:\\Users\\sa\\Desktop\\table_pdf.pdf");
document.close();
}
}
在
处捕获到空指针异常内部单元格类(因为getRow()方法返回null)
public float getBorderWidth() {
//Optional<Float> optBorderWidth = Optional.ofNullable(borderWidth);
//return optBorderWidth.orElse(getRow().getBorderWidth());
if(borderWidth == 0) {
borderWidth = getRow().getTable().getBorderWidth();
}
return borderWidth;
}