我想知道如何在此类中获取对 Object 的引用。我有带有 getter 和 setter 方法的 PDFBean 类,在这个类里面是一个方法createPDF(PDFBean pdf)
,我想使用这样的语法createPDF(this)
有可能吗?
具有标题、文件名、描述字段的类:
public void print() {
PrintToPDF pdf = new PrintToPDF();
try {
if(order.isEmpty())
System.out.println("PDFBean.print() <--\norderisEmpty!");
pdf.createPdf(this);
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
我尝试使用此语法,但出现异常 NPE
创建PDF():
public int createPdf(PDFBean pdf) throws IOException, DocumentException {
OfferFactory of = new OfferFactory(pdf);
Document doc = new Document(PageSize.A4, 34, 34, 110, 45);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(
RESULT));
HeaderFooter event = new HeaderFooter();
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
writer.setPageEvent(event);
doc.open();
doc.add(createTitle(of.getTitle()));
doc.add(createDescription(of.getDescription()));
doc.add(createTable(of.getOrderList()));
doc.add(createSum(of.getSum(), of.getDiscount()));
doc.close();
return 0;
}
报价工厂:
private List<OrderLine> orderList;
private String fileName, title, description;
private Double sum, discount;
public OfferFactory(PDFBean pdf) {
if(pdf == null) {
throw new IllegalStateException("OfferFactory -> pdf is null");
}
orderList.addAll(pdf.getOrder());
fileName = pdf.getFileName();
title = pdf.getTitle();
description = pdf.getOpis();
sum = pdf.getSuma();
discount = pdf.getDiscount();
}
//getters......