1
votes

I new to Java and JSf, am getting null pointer error when accessing a getter value from backing bean in controller. Here is the code snippet :

JSP; input text area and am able see the sysout in setter the value submitted from page and not able to access the getter values. faces-config.xml

<managed-bean>
        <managed-bean-name>researchHisttoryController</managed-bean-name>
        <managed-bean-class>com.controller.ResearchHisttoryController</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
            <property-name>history</property-name>
            <property-class>com.researchhistory.model.ShipmentHistory</property-class>
            <value>#{history}</value>
        </managed-property>
    </managed-bean>
    <managed-bean>
Controller class:

private ShipmentHistory history;

//getters and setter followed;

am accessing varaible from ShipmentHistory class as getHistory.getTrackNumber; NPE error

ShipmentHistory.java
private String trackNumber;
//getters and setters 

can please help me out where am doing wrong. Thanks for your time.

1
can you give your jsf page code and backing bean code that how to set a value from input textJman
<h:inputTextarea id="trackingNumber" binding="#{researchBackingBean.trackingNumber}" validator="#{researchBackingBean.processesValidate}" value="#{shipmentHistory.trackingNumbers}" />userJ
BackingBean validation: private HtmlInputTextarea trackingNumber; let me know if this good enough.userJ

1 Answers

1
votes

Then #{history} is just null. Apparently you haven't declared it as a managed bean.

<managed-bean>
    <managed-bean-name>history</managed-bean-name>
    <managed-bean-class>com.researchhistory.model.ShipmentHistory</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

But this is kind of odd. A model should not be treated as a controller. Depending on the functional requirement, there must surely be better ways. Perhaps you just want a new blank instance of ShipmentHistory everytime when the ResearchHisttoryController get created. In that case, do the job in the (post)constructor of the backing bean instead.

public ResearchHisttoryController() {
    history = new History();
}

(please note that you've a typo in the backing bean class name)