1

Ok, so I get this error and found no solution working for me (I tried around a lot with annotations like @FacesConverter or @Named instead @ManagedBean etc.) If anybody could point me to the right direction, that would be awesome. BalusC mentioned, that this particular error means, that the converter cannot be found, but the Converter is getting called, as I can see in log messages of JBoss (see log.info calls below in the Converter code). At least I don't get NullPointerExceptions from the Converter anymore (although I can't reproduce why). I'm getting a bit frustrated with JSF, but I'm sure it's my fault and I have overseen something obvious?

The code should be simple enough, I want to set zero, one or more Tags for a Link entity (newLink.tags) with means of a selectManyCheckbox:

This is my XHTML form:

<h:form id="create_link_form" rendered="#{authController.loggedIn}">
    <h3>Enter a new Link here:</h3>`
    <h:panelGrid columns="3" columnClasses="titleCell">
        <h:outputLabel for="name" value="Name:"/>
        <h:inputText id="name" value="#{newLink.name}"/>
        <p:message for="name" errorClass="invalid"/>

        <h:outputLabel for="url" value="URL:"/>
        <h:inputText id="url" value="#{newLink.url}"/>
        <p:message for="url" errorClass="invalid"/>

        <h:outputLabel for="description" value="Description:"/>
        <h:inputText id="description" value="#{newLink.description}"/>
        <p:message for="description" errorClass="invalid"/>

        <h:outputLabel for="shared" value="Shared?:"/>
        <h:selectBooleanCheckbox id="shared" label="Shared?:" value="#{newLink.shared}"/>
        <p:message for="shared" errorClass="invalid"/>

    </h:panelGrid>

        <h:outputLabel for="tags" value="Tags:"/>
        <h:selectManyCheckbox label="Tags:" id="tags" value="#{newLink.tags}" converter="#{tagConverter}">
            <f:selectItems value="#{tags}" var="tag" itemLabel="#{tag.name}" itemValue="#{tag}"/>
        </h:selectManyCheckbox>

    <h:inputHidden id="owner" value="#{newLink.owner}" name="{authController.loggedInUserName}"/>
    <p>
        <h:panelGrid columns="2">
            <h:commandButton id="create" action="#{linkController.create}" value="Create"
                             styleClass="create">
            </h:commandButton>
            <p:messages styleClass="messages" autoUpdate="true" globalOnly="true"/>
        </h:panelGrid>
    </p>
</h:form>

And this is my Converter:

@RequestScoped
@ManagedBean
public class TagConverter implements Converter {

@Inject
private Logger log;

@Inject
private TagService tagService;

@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {

    if (value != null && value.trim().length() > 0) {
        try {
            log.info("TTagConverter.getAsObject() => having " + value);
            Long id = Long.parseLong(value);
            Tag tag = tagService.getTagWithId(id);
            log.info("TagConverter.getAsObject() => Tag converted: " + tag);
            return tag;
        } catch (NumberFormatException e) {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid tag."));
        }
    } else {
        return null;
    }
}

@Override
public String getAsString(FacesContext fc, UIComponent uic, Object object) {
    if (object != null) {
        if (object instanceof Tag) {
            Long id = ((Tag) object).getId();
            return id.toString();
        } else {
            throw new ConverterException(new FacesMessage("There was an Object type error for a "
                    + object.getClass() + " in getAsString(" + object + ")"));
        }
    } else {
        return null;
    }
}
}
Tiny
  • 27,221
  • 105
  • 339
  • 599
Christof Kälin
  • 1,384
  • 2
  • 17
  • 26

1 Answers1

0

Your managed bean shouldnt implement it.

Create class and implement converter.

@FacesConverter("myConverter")
public class MyConverter implements Converter{
...
}

in your facelets

<h:selectManyCheckbox label="Tags:" id="tags" value="#{newLink.tags}">
            <f:selectItems value="#{tags}" var="tag" itemLabel="#{tag.name}" itemValue="#{tag}"/>
            <f:converter converterId="myConverter" />
</h:selectManyCheckbox>

Is it clear?

hiimjames
  • 431
  • 4
  • 10
  • Thanks, unfortunately not yet working, it sees the converter, but now I get NullPointerErrors whenever I choose at least 1 tag: java.lang.NullPointerException ch.linuxhome.projects.ckLinks.controller.TagConverter.getAsObject(TagConverter.java:37) And interestingly, if I don't chose a tag, I still get the "null Converter" error as in the OP – Christof Kälin Dec 07 '14 at 19:05
  • Also try this: @FacesConverter(value = "myConverter") or try to debug where your code exactly fails. Or try create some very simple converter for text input and dont need check for null, really simple. – hiimjames Dec 07 '14 at 20:30
  • OK, it works now like this: Using only @Named for the Converter class. Interesting isn't it? It seems as the NPE comes from not having it EL-ready or so... AND I had a hidden field pointing to an Owner field of the Model, that was making problems (although saying nothing about a missing Converter) – Christof Kälin Dec 11 '14 at 13:55
  • Sorry this might sound a bit chaotic, but it was chaotic for me ;-) – Christof Kälin Dec 11 '14 at 13:55