-7

I'm wondering how to get a reference to Object inside this class. I have PDFBean class with getter and setter methods, inside this class is a method createPDF(PDFBean pdf) and I want to use syntax like this createPDF(this) it is possible?

Class with title, fileName, Description fields:

    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();
        }
    }

I tried to use this syntax but I get an exception NPE

createPDF():

    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;
}

OfferFactory:

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......
insict
  • 861
  • 2
  • 11
  • 19

1 Answers1

2

If this refers to a PDFBean than you can invoke the createPDF function with the this keyword.

this is a java keyword that means use the current instance of the referenced object, where the referenced object is the current class.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151