1

I am trying to insert multiple IPs in a List<String> using a simple example. But I am getting following error.

javax.el.PropertyNotFoundException: Target Unreachable, 'BracketSuffix' returned null

Here is my JSF 2.2 page:

<h:form id="form">
    <ui:repeat value="#{exampleBean.ipAddresses}" var="s"
        varStatus="status">
        <h:inputText value="#{exampleBean.ipAddresses[status.index]}" />
    </ui:repeat>
    <h:inputText value="#{exampleBean.newIp}" />
    <h:commandButton value="Add" action="#{exampleBean.add}" />
    <h:commandButton value="Save" action="#{exampleBean.save}" />
</h:form>

And here is my backing bean:

@ManagedBean
@ViewScoped
public class ExampleBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private List<String> ipAddresses;
    private String newIp;

    @PostConstruct
    public void init() {
        ipAddresses= new ArrayList<String>();
    }

    public String save() {
        System.out.println(ipAddresses.toString());
        return null;
    }

    public void add() {
        ipAddresses.add(newIp);
        newIp = null;
    }

    public List<String> getIpAddresses() {
        return ipAddresses;
    }

    public String getNewIp() {
        return newIp;
    }

    public void setNewIp(String newIp) {
        this.newIp = newIp;
    }

}

How is this caused and how can I solve it?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
TusharM
  • 35
  • 1
  • 4

1 Answers1

2

javax.el.PropertyNotFoundException: Target Unreachable, 'BracketSuffix' returned null

The exception message is wrong. This is a bug in the EL implementation being used by the server. What it really meant here in your specific case is:

javax.el.PropertyNotFoundException: Target Unreachable, 'ipAddresses[status.index]' returned null

In other words, there's no such item in the array list. This suggests that the bean got recreated on form submit and therefore reinitializes everything to default. It thus behaves like a @RequestScoped one. Most likely you imported the wrong @ViewScoped annotation. For a @ManagedBean, you need to make sure that the @ViewScoped is imported from the very same javax.faces.bean package, and not thus the JSF 2.2-introduced javax.faces.view one which is specifically for CDI @Named beans.

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

See also:


Update: as per the comments, you're using WebSphere 8.5 which usually ships with an ancient MyFaces 2.0.x version. I reproduced your problem with MyFaces 2.0.5. Its <ui:repeat> failed to remember its view state for iteration status, that's why your construct still fails even though you're properly using a @ViewScoped bean. I could work around it by using <c:forEach> instead.

<c:forEach items="#{exampleBean.ipAddresses}" var="s" varStatus="status">
    ...
</c:forEach>

The alternate solution (apart from upgrading MyFaces to a more recent/decent version, obviously) would be to wrap the immutable String in a mutable javabean such as

public class IpAddress implements Serializable {
    private String value;
    // ...
}

so that you can use List<IpAddress> instead of List<String> and thus you don't need the varStatus anymore which triggered the MyFaces bug.

private List<IpAddress> ipAddresses;
private IpAddress newIp;

@PostConstruct
public void init() {
    ipAddresses= new ArrayList<IpAddress>();
    newIp = new IpAddress();
}

<ui:repeat value="#{exampleBean.ipAddresses}" var="ipAddress">
    <h:inputText value="#{ipAddress.value}" />
</ui:repeat>
<h:inputText value="#{exampleBean.newIp.value}" />
Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Thanks for answer... but i still have that error. here are my imports import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; So.. when i open a page.. it shows me one textbox, add and Save buttons. Then when i put some text in text box and click add... it shows 2 textboxes and text in first box is gone.. After this when i click add / save.. I get above error... I have posted full code.. so you can copy and try... Thanks – TusharM Feb 17 '16 at 16:14
  • Your bean is reconstructed on every request. You could confirm that youself by putting a breakpoint in `@PostConstruct`. It may not be called during form submit requests. If that still happens in spite of the correct annotation then there are several causes, the most common being: 1) duplicate different versioned JSF libraries in classpath, 2) session cookies are not maintained. – BalusC Feb 17 '16 at 16:16
  • I tried breakpoints and see that @PostConstruct is not getting called for each request... not sure where the problem is.... Can you please try my code and see if you can reproduce it?? – TusharM Feb 17 '16 at 20:17
  • Your problem is not reproducible on Mojarra 2.2.12 + Tomcat 8.0.32. Have you tried putting a breakpoint on `getIpAddresses()` to check if it returns the right list on postbacks? – BalusC Feb 17 '16 at 20:18
  • Which JSF impl/version and server impl/version are you using? – BalusC Feb 17 '16 at 21:31
  • WebSphere 8.5 bundles MyFaces 2.0.0~2.0.5 (depending on latest patch), which is quite ancient and bug ridden. How exactly did you upgrade/install Mojarra 2.2 on it? – BalusC Feb 18 '16 at 08:01
  • I followed this link http://stackoverflow.com/questions/13125782/how-to-detect-current-jsf-version and seems like i am using MyFaces JSF API Location: CodeSource, url=file:/C:/Program Files (x86)/IBM/WebSphere/AppServer/plugins/javax.j2ee.jsf.jar, JSF Impl Location: CodeSource, url=file:/C:/Program Files (x86)/IBM/WebSphere/AppServer/plugins/com.ibm.ws.jsf.myfaces.jar, So... What should i do now to fix my original problem? – TusharM Feb 18 '16 at 15:46
  • Normally, JSF impl/version is logged to webapp startup log. – BalusC Feb 18 '16 at 15:48
  • I just tried MyFaces 2.0.5 and I reproduced your problem. See updated answer. – BalusC Feb 18 '16 at 15:58
  • Perfect !! updated answer worked like charm.. only thing is.. in save() i dont get most recent item... For one entry i get [].... for second i get [tushar]... and so on...am i missing any index ?? – TusharM Feb 18 '16 at 17:50
  • It's still in `newIp` because you didn't press `add()`. – BalusC Feb 18 '16 at 17:50