6 Replies Latest reply on Feb 4, 2008 5:38 AM by archana m

    Boolean to integer customconverter!!

    archana m Newbie

      Hi
      I tried to create a custom converter,hoping to convert from Boolean to Integer.
      but in the converterclass the mothod "getAsObject" is not invoking..

      and am getting exception-javax.faces.convert.ConverterException: java.lang.NullPointerException

      can anyone plese help me.

        • 1. Re: Boolean to integer customconverter!!
          Pete Muir Master

          Show your code and the stack trace.

          • 2. Re: Boolean to integer customconverter!!
            archana m Newbie

            hi This is my class

            package org.domain.Portal.entity;

            import javax.faces.component.UIComponent;
            import javax.faces.context.FacesContext;
            import javax.faces.convert.ConverterException;
            import org.jboss.seam.ScopeType;
            import org.jboss.seam.annotations.Logger;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.annotations.Scope;
            import org.jboss.seam.annotations.faces.Converter;
            import org.jboss.seam.annotations.intercept.BypassInterceptors;
            import org.jboss.seam.log.Log;


            @Name("tst")
            @Scope(ScopeType.SESSION)
            @BypassInterceptors
            @Converter

            public class Tst extends com.sun.faces.renderkit.html_basic.CheckboxRenderer implements javax.faces.convert.Converter {

            @Logger private Log log;

            public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException {
            try {
            log.warn("MyConverter.getAsObject action called");
            return (arg2 == null || arg2.equals("") || arg2.equals("0") || arg2.equals("false") ? new Integer(0) : new Integer(1));
            } catch (Exception e) {
            throw new ConverterException(e);
            }
            }

            public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {
            try {

            log.warn("MyConverter.getAsString action called: "+arg2);

            if (arg2 instanceof Boolean)

            {
            log.warn("if getAsString action called"+arg2);
            return (arg2 == null || ((Boolean)arg2).equals(false) ? "0" :"1");

            }
            else{
            log.warn("else getAsString action called"+arg2);
            return (arg2 == null || ((Integer)arg2).intValue() == 0 ? "0" : "1");

            }
            } catch (Exception e) {
            throw new ConverterException(e);
            }


            }




            }

            • 3. Re: Boolean to integer customconverter!!
              archana m Newbie

              and i tried to use the converter in XTHML in this way


              <s:decorate id="activeDecoration" template="layout/edit.xhtml">
              <ui:define name="label">Active</ui:define>
              <h:selectBooleanCheckbox id="active" value="#{roleHome.instance.active}" converter="#{tst}" >

              <a:support event="onselect" reRender="activeDecoration" />
              </h:selectBooleanCheckbox>

              </s:decorate>

              • 4. Re: Boolean to integer customconverter!!
                Nicklas Karlsson Master

                I think the not-being-called it's a JSF-issue, check the sun forums, I have a feeling it comes up frequently there. You could do the conversion later (in getter/setter, or as a custom hibernate field (if that is an option)

                • 5. Re: Boolean to integer customconverter!!
                  archana m Newbie

                  ok
                  thanQ for ur immediate reply

                  • 6. Re: Boolean to integer customconverter!!
                    archana m Newbie

                    i am not getting any exception in stacktrace

                    but in ui this exception is throwing "RoleEdit.xhtml @48,109 value="#{roleHome.instance.active}": java.lang.IllegalArgumentException: argument type mismatch "

                    plese help me