-1

I'm using PrimeFace4.0, JSF 2.2, Java, and XHTML. I have a string value that I need to pass from one bean to another. The value is selected on one XHTML page and when the user clicks to select, a new web page is launched. I can see from my server output that the value is successfully collected by the first bean (TestSelectBean), but I can't seem to get it into the next bean (TargetBeanFranz). When a String is hard coded into the bean, it works correctly. However, when I try to use the managed property to call it as per the user input, I get a NullPointer at the line of code (85) where I'm trying to use it.

The first HTML: testselect.xhtml

//irrelevant code
<p:layoutUnit position="center">
                    <h:form>
                        <p:selectOneRadio id="Test"  value="#{testSelectBean.selectedNameOfExperiments}" >
                            <f:selectItems id="selectedNameOfExperiments" value="#{testSelectBean.nameofexperiments}" var="entry" itemValue="#{entry.key}" itemLabel="#{entry.value}" />
                        </p:selectOneRadio>

                        <p:commandLink id="nonAjax"  action="open" actionListener="#{testSelectBean.getParameters}" style="margin-right:20px;" ajax="false">
                            <h:outputText value="Submit" />
                        </p:commandLink>

                    </h:form>
                </p:layoutUnit>

The user chosen selectedNameOfExperiments is succsefully passed to TestSelectBean:

@ManagedBean(name = "testSelectBean", eager = true)
@SessionScoped

public class TestSelectBean implements Serializable {

    private static final long serialVersionUID = 1L;
    protected String selectedNameOfExperiments;
    private final Map<String, String> nameofexperiments;
    private transient String selected;

    public TestSelectBean() throws SQLException {

        selected = new String();

        nameofexperiments = new HashMap<String, String>();
        XYexpdataServiceAdapter xydata = new XYexpdataServiceAdapterImpl();
        List<String> dbnameofexperiments = xydata.getNameofexperiments();
        for (String ta : dbnameofexperiments) {
            nameofexperiments.put(ta, ta);
        }
    }

    public String getSelectedNameOfExperiments() {
        return selectedNameOfExperiments;
    }

    public void setSelectedNameOfExperiments(String selectedNameOfExperiments1) {
        this.selectedNameOfExperiments = selectedNameOfExperiments1;
    }

    public Map<String, String> getNameofexperiments() {
        return nameofexperiments;
    }

    public void getParameters() {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("You've selected Experiment:" + selectedNameOfExperiments));
        System.out.println("You've selected Experiment:" + selectedNameOfExperiments);
    }
}

However, the XHTML FranzwPopup will not launch unless Experiment is hard coded into TargetBeanFranz.

//irrelevant code
<p:layoutUnit position="center">

                    <p:panelGrid columns="2" style=" font-size: 14">
                        <h:outputLabel  value="Description" style="font-size: 20"/>
                        <h:outputText value="Neutron-induced production of protons, deuterons and tritons in Copper and Bismuth."/>

                        <h:outputLabel value="Reference" style="font-size: 20"/>
                        <h:outputText value="Nuclear Physics A510 (1990) 774-802 (Franz, et. al.)"/>
                    </p:panelGrid>

                    <form action="http://localhost:8080/primefaces/faces/handle.xhtml" method="get" name="login" onsubmit="process();
                            return false;" style="overflow-y: scroll">
                        Click to display chart data in a table.<br />
                        <input type="submit" value="Display"/>
                    </form>

                    <h:form>

                        <h:panelGrid columns="4" cellpadding="5">
                            <p:selectCheckboxMenu id="menu1" value="#{targetBeantFranz.selectedTargets}" label="Targets" filter="true" filterMatchMode="startsWith"
                                                  panelStyle="width:220px">
                                <f:selectItems value="#{targetBeantFranz.targets}" />
                            </p:selectCheckboxMenu>
                            <p:selectCheckboxMenu id="menu2" value="#{targetBeantFranz.selectedSecondaries}" label="Secondaries" filter="true" filterMatchMode="startsWith"
                                                  panelStyle="width:220px">
                                <f:selectItems value="#{targetBeantFranz.secondaries}" />
                            </p:selectCheckboxMenu>
                            <p:selectCheckboxMenu id="menu3" value="#{targetBeantFranz.selectedReactions}" label="Reactions" filter="true" filterMatchMode="startsWith"
                                                  panelStyle="width:220px">
                                <f:selectItems value="#{targetBeantFranz.reactions}" />
                            </p:selectCheckboxMenu>
                            <p:selectCheckboxMenu id="menu4" value="#{targetBeantFranz.selectedBeamEnergies}" label="Beam Energy" filter="true" filterMatchMode="startsWith"
                                                  panelStyle="width:220px">
                                <f:selectItems value="#{targetBeantFranz.beamenergies}" />
                            </p:selectCheckboxMenu>

                        </h:panelGrid>

                        <p:panel header="Resulting Plots"> 



                            <p:commandLink id="nonAjax" actionListener="#{targetBeantFranz.getParameters}" style="margin-right:20px;" ajax="false">
                                <button id="change">Submit Query</button>
                            </p:commandLink>

                            <div id="container" style="min-width: 310px; margin: 0 auto"></div>
                        </p:panel>
                    </h:form>

                </p:layoutUnit>
