3

It looks like the dependency is not beeing injected, is there anything wrong with my code?

Page

<p:dataTable styleClass="centralizado" id="cdt" var="c" value="#{indexBean.getCampanhas()}">

IndexBean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;

@ManagedBean
@ViewScoped
public class IndexBean {
    @Inject
    private GerenciaContas contas;
    @Inject
    private GerenciaCampanhas campanhas;
    @Inject
    private Conversor conv;

    public List<Campaign> getCampanhas() throws Exception {
        return campanhas.getTodasCampanhas();
    }

    public GerenciaContas getContas() {
        return contas;
    }

    public Conversor getConversor() {
        return conv;
    }

}

One of the classes I use:

public class GerenciaCampanhas 
{
    public List<Campaign> getTodasCampanhas() throws Exception {
              //ordinary code
    }
}

Am I missing any Annotation in my Conversor class ? I am getting a NullPointerException because "GerenciaCampanhas campanhas" is null then calling the method getCampanhas() in my indexBean Thanks

fredcrs
  • 3,558
  • 7
  • 33
  • 55
  • Which are the packages of `@ManagedBean` and `@ViewScoped`? – SJuan76 Jan 17 '13 at 14:02
  • import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; – fredcrs Jan 17 '13 at 14:07
  • 3
    What is `GerenciaCampanhas` exactly (CDI, EJB, Spring, ManagedBean)? If you use CDI, you need an empty file named `beans.xml` in your META-INF folder in order to enable CDI. – atamanroman Jan 17 '13 at 14:52

1 Answers1

-1
  1. Replace @ManagedBean by @Named - you can't use @Inject in JSF managed beans (see this question
  2. Choose other scope than @ViewScoped - there's no such scope for CDI managed beans;
  3. Make sure you have the beans.xml file in your ejb's META-INF folder (if it's an enterprise project, it goes under the META-INF in your ejb project and under WEB-INF in your web project).

Like:

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import javax.inject.Inject;
import xxxx.GerenciaContas;

@Named
@SessionScoped
public class IndexBean implements Serializable {
    @Inject
    private GerenciaContas contas;

If you still need to use @ViewScoped add MyFaces CODI extensions to you project as explained in this question. Greetings from Brazil =).

Community
  • 1
  • 1
kauedg
  • 827
  • 8
  • 20
  • -1: you're wrong. You can inject EJB's in JSF 2 just by using the `@EJB` annotation before declaring the EJB attribute. No need to use CDI at all. – Luiggi Mendoza Jan 17 '13 at 16:44
  • 1
    As stated from the question, we're not sure if OP really wanted to use `@Inject` or if he/she's in learning phase and doing some experiments. Another option could be to convert the `GerenciaCampanhas` (and the other business logic classes) into EJB's by adding the `@Stateless` annotation to them, with this the `@ManagedBean` and `@ViewScoped` annotations will remain in the managed bean definition. – Luiggi Mendoza Jan 17 '13 at 16:48
  • Ok @LuiggiMendonza, I'm a newbie here and I'm still learning to answer questions. I've changed item 1 text and added a question on this. OP marked as answered so I understand the answer fit his needs. Thanks for the feedback. – kauedg Jan 17 '13 at 16:56
  • 2
    Point 2 is strange. EJBs does not support any of those JSF/CDI scopes anyway. Aren't you mixing EJBs (`@Stateless`/`@Stateful` classes) with CDI managed beans (`@Named` classes)? – BalusC Jan 17 '13 at 17:29
  • You're right @BalusC, sometimes I mix up definitions. Edited the answer. – kauedg Jan 17 '13 at 17:33