I'm trying to use the ManagedProperty
annotation to Dependency Injection to another ManagedBean, but I always getting a NullPointerException
. I will try to describe how my application works.
This is a PDF generator. First, I have xhtml
page that has a Tabbed Pane. In each tab I use <ui:include>
tag. All of these beans have ViewScope.
In first Tab I have title inputText
and description textArea
fields, and it's strange beacuse this fields are working well, even if I do not use DI.
My problem starts when I go to another Tab, and I want to add some additional information/text whatever.
My app works like this: I write some text to inputText field (1), then I press "Ready" (2) button that passes this
object to the factory, then I press "Print" (3) button to generate .pdf file. In this picture You can see PanelInneBean
and ContainerBean
areas.
Let me show my source:
@ManagedBean(name="inne")
@ViewScoped
public class PanelInneBean extends AbstractPanel implements Serializable {
private static final long serialVersionUID = 1L;
private final int CODE = 4;
private boolean use;
private String tytul, opis;
private PdfFactory pdf;
@PostConstruct
private void init() {
pdf = PdfFactory.getPdfObject();
use = false;
}
public PanelInneBean() {
}
public boolean getUse() {
return use;
}
public String getTytul() {
return tytul;
}
public void setTytul(String tytul) {
this.tytul = tytul;
}
public String getOpis() {
return opis;
}
public void setOpis(String opis) {
this.opis = opis;
}
public int getCode() {
return CODE;
}
private void add() {
use = true;
}
public void addBean() {
add();
pdf.addElement(this);
System.out.println("InnePanel after pdf.addElement() this.opis:" + this.opis);
}
}
Factory source:
@ManagedBean(name="factory")
@SessionScoped
public class PdfFactory implements Serializable {
//.............
private InneP inn = new InneP() //without new does not work too;
//.............
public int addElement(PdfElement element) {
pdfType = true;
if (element.getUse()) {
elementList.add(element);
return 1;
}
return 0;
}
//.............
public List<Element> getDocumentBody() throws DocumentException {
initSignature();
List<Element> list = new ArrayList<Element>();
list.add(createDocDate());
list.add(Chunk.NEWLINE);
for (PdfElement p : elementList) {
if (p.getUse()) {
switch (p.getCode()) {
//............
case 4:
list.addAll(inn.generatePharse());
break;
}
}
}
list.addAll(sF.generatePharse());
return list;
}
last InneP source (this class generate Paragraps to print) in this class i'm getting NPE:
@ManagedBean
@RequestScoped
public class InneP {
private List<Element> list = new ArrayList<Element>();
private Font font;
@ManagedProperty("#{inne}")
private PanelInneBean panelInneBean;
public InneP(){}
public InneP(Font font) {
this.font = font;
}
public void setPanelInneBean(PanelInneBean inne) {
this.panelInneBean = inne;
}
public List<Element> generatePharse() {
if (panelInneBean.getTytul().length() > 0) {
System.out.println("drukowanie tytulu");
list.add(createTitle(panelInneBean.getTytul()));
list.add(Chunk.NEWLINE);
}
if (panelInneBean.getOpis().length() > 0) {
list.add(createDescription(panelInneBean.getOpis()));
list.add(Chunk.NEWLINE);
}
return list;
}
private Paragraph createTitle(String tytul) {
System.out.println("inne tytul: " + tytul);
font.setStyle(Font.NORMAL);
Paragraph t = new Paragraph(tytul, font);
t.setAlignment(Element.ALIGN_LEFT);
return t;
}
private Paragraph createDescription(String opis) {
System.out.println("inne opis:" + opis);
font.setStyle(Font.BOLD);
Paragraph d = new Paragraph(opis, font);
d.setAlignment(Element.ALIGN_LEFT);
return d;
}
}
The print button just adds the list to the for
loop and print all Paragraphs; I use iText lib and OpenFaces to generate Tabbed Pane.
I hope that I wrote enough information.