//more irrelevant 

Instead, I get a NPE on line 85 of TargetBeanFranz where I try to initialize Experiment.

//stuff
@ManagedBean(name = "targetBeantFranz", eager = true)
@SessionScoped

public class TargetBeanFranz implements Serializable {

    @ManagedProperty(value="#{testSelectBean}")
    private TestSelectBean testSelectBean;

    public TestSelectBean getTestSelectBean(){
        return testSelectBean;
    }

    public void setTestSelectBean (TestSelectBean testSelectBean) {
        this.testSelectBean = testSelectBean;
    }



    private static final long serialVersionUID = 1L;
    private String Experiment;
//more stuff
public TargetBeanFranz() throws SQLException {

        targets = new HashMap<String, String>();
        selectedTargets = new ArrayList<String>();
        secondaries = new HashMap<String, String>();
        selectedSecondaries = new ArrayList<String>();
        reactions = new HashMap<String, String>();
        selectedReactions = new ArrayList<String>();
        beamenergies = new HashMap<String, String>();
        selectedBeamEnergies = new ArrayList<String>();
        seriesDataArray = new ArrayList<String>();
        seriesDataArArray = new ArrayList<ArrayList<String>>();
        seriesNameArray = new ArrayList<String>();
        seriesToolTipArray = new ArrayList<String>();
        seriesErrorArray = new ArrayList<String>();
        seriesErrorArArray = new ArrayList<ArrayList<String>>();
        allselected = new ArrayList<XYexpdata>();
        Experiment = testSelectBean.getSelectedNameOfExperiments(); //This is line 85 and where I'm getting the NPE.


        XYexpdataServiceAdapter xydata = new XYexpdataServiceAdapterImpl();
        List<String> dbtargets = xydata.getTargetNamesbyExperiment(Experiment);
        for (String ta : dbtargets) {
            targets.put(ta, ta);
        }
        List<String> dbsecondaries = xydata.getSecondaryNamesbyExperiment(Experiment);
        for (String ta : dbsecondaries) {
            secondaries.put(ta, ta);

        }
        List<String> dbreactions = xydata.getReactionNamesbyExperiment(Experiment);
        for (String ta : dbreactions) {
            reactions.put(ta, ta);
        }
        List<String> dbbeamenergies = xydata.getBeamEnergybyExperiment(Experiment);
        for (String ta : dbbeamenergies) {
            beamenergies.put(ta, ta);
        }

    }
//more code
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
SheldonFan
  • 31
  • 7
  • add process="@name_of_your_form" in the command link you click to go to the next page. I think you are not processing that form that's why that attribute is null when you access on the second bean – eldjon Aug 02 '14 at 02:09

1 Answers1

0

I think the reason for your problem is, that the signature of the action listener in your command link does not match.

The method in the bean has to be of the following type:

public void getParameters(javax.faces.ActionEvent event) {
   .....
}

If you are using Java EE 7 you should also avoid the annotations in the package javax.faces.bean, since they are abandoned. Use CDI beans, it makes life easier:

@Named
@SessionScoped
public class TestSelectBean {
 ...
}

@Named
@SessionScoped  
public class TargetBeanFranz {

    @Inject
    private TestSelectBean testSelectBean;

    ....
}

See more about the CDI topic here: Backing beans (@ManagedBean) or CDI Beans (@Named)?

Community
  • 1
  • 1
Spindizzy
  • 2,073
  • 15
  • 23