1 Reply Latest reply: Jun 13, 2013 3:59 PM by TPD-Opitz RSS

    error handling when value is null

    Tshifhiwa

      hi i have a stuation where i what to display meaningfull error message if the lov is not selected,am navigating to next page based on lov selection,i what to display good message if you try to navigate without making selection

       

      public String showSelectedOrg() {
      BindingContext lBindingContext = BindingContext.getCurrent();
      BindingContainer bindings = lBindingContext.getCurrentBindingsEntry();
      // get the list binding for the Organisation lov
      JUCtrlListBinding list = (JUCtrlListBinding)bindings.get("Organisationid");
      if (list.isEmpty()) {
      JSFUtils.addFacesErrorMessage("Select Atleast One Organisation");
      }
      // get the selected index from the list which is stored in the Orgidd1 attribute
      AttributeBinding attr = (AttributeBinding)bindings.getControlBinding("Orgid1");
      Integer selid = (Integer)attr.getInputValue();
      // load the listdata
      Object row = list.getDisplayData();
      // get the selected row from the list
      if (row == null){
      JSFUtils.addFacesErrorMessage("Select Atleast One Organisation");
      //JSFUtils.addFacesErrorMessage(msg);
      }
      Row lFromList = (Row)list.getValueFromList(selid);
      if( lFromList.equals(null)){
      JSFUtils.addFacesErrorMessage("Select Atleast One Organisation");
      //JSFUtils.addFacesErrorMessage(msg);
      }
      // from the row we get the PK the orgnasationId
      Object lAttribute = lFromList.getAttribute("Organisationid");
      Object orgname = lFromList.getAttribute("Organisationname");
      Number newVal = (Number)lAttribute;
      String neworg = (String)orgname;
      _logger.info("Information: selected Organisation = " + newVal);
      System.out.println("neworg " + newVal);
      System.out.println("testorg " + neworg);
      selectedState = neworg;
      // get the MethodAction for setCurrentRowWithKeyValue
      OperationBinding method = bindings.getOperationBinding("setCurrentRowWithKeyValue");
      // set hte needed parameter as the organisation id
      method.getParamsMap().put("rowKey", newVal);
      method.execute();
      // after execution check for errors
      if (list.isEmpty()) {
      JSFUtils.addFacesErrorMessage("Select Atleast One Organisation");
      }
      List errors = method.getErrors();
      if (!errors.isEmpty()) {
      JSFUtils.addFacesErrorMessage("Select Atleast One Organisation");
      /* Exception ex = (Exception)errors.get(0);
      FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), "Select Atleast One Organisation");
      FacesContext.getCurrentInstance().addMessage(null, msg);*/
      // keep on page in case of an error
      return null;
      }
      // navigate to the next page
      return "show";

      }

      public static void addFacesErrorMessage(String msg) {
      FacesContext ctx = getFacesContext();
      FacesMessage fm =
      new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, "");
      ctx.addMessage(getRootViewComponentId(), fm);
      }

      currently am just geting NPE IN THIS LINE

      Row lFromList = (Row)list.getValueFromList(selid);

      Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException

          at org.apache.myfaces.trinidad.component.MethodExpressionMethodBinding.invoke(MethodExpressionMethodBinding.java:51)

          at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)

          at org.apache.myfaces.trinidad.component.UIXCommand.broadcast(UIXCommand.java:190)

          at oracle.adf.view.rich.component.fragment.UIXRegion.broadcast(UIXRegion.java:148)

          at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl.broadcastEvents(LifecycleImpl.java:1018)

          at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._executePhase(LifecycleImpl.java:386)

          at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:194)

          at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)

          ... 38 more

      Caused by: java.lang.NullPointerException

          at uam.cadastre.gov.za.OrgDetails.showSelectedOrg(OrgDetails.java:2414)

          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

          at java.lang.reflect.Method.invoke(Method.java:597)

          at com.sun.el.parser.AstValue.invoke(Unknown Source)

          at com.sun.el.MethodExpressionImpl.invoke(Unknown Source)

          at org.apache.myfaces.trinidad.component.MethodExpressionMethodBinding.invoke(MethodExpressionMethodBinding.java:46)

          ... 45 more