1 2 Previous Next 28 Replies Latest reply: Nov 26, 2012 3:26 AM by 925715 Go to original post RSS
      • 15. Re: How To add blank value to af:selectOneChoice?
        Alejandro Profet
        Well, maybe you need to change the above code for this:
        Integer index = (Integer)getSoc3().getValue();
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (UIComponent ch : getSoc3().getChildren())
          if (ch instanceof UISelectItems)
            items.addAll((List<SelectItem>)((UISelectItems)ch).getValue());
          else if (ch instanceof UISelectItem)
            items.add((SelectItem)((UISelectItem)ch).getValue());
        if (items.size() > 0)
          String label = items.get(index).getLabel();
        AP
        • 16. Re: How To add blank value to af:selectOneChoice?
          925715
          Can u plz give me import statements for List,SelectItem,ArrayList?
          • 17. Re: How To add blank value to af:selectOneChoice?
            Alejandro Profet
            Of course:
            import java.util.List;
            import java.util.ArrayList;
            import javax.faces.component.UISelectItem;
            import javax.faces.component.UISelectItems;
            AP
            • 18. Re: How To add blank value to af:selectOneChoice?
              925715
              getting null pointer exception:

              //code

              System.out.println("Before new code");
              String label="";
              System.out.println("1");
              Integer index = (Integer)getSoc3().getValue();
              System.out.println("2");
              List<SelectItem> items = new ArrayList<SelectItem>();
              System.out.println("3");
              for (UIComponent ch : getSoc3().getChildren())
              if (ch instanceof UISelectItems)
              {System.out.println("4");
              items.addAll((List<SelectItem>)((UISelectItems)ch).getValue());
              }
              else if (ch instanceof UISelectItem){
              System.out.println("5");
              items.add((SelectItem)((UISelectItem)ch).getValue());
              }
              if (items.size() > 0)
              label = items.get(index).getLabel();

              System.out.println("Label value is :"+label);

              // Error
              Before new code
              1
              2
              3
              5
              4
              java.lang.NullPointerException
              • 19. Re: How To add blank value to af:selectOneChoice?
                Alejandro Profet
                Change this line:
                items.addAll((List<SelectItem>)((UISelectItems)ch).getValue());
                for these:
                UISelectItems uisis = (UISelectItems)ch;
                List<SelectItem> list = (List<SelectItem>)uisis.getValue();
                items.addAll(list);
                and check if uisis/list is null.

                AP
                • 20. Re: How To add blank value to af:selectOneChoice?
                  925715
                  I modifeied the code still igot the same error:
                  Before new code
                  1
                  2
                  3
                  5
                  4
                  java.lang.NullPointerException

                  // Modified code
                  System.out.println("Before new code");
                  String label="";
                  System.out.println("1");
                  Integer index = (Integer)getSoc3().getValue();
                  System.out.println("2");
                  List<SelectItem> items = new ArrayList<SelectItem>();
                  System.out.println("3");
                  for (UIComponent ch : getSoc3().getChildren())
                  if (ch instanceof UISelectItems)
                  {System.out.println("4");
                  //items.addAll((List<SelectItem>)((UISelectItems)ch).getValue());
                  UISelectItems uisis = (UISelectItems)ch;
                  if(uisis!=null)
                  {
                  List<SelectItem> list = (List<SelectItem>)uisis.getValue();
                  if(list!=null){
                  items.addAll(list);
                  }
                  else {
                  System.out.println("list is null");
                  }
                  }
                  else {
                  System.out.println("uisis is null");
                  }

                  }
                  else if (ch instanceof UISelectItem){
                  System.out.println("5");
                  items.add((SelectItem)((UISelectItem)ch).getValue());
                  }
                  if (items.size() > 0)
                  label = items.get(index).getLabel();

                  System.out.println("Label value is :"+label);
                  • 21. Re: How To add blank value to af:selectOneChoice?
                    Alejandro Profet
                    Well, items.get(index) seems that returns null.
                    Can you try with this code?
                    <af:selectOneChoice value="#{bindings.EmpLovVO1.inputValue}"
                                        shortDesc="#{bindings.EmpLovVO1.hints.tooltip}"
                                        binding="#{backingBeanScope.backing_EmpSearchPage.soc3}"
                                        id="soc3" partialTriggers="soc2"
                                        autoSubmit="true" immediate="true">
                      <f:selectItem id="siTrueFalse1" itemValue=""/>
                      <af:forEach var="item" items="#{bindings.EmpLovVO1.items}">
                        <f:selectItem itemValue="#{item.value}" itemLabel="#{item.label}" id="si4"/>
                      </af:forEach>
                    </af:selectOneChoice>
                    AP
                    • 22. Re: How To add blank value to af:selectOneChoice?
                      925715
                      <af:selectOneChoice value="#{bindings.OperationLovVO1.inputValue}"
                      shortDesc="#{bindings.OperationLovVO1.hints.tooltip}"
                      binding="#{backingBeanScope.backing_XxalvGmeShopFloorSearchPage.soc3}"
                      id="soc3" partialTriggers="soc2"
                      autoSubmit="true" immediate="true">
                      <f:selectItem id="siTrueFalse1" itemValue=""/>
                      <af:forEach var="item" items="#{bindings.OperationLovVO1.items}">
                      <f:selectItem itemValue="#{item.value}" itemLabel="#{item.label}" id="si4"/> -- its showing error: Reference item.value not found
                      </af:forEach>
                      </af:selectOneChoice>
                      • 23. Re: How To add blank value to af:selectOneChoice?
                        Alejandro Profet
                        Yes, but is just a warning because JDev is not able to find the reference, so ignore it.

                        AP
                        • 24. Re: How To add blank value to af:selectOneChoice?
                          925715
                          NOw got the error:

                          Before new code
                          1
                          2
                          3
                          5
                          5
                          java.lang.NullPointerException
                          • 25. Re: How To add blank value to af:selectOneChoice?
                            Alejandro Profet
                            Try this:
                            Integer index = (Integer)getSoc3().getValue();
                            List<UISelectItem> items = new ArrayList<UISelectItem>();
                            for (UIComponent ch : getSoc3().getChildren())
                              if (ch instanceof UISelectItem)
                                items.add((UISelectItem)ch);
                            if (items.size() > 0)
                              String label = items.get(index).getItemLabel();
                            AP
                            • 26. Re: How To add blank value to af:selectOneChoice?
                              925715
                              This time i didnt get error but o selected value in soc3 but its showing null:
                              Output:
                              1
                              2
                              3
                              4
                              5
                              6
                              4
                              5
                              6
                              7
                              8
                              LAbel value:null
                              9

                              code:
                              String label =null;
                              System.out.println("1");
                              Integer index = (Integer)getSoc3().getValue();
                              System.out.println("2");
                              List<UISelectItem> items = new ArrayList<UISelectItem>();
                              System.out.println("3");
                              for (UIComponent ch : getSoc3().getChildren())
                              {
                              System.out.println("4");
                              if (ch instanceof UISelectItem)
                              {
                              System.out.println("5");
                              items.add((UISelectItem)ch);
                              System.out.println("6");
                              }
                              }
                              if (items.size() > 0)
                              {
                              System.out.println("7");
                              label = items.get(index).getItemLabel();
                              System.out.println("8");
                              }
                              System.out.println("LAbel value:"+label);
                              System.out.println("9");
                              • 27. Re: How To add blank value to af:selectOneChoice?
                                Alejandro Profet
                                It seems that index is 0 (or label of 2nd item is null).
                                Also there are only two items in your af:selectOneChoice, is that correct?

                                Another way to get the label is this one:
                                FacesContext ctx = FacesContext.getCurrentInstance();
                                Application app = ctx.getApplication();
                                ExpressionFactory elFactory = app.getExpressionFactory();
                                ELContext elContext = ctx.getELContext();
                                ValueExpression valueExp = elFactory.createValueExpression(elContext, "#{bindings.OperationLovVO1.items[bindings.OperationLovVO1.inputValue].label}", Object.class);
                                String label = (String)valueExp.getValue(elContext);
                                AP
                                • 28. Re: How To add blank value to af:selectOneChoice?
                                  925715
                                  Thanks for ur support.
                                  i got the label value but i selected the unselected label from the lov list but instead of that i got the first value in the list in the label.

                                  what should i do now?
                                  1 2 Previous Next