1

I have two classes of managed beans and use @ManagedProperty to try to access the second one but I get NullPointerException every time. What is the problem here?

@ManagedBean
@SessionScoped
public class EventCreateEditModel implements Serializable {
    @ManagedProperty("#{eventCreateEditCostModel}")
    private EventCreateEditCostModel eventCreateEditCostModel;

    public void update() {
        eventCreateEditCostModel.update();
    }
    public void setEventCreateEditCostModel(final EventCreateEditCostModel eventCreateEditCostModel) {
        this.eventCreateEditCostModel = eventCreateEditCostModel;
    }

    public EventCreateEditCostModel getEventCreateEditCostModel() {
        return eventCreateEditCostModel;
    }
}

Here is my second class.

@ManagedBean
@SessionScoped
public class EventCreateEditCostModel implements Serializable {
    public void update() {
        System.out.println("IT works");
    }
}

I try to get the first class to call the second class update using @ManagedProperty but all I get is NullPointerException, when I try to access the update from the first class. I do have setters and getters for the @ManagedProperty.

Here is the stack trace which points to the update method in class EventCreateEditModel (tried to paste the whole stack but this system will not format it so here are the key parts)

    `Dec 24, 2015 2:02:57 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
    WARNING: java.lang.NullPointerException
    javax.el.ELException: java.lang.NullPointerException
at com.sun.el.parser.AstValue.invoke(AstValue.java:238)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    Caused by: java.lang.NullPointerException
at com.rem40.controller.EventCreateEditModel.update       (EventCreateEditModel.java:116)
... 35 more

Dec 24, 2015 2:02:57 PM com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError SEVERE: java.lang.NullPointerException at com.rem40.controller.EventCreateEditModel.update(EventCreateEditModel.java:116) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)`

snafua
  • 75
  • 1
  • 14
  • Please include the exact stack trace, and indicate which line it points to. Also, show us how you use the code you have here. I don't see anything in this code that would by itself create an NPE. – The Guy with The Hat Dec 24 '15 at 05:01
  • please show import statement of `@SessionScoped – Mahendran Ayyarsamy Kandiar Dec 24 '15 at 12:16
  • The imports are import javax.annotation.ManagedBean; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedProperty; import javax.faces.bean.SessionScoped; – snafua Dec 24 '15 at 19:01
  • 2
    `javax.annotation.ManagedBean` is not from JSF. – BalusC Dec 25 '15 at 08:24
  • I changed it to javax.faces.bean.ManagedBean; but I am still getting a NPE. I debugged and the eventCreateEditCostModel is null. The ManagedProperty is not injecting the bean. any ideas as to why? – snafua Dec 28 '15 at 16:42

0 Answers0